简体   繁体   English

一个简单例子的空间复杂性

[英]Space complexity on a trivial example

So I was checking the codibility website for online code evaluation. 因此,我正在检查可编码性网站以进行在线代码评估。 I tried the demo example http://codility.com/c/intro/demoRVC9C9-W8X to familiarize myself with the system. 我尝试了演示示例http://codility.com/c/intro/demoRVC9C9-W8X来熟悉系统。

A zero-indexed array A consisting of N different integers is given. 给出了由N个不同整数组成的零索引数组A。 The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. 该数组包含[1 ..(N + 1)]范围内的整数,这意味着恰好缺少一个元素。 Your goal is to find that missing element. 您的目标是找到缺少的元素。 Write a function: 编写一个函数:

class Solution { public int solution(int[] A); 类Solution {public int solution(int [] A); } }

that, given a zero-indexed array A, returns the value of the missing element. 给定零索引数组A,则返回缺少元素的值。 For example, given array A such that: 例如,给定数组A使得:

A[0] = 2 A [0] = 2
A[1] = 3 A [1] = 3
A[2] = 1 A [2] = 1
A[3] = 5 A [3] = 5

the function should return 4, as it is the missing element. 该函数应返回4,因为它是缺少的元素。 Assume that: N is an integer within the range [0..100,000]; 假定:N是在[0..100,000]范围内的整数; the elements of A are all distinct; A的元素都是不同的; each element of array A is an integer within the range [1..(N + 1)]. 数组A的每个元素都是[1 ..(N +1)]范围内的整数。

Complexity: 复杂:

expected worst-case time complexity is O(N); 预期最坏情况下的时间复杂度为O(N);

expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments). 预期的最坏情况下的空间复杂度是O(1),超过了输入存储(不计算输入参数所需的存储)。

Elements of input arrays can be modified. 输入数组的元素可以修改。

Here is my solution in Python: 这是我在Python中的解决方案:

def solution(A):
  tmp = [ 0 for x in xrange( len(A) + 1 ) ]
  for i,x in enumerate( A ):
    tmp[ x - 1 ] = 1
  return 1 + tmp.index( 0 )

I was pretty sure that my solution would NOT be accepted because the space complexity is O(n) where n is the size of A. However the system accepted my answer with a perfect score. 我非常确定我的解决方案将不被接受,因为空间复杂度为O(n),其中n是A的大小。但是,系统以完美的分数接受了我的答案。 Am I missing something here? 我在这里想念什么吗?

My solution takes it's base from a ruby perspective. 我的解决方案是从红宝石的角度出发。

in ruby 在红宝石

def solution(a)
  (1..(a.count+1)).select{|x| !a.include?(x)}.first
end 

in python 在python中

def solution(A):
    # write your code in Python 2.7
    return filter(lambda x: (x not in A), range(1, (len(A)+2)))[0] 

This solution resulted 100/100 该解决方案得出100/100

def solution(A):

if not A:
    return 1
sumz=sum(xrange(1,(max(A)+1)))
if ((len(A)+1)) not in A:
    return len(A)+1

a_sum=sum(A)

return sumz-a_sum

My solution in Ruby. 我在Ruby中的解决方案。

def solution(a)    
  return 1 if a.empty?
  ac = a.count
  ((ac+1)*(ac+2))/2 - a.inject(0, :+)
end

.inject(0, :+) sums up elements in array. .inject(0,:+)汇总数组中的元素。

Just start lernig Python and this is my 100 score solution: 只需启动lernig Python,这就是我的100分解决方案:

def solution(A):
    a=[]
    a=A
    a.sort()
    b=0
    if len(a)<1:
        return 1
    if a[0] != 1:
        return 1
    else:
        for x in xrange(0,len(a)):
            b=b+int(a[x])
    if a[len(a)-1]==len(a)+1:        
        c=(1+len(a))*len(a)/2
        d=b-a[len(a)-1]
        e=c-d
        return e
    else:
        return len(a)+1

i know its not great im just start lerning Python 我知道它不是很好我只是开始学习Python

these are two complicated scenarios. 这是两个复杂的场景。

think simply. 简单思考。

step #1: order the array step #2: iterate trough the ordered array. 步骤1:对数组进行排序步骤2:对有序数组进行迭代。 if A[i] != i+1 you have fund the missing element. 如果A [i]!= i + 1,则表示您已经为缺失的元素提供了资金。 in case the loop iterates through the whole array than the last element is missing just return the last element + 1. 如果循环遍历整个数组而不是缺少最后一个元素,则只需返回最后一个元素+ 1。

this is 100% solution. 这是100%的解决方案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM