代码随想录-算法训练营day33(贪心算法03:K次取反后最大化的数组和,加油站,分发糖果)

第八章 贪心算法 part03
 
● 1005.K次取反后最大化的数组和 
● 134. 加油站
● 135. 分发糖果  
 
 详细布置 
 
 1005.K次取反后最大化的数组和  
本题简单一些,估计大家不用想着贪心 ,用自己直觉也会有思路。 
https://programmercarl.com/1005.K%E6%AC%A1%E5%8F%96%E5%8F%8D%E5%90%8E%E6%9C%80%E5%A4%A7%E5%8C%96%E7%9A%84%E6%95%B0%E7%BB%84%E5%92%8C.html  
 
 
 134. 加油站 
本题有点难度,不太好想,推荐大家熟悉一下方法二 
https://programmercarl.com/0134.%E5%8A%A0%E6%B2%B9%E7%AB%99.html  
 
 
 135. 分发糖果 
本题涉及到一个思想,就是想处理好一边再处理另一边,不要两边想着一起兼顾,后面还会有题目用到这个思路 
https://programmercarl.com/0135.%E5%88%86%E5%8F%91%E7%B3%96%E6%9E%9C.html  
往日任务
● day 1 任务以及具体安排:https://docs.qq.com/doc/DUG9UR2ZUc3BjRUdY  
● day 2 任务以及具体安排:https://docs.qq.com/doc/DUGRwWXNOVEpyaVpG  
● day 3 任务以及具体安排:https://docs.qq.com/doc/DUGdqYWNYeGhlaVR6 
● day 4 任务以及具体安排:https://docs.qq.com/doc/DUFNjYUxYRHRVWklp 
● day 5 周日休息
● day 6 任务以及具体安排:https://docs.qq.com/doc/DUEtFSGdreWRuR2p4 
● day 7 任务以及具体安排:https://docs.qq.com/doc/DUElCb1NyTVpXa0Jj 
● day 8 任务以及具体安排:https://docs.qq.com/doc/DUGdsY2JFaFhDRVZH 
● day 9 任务以及具体安排:https://docs.qq.com/doc/DUHVXSnZNaXpVUHN4 
● day 10 任务以及具体安排:https://docs.qq.com/doc/DUElqeHh3cndDbW1Q 
●day 11 任务以及具体安排:https://docs.qq.com/doc/DUHh6UE5hUUZOZUd0 
●day 12 周日休息 
●day 13 任务以及具体安排:https://docs.qq.com/doc/DUHNpa3F4b2dMUWJ3 
●day 14 任务以及具体安排:https://docs.qq.com/doc/DUHRtdXZZSWFkeGdE 
●day 15 任务以及具体安排:https://docs.qq.com/doc/DUHN0ZVJuRmVYeWNv 
●day 16 任务以及具体安排:https://docs.qq.com/doc/DUHBQRm1aSWR4T2NK 
●day 17 任务以及具体安排:https://docs.qq.com/doc/DUFpXY3hBZkpabWFY 
●day 18 任务以及具体安排:https://docs.qq.com/doc/DUFFiVHl3YVlReVlr 
●day 19 周日休息
●day 20 任务以及具体安排:https://docs.qq.com/doc/DUGFRU2V6Z1F4alBH  
●day 21 任务以及具体安排:https://docs.qq.com/doc/DUHl2SGNvZmxqZm1X 
●day 22 任务以及具体安排:https://docs.qq.com/doc/DUHplVUp5YnN1bnBL  
●day 23 任务以及具体安排:https://docs.qq.com/doc/DUFBUQmxpQU1pa29C 
●day 24 任务以及具体安排:https://docs.qq.com/doc/DUEhsb0pUUm1WT2NP  
●day 25 任务以及具体安排:https://docs.qq.com/doc/DUExTYXVzU1BiU2Zl 
●day 26 休息 
●day 27 任务以及具体安排:https://docs.qq.com/doc/DUElpbnNUR3hIbXlY 
●day 28 任务以及具体安排:https://docs.qq.com/doc/DUG1yVHdlWEdNYlhZ  
●day 29 任务以及具体安排:https://docs.qq.com/doc/DUHZYbWhwSHRCRmp3 
●day 30 任务以及具体安排:https://docs.qq.com/doc/DUEdTVVhxbnJiY3BR 
●day 31 任务以及具体安排:https://docs.qq.com/doc/DUG1PQ1ZZY2xXY1ly 
●day 32 任务以及具体安排:https://docs.qq.com/doc/DUGFEdGFWeVhleFF1 
●day 33 周日休息

day33

k次取反后最大化的数组和

 class Solution {
     public int largestSumAfterKNegations(int[] nums, int k) {
         nums = IntStream.of(nums)
             .boxed()
             .sorted((o1,o2) -> Math.abs(o2) - Math.abs(o1))
             .mapToInt(Integer::intValue)
             .toArray();
             //按绝对值大小排序
         for( int i = 0; i < nums.length ;i++){
             if(nums[i] < 0 && k > 0){
                 nums[i] = -nums[i];
                 k--;
             }
             if(k == 0) break;   
         }
         if( k > 0 && k % 2 != 0){
                 nums[ nums.length - 1] = -nums[ nums.length - 1];
             } 
         int result = 0;
         for(int n : nums){
             result += n;
         }
         return result;
     }
 }

加油站

 class Solution {
     public int canCompleteCircuit(int[] gas, int[] cost) {
         int cursum = 0;
         int totalsum = 0;
         int start = 0;
         for(int i = 0; i < gas.length; i++){
             cursum += gas[i] - cost[i];
             totalsum += gas[i] - cost[i];
             if( cursum < 0){
                 start = i + 1;
                 cursum = 0;
             }
         }
         if( totalsum < 0 ) return -1;
         return start;
     }
 }

分发糖果

 class Solution {
     public int candy(int[] ratings) {
         int[] candy = new int[ratings.length];
         candy[0] = 1;
         for (int i = 1; i < ratings.length; i++) {
             candy[i] = (ratings[i] > ratings[i - 1]) ? candy[i - 1] + 1 : 1;
         }
         for( int i = ratings.length - 2; i >=0; i--){
             if(ratings[i] > ratings[i + 1]){
                 candy[i] = Math.max(candy[i+1] + 1 , candy[i]);
             }
         }
         int result = 0;
         for(int n : candy){
             result += n;
         }
         return result;
     }
 }

感谢大佬分享:

代码随想录-算法训练营day33【贪心算法03:K次取反后最大化的数组和、加油站、分发糖果】-CSDN博客

你可能感兴趣的:(算法训练营,算法,贪心算法,leetcode,java,力扣)