[LeetCode] Smallest Missing Integer Greater Than Sequential Prefix Sum

10031. Smallest Missing Integer Greater Than Sequential Prefix Sum

You are given a 0-indexed array of integers nums.

A prefix nums[0..i] is sequential if, for all 1 <= j <= i, nums[j] = nums[j - 1] + 1. In particular, the prefix consisting only of nums[0] is sequential.

Return the smallest integer x missing from nums such that x is greater than or equal to the sum of the longest sequential prefix.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int missingInteger(vector<int>& nums) {
int res = nums[0];
for(int i = 1; i < nums.size(); i++) {
if(nums[i] == nums[i-1] + 1) res += nums[i];
else break;
}
unordered_set<int> us(begin(nums), end(nums));
while(us.count(res)) res += 1;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/01/07/PS/LeetCode/smallest-missing-integer-greater-than-sequential-prefix-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.