Skip to content

Commit

Permalink
feat(C++): Add 2914
Browse files Browse the repository at this point in the history
  • Loading branch information
euchangxian committed Nov 5, 2024
1 parent f0e5c6c commit 707fec7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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;
}
};
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ Now solving in C++. Like it.
| 2816 | DoubleANumberRepresentedAsALinkedList | [![Go](assets/go.svg)](Go/2816-DoubleANumberRepresentedAsALinkedList/Solution.go) |
| 2830 | MaximizeTheProfitAsASalesman | [![Go](assets/go.svg)](Go/2830-MaximizeTheProfitAsASalesman/Solution.go) |
| 2838 | MaximumCoinsHeroesCanCollect | [![C++](assets/c++.svg)](C++/2838-MaximumCoinsHeroesCanCollect/Solution.cpp) |
| 2914 | MinimumNumberOfChangesToMakeBinaryStringBeautiful | [![C++](assets/c++.svg)](C++/2914-MinimumNumberOfChangesToMakeBinaryStringBeautiful/Solution.cpp) |
| 2931 | MaximumSpendingAfterBuyingItems | [![C++](assets/c++.svg)](C++/2931-MaximumSpendingAfterBuyingItems/Solution.cpp) |
| 2938 | SeparateBlackAndWhiteBalls | [![C++](assets/c++.svg)](C++/2938-SeparateBlackAndWhiteBalls/Solution.cpp) |
| 2976 | MinimumCostToConvertStringOne | [![C++](assets/c++.svg)](C++/2976-MinimumCostToConvertStringOne/Solution.cpp) |
Expand Down

0 comments on commit 707fec7

Please sign in to comment.