贪心法题目总结

1. 分饼干

Leetcode : 455. Assign Cookies (Easy)
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int gi = 0;
        int si = 0;
        while(gi<=g.length-1 && si<=s.length-1){
            if(s[si]>=g[gi]){si++; gi++;}
            else si++;
        }
        return gi;
    }
}

2.投飞镖刺破气球

Leetcode : 452. Minimum Number of Arrows to Burst Balloons (Medium)
思想:先按照头来排序。飞镖的位置指着尾部

public int findMinArrowShots(int[][] points) {
    if(points.length == 0) return 0;
    // 从小到大排序
    Arrays.sort(points,(a,b) -> (a[1] - b[1]));
    //记录飞镖位置
    int curPos = points[0][1];
    //记录飞镖的个数
    int ret = 1;
    for (int i = 1; i < points.length; i++) {
        if(points[i][0] <= curPos) {
            continue;
        }
        curPos = points[i][1];
        ret++;
    }
    return ret;
 }

3. 股票的最大收益

Leetcode : 122. Best Time to Buy and Sell Stock II (Easy)
题目描述:一次交易包含买入和卖出,多个交易之间不能交叉进行

思路:对于 [a, b, c, d],如果有 a <= b <= c <= d ,那么最大收益为 d - a。而 d - a = (d - c) + (c - b) + (b - a) ,因此当访问到一个 prices[i] 且 prices[i] - prices[i-1] > 0,那么就把 prices[i] - prices[i-1] 添加加到收益中,从而在局部最优的情况下也保证全局最优。

public int maxProfit(int[] prices) {
    int profit = 0;
    for(int i = 1; i < prices.length; i++){
        if(prices[i] > prices[i-1]) profit += (prices[i] - prices[i-1]);
    }
    return profit;
}

4. 种植花朵

Leetcode : 605. Can Place Flowers (Easy)
Input: flowerbed = [1,0,0,0,1], n = 1
Output: True

public boolean canPlaceFlowers(int[] flowerbed, int n) {
    int cnt = 0;
    for(int i = 0; i < flowerbed.length; i++){
        if(flowerbed[i] == 1) continue;
        \\这儿有个技巧,当Index是头或者尾部的时候,多添加0
        int pre = i == 0 ? 0 : flowerbed[i - 1];
        int next = i == flowerbed.length - 1 ? 0 : flowerbed[i + 1];
        if(pre == 0 && next == 0) {
            cnt++;
            flowerbed[i] = 1;
        }
    }
    return cnt >= n;
}

5. 修改一个数成为非递减数组

Leetcode : 665. Non-decreasing Array (Easy)
思路:
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 不影响后续的操作 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,那么就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2],只修改 nums[i - 1] = nums[i] 不能令数组成为非递减,只能通过修改 nums[i] = nums[i - 1] 才行。

public boolean checkPossibility(int[] nums) {
    int cnt = 0;
    for(int i = 1; i < nums.length; i++){
        if(nums[i] < nums[i - 1]){
            cnt++;
            if(i - 2 >= 0 && nums[i - 2] > nums[i]) nums[i] = nums[i-1];
            else nums[i - 1] = nums[i];
        }
    }
    return cnt <= 1;
}

6.判断是否为子串

Leetcode : 392. Is Subsequence (Medium)

s = "abc", t = "ahbgdc"
Return true.
class Solution {
    public boolean isSubsequence(String s, String t) {
        if(t.length() < s.length()) return false;
        int si = 0;
        int ti = 0;
        while(si<s.length()&&ti<t.length()){
            if(s.charAt(si)==t.charAt(ti)) si++;
            ti++;
        }
        if(si==s.length()) return true;
        else return false;
    }
}

7.分隔字符串使同种字符出现在一起

Leetcode : 763. Partition Labels (Medium)
思路: 因为一个字符只能落在一个区间内,所以你这个区间里的所有字符的头尾都在该区间内,所以我们遍历整个字符串,对每个字符串找到尾部,并根据情况决定是否更新当前区间的尾端

class Solution {
    public List<Integer> partitionLabels(String S) {
        List<Integer> ans = new ArrayList();
        // Define the start index and the end index of current part
        int startIndex = 0;
        int lastIndex = 0;
        // Iterate through given S
        for(int i=0; i<=S.length()-1; i++){
        // if the index we're searching is larger than the end index, we add the string length to array and then update the start index.
            if(i>lastIndex){
                ans.add(lastIndex-startIndex+1);
                startIndex = i;
            }
            if(i==S.length()-1) ans.add(i-startIndex+1);
            // 不能j=i+1,因为lastIndex可能是自己
            for(int j=i; j<=S.length()-1; j++){
                if(S.charAt(j)==S.charAt(i)&&j>lastIndex) lastIndex=j; 
            }
        }
        return ans;
    }
}  

8. 根据身高和序号重组队列

406. Queue Reconstruction by Height

class Solution {
    public int[][] reconstructQueue(int[][] people){
        if (people == null || people.length == 0 || people[0].length == 0) {
            return new int[0][0];
        }
        // 先排序,如果前面相同,从小到大来排,这样符合到时放进新数组的顺序。如果前面不同,按从大到小来排。小的放到后面,这样能保证插入后还是不影响原来的。比如末尾都是4,都是插到第四位,但如果先插大的,小的再插进来,大的不会受影响。
        Arrays.sort(people, (a,b)->(a[0] == b[0]? a[1] - b[1] : b[0] - a[0]));
        List<int[]> tmp = new ArrayList<>();
        for(int i=0; i<people.length; i++){
            tmp.add(people[i][1], new int[]{people[i][0], people[i][1]});
        }
        return tmp.toArray(new int[people.length][]);
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值