Algorithm :Missing Number

Q: Missing Number

Description:
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

A1: Bit Manipulation

Intuition
We can harness the fact that XOR is its own inverse to find the missing element in linear time.

Algorithm
Because we know that nums containsnnumbers and that it is missing exactly one number on the range [0..n-1] , we know thatndefinitely replaces the missing number in nums. Therefore, if we initialize an integer tonand XOR it with every index and value, we will be left with the missing number. Consider the following example (the values have been sorted for intuitive convenience, but need not be):

Index 0 1 2 3
Value 0 1 3 4
WX20171111-150701@2x

Python3

class Solution:
    def missingNumber(self, nums):
        missing = len(nums)
        for i, num in enumerate(nums):
             missing ^= i ^ num
        return missing

Complexity Analysis

  • Time complexity : O(n)

    Assuming that XOR is a constant-time operation, this algorithm does constant work on nn iterations, so the runtime is overall linear.

  • Space complexity : O(1)

    This algorithm allocates only constant additional space.

A2:Gauss' Formula

Intuition
One of the most well-known stories in mathematics is of a young Gauss, forced to find the sum of the first 100 natural numbers by a lazy teacher. Rather than add the numbers by hand, he deduced a closed-form expression for the sum, or so the story goes. You can see the formula below:

QQ20171112-092718@2x.png

Algorithm

We can compute the sum of nums in linear time, and by Gauss' formula, we can compute the sum of the first n natural numbers in constant time. Therefore, the number that is missing is simply the result of Gauss' formula minus the sum of nums, as nums consists of the first n natural numbers minus some number.

Python3

class Solution:
    def missingNumber(self, nums):
        expected_sum = len(nums)*(len(nums)+1)//2
        actual_sum = sum(nums)
        return expected_sum - actual_sum

Complexity Analysis

  • Time complexity : O(n)

    Although Gauss' formula can be computed in O(1) time, summing nums costs us O(n) time, so the algorithm is overall linear. Because we have no information about which number is missing, an adversary could always design an input for which any algorithm that examines fewer thann numbers fails. Therefore, this solution is asymptotically optimal.

  • Space complexity : O(1)
    This approach only pushes a few integers around, so it has constant memory usage.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在暖暖晴天的鼓浪嶼,我給未來(lái)的自己寫(xiě)了幾封信,內(nèi)容太多,已然記不得。 大抵是愿歲月無(wú)恙,只是舊了時(shí)光之類的。 深知...
    我不是妖精閱讀 432評(píng)論 0 3
  • 1.少有人走得路 (完,9.12重看)2.烏有鄉(xiāng)(完)3.番茄工作法(完)4.熱夜之夢(mèng)(完)5.利維坦 (中斷)6...
    scarqin閱讀 205評(píng)論 0 0
  • 你說(shuō)我變了,卻忘了,如今,還有誰(shuí)會(huì)傻傻地在原地等你呢? 記不住有多久總是一個(gè)人在家里默默做菜等待,直到深夜蜷縮在床...
    94暖暖陽(yáng)光閱讀 242評(píng)論 3 2

友情鏈接更多精彩內(nèi)容