0
点赞
收藏
分享

微信扫一扫

每日刷题 第 4 天 - Greedy

small_Sun 2022-05-05 阅读 92

每日刷题 五月集训 第 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)

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
举报

相关推荐

0 条评论