每日刷题 五月集训 第 4 天 - Greedy
1221. Split a String in Balanced Strings (Easy)
题目介绍:
Balanced strings are those that have an equal quantity of ‘L’ and ‘R’ characters.
Given a balanced string s, split it in the maximum amount of balanced strings.
Return the maximum amount of split balanced strings.
Example 1:
Input: s = “RLRRLLRLRL”
Output: 4
Explanation: s can be split into “RL”, “RRLL”, “RL”, “RL”, each substring contains same number of ‘L’ and ‘R’.
解题思路:
- 当正好碰到等数量的R和L之后算作一个string
- 可以用一个variable来更新当前R和L差值的量。如果count 等于0了那么说明又获得了一个R和 L 相等的string.
注意的地方 & 须记住的地方:
代码部分:
class Solution {
public int balancedStringSplit(String s) {
if(s.length() == 0 ){
return 0;
}
int res = 0 ;
int count = 0;
for(int i = 0; i< s.length(); i++){
if(s.charAt(i) == 'L'){
count ++;
}else{
count--;
}
if(count == 0){
res++;
}
}
return res;
}
}
**Time Complexity and Space Complexity: **
- Time Complexity: O(n)
- Space Complexity: O(1)
1827. Minimum Operations to Make the Array Increasing (Easy)
题目介绍:
You are given an integer array nums (0-indexed). In one operation, you can choose an element of the array and increment it by 1.
For example, if nums = [1,2,3], you can choose to increment nums[1] to make nums = [1,3,3].
Return the minimum number of operations needed to make nums strictly increasing.
An array nums is strictly increasing if nums[i] < nums[i+1] for all 0 <= i < nums.length - 1. An array of length 1 is trivially strictly increasing.
解题思路:
- 判断当前数字和前一个数字的差值,并且更新当前数字,以及把差值加到要返回的值中。
注意的地方 & 须记住的地方:
代码部分:
class Solution {
public int minOperations(int[] nums) {
int res = 0;
for(int i = 1; i < nums.length; i++){
if(nums[i] <= nums[i - 1]){
res += nums[i - 1] - nums[i] + 1;
nums[i] = nums[i - 1] + 1;
}
}
return res;
}
}
**Time Complexity and Space Complexity: **
- Time Complexity: O(n)
- Space Complexity: O(1)
2144. Minimum Cost of Buying Candies With Discount (Easy)
题目介绍:
A shop is selling candies at a discount. For every two candies sold, the shop gives a third candy for free.
The customer can choose any candy to take away for free as long as the cost of the chosen candy is less than or equal to the minimum cost of the two candies bought.
Example 1:
Input: cost = [1,2,3]
Output: 5
Explanation: We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.
The total cost of buying all candies is 2 + 3 = 5. This is the only way we can buy the candies.
Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.
The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.
解题思路:
- 对Array进行排序,然后由大到小更新相邻的两个数字的和然后跳过下一个数字,直到为0的地方。
注意的地方 & 须记住的地方:
代码部分:
class Solution {
public int minimumCost(int[] cost) {
if(cost.length == 0 ){
return 0;
}
if(cost.length == 1){
return cost[0];
}
int i = cost.length-1;
int sum = 0;
Arrays.sort(cost);
while( i >= 0 ){
sum += (i-1 >= 0)? cost[i] + cost[i-1] : cost[i];
i-= 3;
}
return sum;
}
}
**Time Complexity and Space Complexity: **
- Time Complexity: O( sort )
- Space Complexity: O( sort )
1400. Construct K Palindrome Strings (Medium)
题目介绍:
Given a string s and an integer k, return true if you can use all the characters in s to construct k palindrome strings or false otherwise.
Example 1:
Input: s = “annabelle”, k = 2
Output: true
Explanation: You can construct two palindromes using all characters in s.
Some possible constructions “anna” + “elble”, “anbna” + “elle”, “anellena” + “b”
解题思路:
- 寻找到的次数为偶数的字符可以和单个次数的字符看做一个整体(组成一个palindrome的字符串)
- 只关心是否K 大于等于单个次数的字符数量。
注意的地方 & 须记住的地方:
代码部分:
class Solution {
public boolean canConstruct(String s, int k) {
if(s.length() == 0 || s.length() < k){
return false;
}
int[] arr = new int[26];
int single = 0;
int even = 0;
for(int i = 0; i< s.length(); i++){
arr[s.charAt(i)-'a']++;
}
for(int i = 0; i< arr.length; i++){
if(arr[i] % 2 == 1){
single++;
}
}
return single <= k;
}
}
**Time Complexity and Space Complexity: **
- Time Complexity: O(n)
- Space Complexity: O(1) 用了fixed value大小的 Array
平衡字符串拆分与数组递增操作:算法解析与实现
这篇博客探讨了四个计算机科学领域的算法问题:如何最大限度地拆分平衡字符串,使数组严格递增所需的最小操作数,以折扣价购买糖果的最低成本,以及构建回文串的策略。对于每个问题,都提供了详细的解题思路、注意事项以及时间复杂度和空间复杂性的分析。通过这些实例,读者可以深入理解字符串处理、数组操作和动态规划等概念。
842

被折叠的 条评论
为什么被折叠?



