小工具知识网

当前位置: 首页 > 行业知识 > 贪心算法(基础题)

行业知识

贪心算法(基础题)

2026-11-03 22:43:43 来源:互联网转载

455. 分发饼干

题目

��设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。

()

对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。

示例 1:

()
输入: g = [1,2,3], s = [1,1]输出: 1解释: 你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。所以你应该输出1。

示例 2:

输入: g = [1,2], s = [1,2,3]输出: 2解释: 你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。你拥有的饼干数量和尺寸都足以让所有孩子满足。所以你应该输出2.

答案

class Solution {    public int findContentChildren(int[] g, int[] s) {        Arrays.sort(g);        Arrays.sort(s);                int count = 0;        for(int i=0;i            if(s[i]=g[count]){                count++;            }        }        return count;    }}    public int wiggleMaxLength(int[] nums) {        int currDiff = 0;        int preDiff = 0;        int count = 1;        for(int i=1;i            currDiff = nums[i] - nums[i-1];            if((currDiff0 && preDiff                count++;                preDiff = currDiff;            }        }        return count;    }}    public int maxSubArray(int[] nums) {        if(nums.length==1){            return nums[0];        }        int[] dp = new int[nums.length];        dp[0] = nums[0];        int res = dp[0];        for(int i=1;i            dp[i] = Math.max(nums[i],dp[i-1]+nums[i]);            res = Math.max(res,dp[i]);        }        return res;    }}    public int maxProfit(int[] prices) {        int res = 0;                for(int i=1;i            if(prices[i]prices[i-1]){                res += prices[i] - prices[i-1];            }        }        return res;    }}    public boolean canJump(int[] nums) {        int coverRange = 0;        for(int i=0;i            coverRange = Math.max(coverRange,i+nums[i]);        }        return coverRange=nums.length-1 ? true : false;    }}    public int jump(int[] nums) {        int currCoverRange = 0;        int nextCoverRange = 0;        int res = 0;        for(int i=0;i            nextCoverRange = Math.max(nextCoverRange,i+nums[i]);            if(i==currCoverRange){                res++;                currCoverRange = nextCoverRange;            }        }        return res;    }}    public int largestSumAfterKNegations(int[] nums, int k) {        nums = IntStream.of(nums).boxed()                        .sorted((o1,o2)-Math.abs(o1)-Math.abs(o2))                        .mapToInt(Integer::intValue).toArray();        int len = nums.length;        for(int i=len-1;i=0;i--){            if(nums[i]                nums[i] = -nums[i];                k--;            }        }        if(k%2==1){            nums[0] = -nums[0];        }        int res = 0;        for(int num : nums){            res += num;        }        return res;    }}    public int canCompleteCircuit(int[] gas, int[] cost) {        int currSum = 0;        int totalSum = 0;        int index = 0;        for(int i=0;i            currSum += gas[i] - cost[i];            totalSum += gas[i] - cost[i];            if(currSum                currSum = 0;                index = (i+1) % gas.length;            }        }        return totalSum    public int candy(int[] ratings) {        int len = ratings.length;        int[] arr = new int[len];        arr[0] = 1;        for(int i=1;i            arr[i] = (ratings[i]ratings[i-1]) ? arr[i-1]+1 : 1;        }        for(int i=len-2;i=0;i--){            if(ratings[i]ratings[i+1]){                arr[i] = Math.max(arr[i],arr[i+1]+1);            }        }        int res = 0;        for(int num : arr){            res += num;        }        return res;    }}    public boolean lemonadeChange(int[] bills) {        int five = 0;        int ten = 0;        for(int i=0;i            if(bills[i]==5){                five++;            }else if(bills[i]==10){                ten++;                five--;            }else{                if(ten0){                    ten--;                    five--;                }else{                    five -= 3;                }            }            if(five                return false;            }        }        return true;    }}    public int[][] reconstructQueue(int[][] people) {        Arrays.sort(people,(p1,p2)-{            if(p1[0]==p2[0]){                return p1[1] - p2[1];            }            return p2[0] - p1[0];        });        List            list.add(num[1],num);        }        return list.toArray(new int[people.length][]);    }}

上一篇:Acrobat安装全攻略,轻松下载与安装指南

下一篇:怎么给网页加图片?