Skip to content

Commit a522ea3

Browse files
committed
2996. Smallest Missing Integer Greater Than Sequential Prefix Sum
1 parent 9b712a2 commit a522ea3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
3+
// Solution by Sergey Leschev
4+
// 2996. Smallest Missing Integer Greater Than Sequential Prefix Sum
5+
6+
// Time complexity: O(N * log(N))
7+
// Space complexity: O(1)
8+
9+
func missingInteger(_ nums: [Int]) -> Int {
10+
var countSum = nums[0]
11+
var sortedNums = Array(nums)
12+
sortedNums.sort()
13+
14+
for i in 1..<nums.count {
15+
if nums[i - 1] + 1 == nums[i] {
16+
countSum += nums[i]
17+
} else {
18+
break
19+
}
20+
}
21+
22+
for i in 0..<sortedNums.count {
23+
if countSum == sortedNums[i] {
24+
countSum += 1
25+
}
26+
}
27+
return countSum
28+
}
29+
}

0 commit comments

Comments
 (0)