-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0e5c6c
commit 707fec7
Showing
2 changed files
with
22 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
C++/2914-MinimumNumberOfChangesToMakeBinaryStringBeautiful/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <cstddef> | ||
#include <cstdlib> | ||
#include <string_view> | ||
|
||
using namespace std; | ||
class Solution { | ||
public: | ||
int minChanges(std::string_view s) { | ||
// Seems rather simple, where the given string is of even length, it is | ||
// guaranteed that we can pair up elements, (which is the smallest possible | ||
// substring of size 2). | ||
// Therefore, for each pair, we can simply change pairs that have elements | ||
// that do not match for a cost of 1, in a greedy fashion. | ||
// Can we prove that the greedy choice works? | ||
int changes = 0; | ||
for (int i = 0; i < s.size(); i += 2) { | ||
changes += s[i] != s[i + 1]; | ||
} | ||
return changes; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters