简体   繁体   English

给定一个包含 N 个整数的数组 A,返回在 O(n) 时间复杂度内不会出现在 A 中的最小正 integer(大于 0)(Python Sol)

[英]Given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A in O(n) time complexity (Python Sol)

For anyone confused on the Codility Sample Test对于任何对 Codility 样本测试感到困惑的人

Write a function:写一个 function:

def solution(A)定义解决方案(A)

that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.即,给定一个包含 N 个整数的数组 A,返回 A 中未出现的最小正 integer(大于 0)。

For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.例如,给定 A = [1, 3, 6, 4, 1, 2],function 应该返回 5。

Given A = [1, 2, 3], the function should return 4.给定 A = [1, 2, 3],function 应该返回 4。

Given A = [−1, −3], the function should return 1.给定 A = [−1, −3],function 应返回 1。

Write an efficient algorithm for the following assumptions:为以下假设编写一个有效的算法:

N is an integer within the range [1..100,000]; N 是 [1..100,000] 范围内的 integer; each element of array A is an integer within the range [−1,000,000..1,000,000].数组 A 的每个元素都是 [−1,000,000..1,000,000] 范围内的 integer。

# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")

def solution(A):
    # write your code in Python 3.6
    pass

    #check that if maximum number in array is less than 1 then the smallest positive integer that doesn't occur in A will be 1
    if max(A) < 1:
        return 1
    
    #sorted set of A
    hashset = set(A)

    #smallest possible positive integer
    result = 1

    #iterate through array seeing if the integer is in the hashset
    while result in hashset:
        
        #increment result
        result += 1
    
    #return smallest positive integer (greater than 0) that does not occur in A
    return result

I think the fastest solution is define an external vector "n" with all possible int from 1 to 100'000我认为最快的解决方案是定义一个外部向量“n”,所有可能的 int 从 1 到 100'000

N = np.arange(100_000)
def solution(A):
    return n[~np.isin(N,A)][0]

I hope this is useful.我希望这很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 给定一个N个整数的数组A,在O(n)时间复杂度内返回A中没有出现的最小正数integer(大于0) - Python given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A in O(n) time complexity 取正整数n并返回小于n的所有正整数的平方和的Python函数 - Python function that takes a positive integer n and returns the sum of the squares of all the positive integers smaller than n 如何使用包含负整数的数组在 O(n) 时间复杂度中用 python 解决两个求和问题 - how to solve two sum problem with python in O(n) time complexity with an array that include negative integers Python-时间复杂度O(N ** 2) - Python - time complexity O(N**2) 接受一个正的 integer n 作为输入,并找到打印最小的 integer 可以被范围 [1, n] 内的所有整数整除 - Accept a positive integer n as input and find the print the smallest integer that is divisible by all the integers in the range [1, n] Python 时间复杂度将 O(n^2) 代码转换为 O(n) - Python time complexity convert O(n^2) code to O(n) O(N)简单Python函数的时间复杂度 - O(N) Time complexity for simple Python function 给定一个非负的 integer N,返回类似于 Python 中 N 的非负整数的个数 - Given a non-negative integer N, returns the number of non-negative integers similar to N in Python 在 Python 中找到大于或等于 n 的最小 2 次方 - Find the smallest power of 2 greater than or equal to n in Python python O(n log n)中算法的时间复杂度 - Time complexity of an algorithm in python O(n log n)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM