Skip to content

Commit 930cb69

Browse files
authored
Create minimum-levels-to-gain-more-points.cpp
1 parent 57b09cd commit 930cb69

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// prefix sum
5+
class Solution {
6+
public:
7+
int minimumLevels(vector<int>& possible) {
8+
vector<int> prefix(size(possible) + 1);
9+
for (int i = 0; i < size(possible); ++i) {
10+
prefix[i + 1] = prefix[i] + (possible[i] ? +1 : -1);
11+
}
12+
for (int i = 0; i + 1 < size(possible); ++i) {
13+
if (prefix[i + 1] > prefix.back() - prefix[i + 1]) {
14+
return i + 1;
15+
}
16+
}
17+
return -1;
18+
}
19+
};

0 commit comments

Comments
 (0)