Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Dec 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Create 3389.Minimum-Operations-to-Make-Character-Frequencies-Equal.cpp
  • Loading branch information
wisdompeak authored Dec 17, 2024
commit ad51a828733ffb78fd96dac7d2183fc4f730b7d3
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Solution {
public:
int makeStringGood(string s)
{
vector<int>freq(26);
for (char c : s) {
freq[c-'a']++;
}
int max_freq = *max_element(freq.begin(), freq.end());

int ret = INT_MAX/2;
vector<int>diff(26);

for (int target = 1; target <= max_freq; target++)
{
for (int i=0; i<26; i++)
diff[i] = freq[i] - target;
vector<vector<int>>dp(26, vector<int>(2, INT_MAX/2));

int carry;
dp[0][0] = freq[0];
dp[0][1] = abs(diff[0]);

for (int i=1; i<26; i++)
{
dp[i][0] = min(dp[i-1][0], dp[i-1][1]) + freq[i];

dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + abs(diff[i]);

if (i>=1 && diff[i-1]>0 && diff[i]<0)
{
int common = min(abs(diff[i-1]), abs(diff[i]));
dp[i][1] = min(dp[i][1], dp[i-1][1] + abs(diff[i])-common);
}

if (i>=1 && freq[i-1]>0 && diff[i]<0)
{
int common = min(abs(freq[i-1]), abs(diff[i]));
dp[i][1] = min(dp[i][1], dp[i-1][0] + abs(diff[i])-common);
}
}

ret = min(ret, min(dp[25][0], dp[25][1]));
}

return ret;
}
};

AltStyle によって変換されたページ (->オリジナル) /