forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsounmind.js
More file actions
26 lines (24 loc) · 877 Bytes
/
sounmind.js
File metadata and controls
26 lines (24 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
const expectedSum = (nums.length * (nums.length + 1)) / 2;
const actualSum = nums.reduce((prev, curr) => prev + curr);
return expectedSum - actualSum;
};
/**
* Time Complexity: O(n)
* Reason:
* - Finding the maximum number in the array takes O(n) time.
* - Calculating the target sum takes O(1) time.
* - Iterating through the array to update the target number takes O(n) time.
* - Checking the condition and returning the result takes O(1) time.
* - Therefore, the overall time complexity is O(n).
*
* Space Complexity: O(1)
* Reason:
* - The algorithm uses a constant amount of extra space (variables like maxNumber, hasZero, targetNumber).
* - No additional space proportional to the input size is used.
* - Therefore, the overall space complexity is O(1).
*/