Skip to content

Commit

Permalink
Create time-needed-to-rearrange-a-binary-string.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Aug 22, 2022
1 parent 907b870 commit a26e27b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions C++/time-needed-to-rearrange-a-binary-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Time: O(n)
// Space: O(1)

// dp
class Solution {
public:
int secondsToRemoveOccurrences(string s) {
int result = 0, cnt = 0;
for (const auto& c : s) {
if (c == '0') {
++cnt;
continue;
}
if (cnt) {
result = max(result + 1, cnt);
}
}
return result;
}
};

0 comments on commit a26e27b

Please sign in to comment.