Skip to content

Commit f0b6560

Browse files
committed
add 3144
1 parent e8a3a7a commit f0b6560

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ LeetCode Solution In Hard Way
259259
|3151|[Special Array I](https://leetcode.cn/problems/special-array-i/) | [C++](./src/Problem_3151_isArraySpecial.cc)|Easy|
260260
|3148|[Maximum Difference Score in a Grid](https://leetcode.cn/problems/maximum-difference-score-in-a-grid/) | [C++](./src/Problem_3148_maxScore.cc)|Medium|
261261
|3145|[Maximum Difference Score in a Grid](https://leetcode.cn/problems/find-products-of-elements-of-big-array/) | [C++](./src/Problem_3145_findProductsOfElements.cc)|Hard|
262+
|3144|[Minimum Substring Partition of Equal Character Frequency](https://leetcode.cn/problems/minimum-substring-partition-of-equal-character-frequency/) | [C++](./src/Problem_3144_minimumSubstringsInPartition.cc)|Medium|
262263
|3134|[Find the Median of the Uniqueness Array](https://leetcode.cn/problems/find-the-median-of-the-uniqueness-array/) | [C++](./src/Problem_3134_medianOfUniquenessArray.cc)|Hard|
263264
|3131|[Find the Integer Added to Array I](https://leetcode.cn/problems/find-the-integer-added-to-array-i/) | [C++](./src/Problem_3131_addedInteger.cc)|Easy|
264265
|3117|[Minimum Sum of Values by Dividing Array](https://leetcode.cn/problems/minimum-sum-of-values-by-dividing-array/) | [C++](./src/Problem_3117_minimumValueSum.cc)|Hard|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <cstdint>
2+
#include <string>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
class Solution
8+
{
9+
public:
10+
// 记忆化搜索
11+
int minimumSubstringsInPartition1(string s)
12+
{
13+
int n = s.length();
14+
// dp[i] 的含义为 s[0...i]内,最少能分割多少个平衡子串
15+
vector<int> dp(n, -1);
16+
return dfs(n - 1, s, dp);
17+
}
18+
19+
int dfs(int i, string& s, vector<int>& dp)
20+
{
21+
if (i < 0)
22+
{
23+
return 0;
24+
}
25+
if (dp[i] != -1)
26+
{
27+
return dp[i];
28+
}
29+
int ans = INT32_MAX;
30+
vector<int> cnt(26);
31+
// 不同字符种类
32+
int k = 0;
33+
// 字符计数的最大值
34+
int max = 0;
35+
for (int j = i; j >= 0; j--)
36+
{
37+
// 新字符加入
38+
k += cnt[s[j] - 'a']++ == 0;
39+
// 字符计数最大值
40+
max = std::max(max, cnt[s[j] - 'a']);
41+
// 检查是否每个字符的计数都一样
42+
if (i - j + 1 == k * max)
43+
{
44+
// 如果 s[i...j] 是平衡字符串,那么讨论子问题s[0...j-1]
45+
ans = std::min(ans, dfs(j - 1, s, dp) + 1);
46+
}
47+
}
48+
dp[i] = ans;
49+
return ans;
50+
}
51+
52+
// 动态规划
53+
int minimumSubstringsInPartition2(string s)
54+
{
55+
int n = s.length();
56+
vector<int> dp(n + 1, INT32_MAX);
57+
dp[0] = 0;
58+
for (int i = 0; i < n; i++)
59+
{
60+
vector<int> cnt(26);
61+
int k = 0;
62+
int max = 0;
63+
for (int j = i; j >= 0; j--)
64+
{
65+
k += cnt[s[j] - 'a']++ == 0;
66+
max = std::max(max, cnt[s[j] - 'a']);
67+
if (i - j + 1 == k * max)
68+
{
69+
dp[i + 1] = std::min(dp[i + 1], dp[j] + 1);
70+
}
71+
}
72+
}
73+
return dp[n];
74+
}
75+
};

0 commit comments

Comments
 (0)