leetcode -- 贪心

题目1  .汽车加油

https://leetcode.com/problems/gas-station/

题目描述,gas数组为每个站可以加油的量,cost数组为每个站到下一站的耗费。

题解: curGas += gas[i] -cost[i]   统计目前还剩油量,如果小于0则表示在i之前的所有站点都不可能是起点(因为累加到现在有负了),start = i+1,curGas清0.  本题思路中一个重要的点是每次只统计了上一个start到i之间的curGas是否大于0,那上个start之前的加油站怎么知道能不能走?  通过  总量来判断,当总加油量大于总耗油量时总存在一个方法能通过所有加油站, curGas>0 则保证了找到一个能走到最后的路径。

class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
       int N = gas.length;
       int curGas=0;
       int balance = 0;
       int start = 0;
       for(int i=0;i<N;i++){
           curGas += gas[i] - cost[i] ;
           if(curGas <0){
               curGas = 0;
               start = (i+1)%N;
           }
           balance += gas[i] - cost[i] ;
       }
        return balance >=0 ? start :-1;
    }
}

 

题目2 . 分糖果问题

https://leetcode.com/problems/candy/

题目描述: 小朋友分糖果,每个小朋友都有一个分数,分数大的小朋友要比临近的小朋友糖果多,且每个小朋友至少一个糖果。

题解: ①先给每个小朋友一个糖果,②然后从左到右 比左边大的小朋友糖果数等于左边+1  ,③从右到左 比右边大的小朋友糖果等于  max(自身,右边+1)

class Solution {
    public int candy(int[] ratings) {
        
        int[] candys = new int[ratings.length];
        
        Arrays.fill(candys,1);
        
        for(int i=1;i<ratings.length;i++){
            if(ratings[i]>ratings[i-1]){
                candys[i] = candys[i-1]+1;
            }
        }
        
        for(int i= ratings.length-2;i>=0;i--){
            if(ratings[i] > ratings[i+1]){
                candys[i] = Math.max(candys[i],candys[i+1]+1);
            }
        }
        int count = 0;
        for(int i=0;i<candys.length;i++){
            count += candys[i];
        }
        return count;
    }
}

题目3. 修补数组  (前缀和问题)

https://leetcode.com/problems/patching-array/

题目描述: 增加元素使得数组中的元素通过组合可以得到    n  以内的所有数字【1-n】.数组为有序;

题解:前缀和问题,用一个变量记录此时能达到的 和  pre_max ,遍历数组,如果下一个数A[i] 大于前缀和,证明在A[i-1] 和A[i]之间需要补充其它数字,需要补充的数字应该为   pre_max+1 ,因为前面的数字已经保证了最大能拼凑出pre_max, 故下一个数字必须要为pre_max +1, 这样以后前缀和就更新为 pre_max += pre_max+1. 

class Solution {
    public int minPatches(int[] nums, int n) {
       if(n <= 0 )return 0;
       long pre_max = 0;
        int ans = 0;
        
        for(int i=0;i<nums.length ;i++){
            int e = nums[i];
            if(e >n || pre_max > n)break; 
            // 当e比n都大的时候无意义,pre_max比n大的时候早就达到要求了
            
            while(pre_max+1 <e ){
                ans++;
                pre_max += pre_max +1 ;
            }
            pre_max += e;
        }
        
        if(pre_max <n){
            while(pre_max < n){
                ans++;
                pre_max += pre_max +1;
            }
        }
        return ans;
    }
    
}

题目4. 柱子盛水最多

https://leetcode.com/problems/trapping-rain-water/

题目描述: 给一个数组,求数组中间盛水多少。

题解:左右两边初始化为各自最大, 找相对小的那一边,将这边的下一个和这边的最大值求差,就可得到该柱子能盛水的量。

class Solution {
    public int trap(int[] height) {
       if(height == null || height.length <2)return 0;
        
        int ans = 0;
        int i = 0,j = height.length-1;
        int lmax = height[i],rmax = height[j];
        while(i<j){
            if(lmax >rmax){
                rmax = Math.max(rmax,height[j-1]);//看最大值和左边的一个哪个大
                ans += rmax - Math.min(rmax,height[--j]); //看左边的这个值和rmax差了多少
            }else{
                lmax = Math.max(lmax,height[i+1]);
                ans += lmax - Math.min(lmax,height[++i]);
            }
        }
        return ans;
        
    }
}

题目5. 找出3个数之和为0

https://leetcode.com/problems/3sum/

题目简介:给一个数组,列出所有可能的 和为0的3个数字。

题解:先来一个大循环,每次都把一个数字作为第一项,  找后两项数字 ,left = nums[i+1],right = nums[length-1] ,left和right逐渐向中间靠拢,若三者相加等于0,记录下来,并看最左和最右有没有相等的。

class Solution {

     public List<List<Integer>> threeSum(int[] nums) {
         List<List<Integer>> totlelist= new ArrayList<>();
         Arrays.sort(nums); //先排个序
         
         for(int i=0;i<nums.length;i++){
             if(i >0 && nums[i] == nums[i-1])continue;   //作为开头的肯定不能有重复
             int low = i+1,high = nums.length-1,sum = 0-nums[i];
             
             while(low < high){
                 if(low < high &&(nums[low] + nums[i] + nums[high] == 0)){

                     totlelist.add(Arrays.asList(nums[i],nums[low],nums[high]));
                     while(low < high &&(nums[low] == nums[low+1]))low++;
                     while(low < high && (nums[high] == nums[high-1]))high--;
                     low++;
                     high--;
                 }else if(nums[low] + nums[i] + nums[high] <0){
                     low++;
                 }else{
                     high--;
                 }
                
             }
         }
         return totlelist;
         
     }
}

升级版  4sum

https://leetcode.com/problems/4sum/

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
       
        Arrays.sort(nums);
       
        return  ksum(nums,4,0,target); // 输入n代表nsum
    }
    
    List<List<Integer>> ksum(int[] nums,int k,int n,int target){
        List<List<Integer>> totleList = new ArrayList<List<Integer>>();
        if(k == 2){
            int low = n,high = nums.length-1;
            while(low <high){
                if(nums[low] + nums[high] == target){
                    totleList.add(Arrays.asList(nums[low],nums[high]));
                    while(low < high && nums[low] == nums[low+1])low++;
                    while(low <high && nums[high] == nums[high-1])high--;
                    low++;
                    high--;
                }else if(nums[low] + nums[high] <target){
                    low ++;
                }else{
                    high--;
                }
            }
        }else{
           
            for(int i=n;i<nums.length - k +1;i++){
                if(i >n && nums[i] == nums[i-1] )continue;
               
                 List<List<Integer>> temp =  ksum(nums,k-1,i+1,target-nums[i]);
                 if(temp != null)
                 for(List<Integer> t:temp){
                    List<Integer> L= new ArrayList<Integer>(t);
                    L.add(0,nums[i]);
                    totleList.add(L);
                 }
               

            }
        }
        return totleList;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值