文章目录
- 0. Leetcode [1221. 分割平衡字符串](https://leetcode-cn.com/problems/split-a-string-in-balanced-strings/)
- 1. Leetcode [1827. 最少操作使数组递增](https://leetcode-cn.com/problems/minimum-operations-to-make-the-array-increasing/)
- 2. Leetcode [2144. 打折购买糖果的最小开销](https://leetcode-cn.com/problems/minimum-cost-of-buying-candies-with-discount/)
- 3. Leetcode [1400. 构造 K 个回文字符串](https://leetcode-cn.com/problems/construct-k-palindrome-strings/)
- 总结
0. Leetcode 1221. 分割平衡字符串
在一个 平衡字符串 中,‘L’ 和 ‘R’ 字符的数量是相同的。
给你一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
注意:分割得到的每个字符串都必须是平衡字符串,且分割得到的平衡字符串是原平衡字符串的连续子串。
返回可以通过分割得到的平衡字符串的 最大数量 。
提示:
1 <= s.length <= 1000
s[i] = ‘L’ 或 ‘R’
s 是一个 平衡 字符串
分析与解答
由于 s
为一个平衡串,因此在取出任意一个平衡串后余下的依然是一个平衡串。为了取出最多的平衡串,我们只需在遇到每个满足条件的平衡串时取出即可。该操作可以通过两个计数器完成,分别记录 L
与 R
的数量,当计数器相等时表示能够形成一个平衡串:
class Solution {
public:
int balancedStringSplit(string s) {
int result(0);
int numL(0), numR(0);
// 当 `numL` == `numR` 且 `numL != 0` & `numR != 0` 时就找到一个
for (int i = 0; i < s.size(); ++i) {
if (s[i] == 'L') {
numL++;
}
if (s[i] == 'R') {
numR++;
}
if (numL == numR && numL != 0) {
result++;
numL = 0; numR = 0;
}
}
return result;
}
};
由于只需要遍历一遍 string
,因此算法时间复杂度为
O
(
N
)
O(N)
O(N)
1. Leetcode 1827. 最少操作使数组递增
给你一个整数数组 nums (下标从 0 开始)。每一次操作中,你可以选择数组中一个元素,并将它增加 1 。
比方说,如果 nums = [1,2,3] ,你可以选择增加 nums[1] 得到 nums = [1,3,3] 。
请你返回使 nums 严格递增 的 最少 操作次数。
我们称数组 nums 是 严格递增的 ,当它满足对于所有的 0 <= i < nums.length - 1 都有 nums[i] < nums[i+1] 。一个长度为 1 的数组是严格递增的一种特殊情况。
提示:
1 <= nums.length <= 5000
1 <= nums[i] <= 104
分析与解答
要使得操作次数最少,且数列结果严格单调递增,只需要从前往后遍历数组,当遇到 n u m s [ i ] < = n u m s [ i − 1 ] nums[i] <= nums[i - 1] nums[i]<=nums[i−1] 时,令 n u m s [ i ] = n u m s [ i − 1 ] + 1 nums[i] = nums[i - 1] + 1 nums[i]=nums[i−1]+1 并更新操作次数即可:
class Solution {
public:
int minOperations(vector<int>& nums) {
int result(0);
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] <= nums[i - 1]) {
result += nums[i - 1] - nums[i] + 1;
nums[i] = nums[i - 1] + 1;
}
}
return result;
}
};
由于只需要遍历一遍 vector
,因此算法时间复杂度为
O
(
N
)
O(N)
O(N)
2. Leetcode 2144. 打折购买糖果的最小开销
一家商店正在打折销售糖果。每购买 两个 糖果,商店会 免费 送一个糖果。
免费送的糖果唯一的限制是:它的价格需要小于等于购买的两个糖果价格的 较小值 。
比方说,总共有 4 个糖果,价格分别为 1 ,2 ,3 和 4 ,一位顾客买了价格为 2 和 3 的糖果,那么他可以免费获得价格为 1 的糖果,但不能获得价格为 4 的糖果。
给你一个下标从 0 开始的整数数组 cost ,其中 cost[i] 表示第 i 个糖果的价格,请你返回获得 所有 糖果的 最小 总开销。
提示:
1 <= cost.length <= 100
1 <= cost[i] <= 100
分析与解答
根据题意,白嫖最贵的商品是不被允许的,因此我们的最佳策略是每次白嫖第三贵的商品。
- 将数组排序
- 从尾至头遍历数组,同时计数,每购入两个当前最贵的商品就白嫖第三个商品
- 返回购买的商品总价格
class Solution {
public:
void qSort(vector<int>& num, int low, int high) {
if (low >= high) {
return;
}
int pivot = num[low];
int l(low), r(high);
while (l < r) {
while (num[r] > pivot) {
r--;
}
if (l < r) {
num[l] = num[r];
}
while (num[l] <= pivot && l < r) {
l++;
}
if (l < r) {
num[r] = num[l];
}
}
num[l] = pivot;
qSort(num, low, l - 1);
qSort(num, l + 1, high);
}
int minimumCost(vector<int>& cost) {
qSort(cost, 0, cost.size() - 1);
int result(0);
for (int i = cost.size() - 1, k = 0; i >= 0; --i) {
k++; // 买入一个
if (k % 3 != 0) {
result += cost[i];
} else { // 白嫖一个
;
}
}
return result;
}
};
算法时间复杂度为 O ( N ) O(N) O(N)
3. Leetcode 1400. 构造 K 个回文字符串
给你一个字符串 s 和一个整数 k 。请你用 s 字符串中 所有字符 构造 k 个非空 回文串 。
如果你可以用 s 中所有字符构造 k 个回文字符串,那么请你返回 True ,否则返回 False 。
提示:
1 <= s.length <= 10^5
s 中所有字符都是小写英文字母。
1 <= k <= 10^5
分析与解答
字符串中的字母可能出现奇数次或偶数次。为了构造一个回文字符串,我们必须将出现奇数次的字母放在中间位置。因此对于一个字符串 s
是否能构造 k
个回文字符串,我们只需看 s
中奇数字母个数是否大于 k
,若奇数字母个数大于 k
,则不能满足构造要求。
class Solution {
public:
bool canConstruct(string s, int k) {
if (k > s.size()) { // 数量超过字符串长度
return false;
}
// 字符串中奇数字母个数 <= k 则 true,否则 false
map<char, int> chMap;
for (auto ch: s) {
chMap[ch]++;
}
int resNum(0);
for (auto ite = chMap.begin(); ite != chMap.end(); ++ite) {
if (ite->second % 2 == 1) {
resNum++;
}
}
if (resNum > k) {
return false;
} else {
return true;
}
}
};
由于遍历一遍字符串与最大长度为 26 的 map
,因此算法时间复杂度为
O
(
N
)
O(N)
O(N)
总结
贪心算法直觉上更易理解,但在理论证明上有时候并不容易。