1423. 可获得的最大点数 --- 滑动窗口+简单建模

1423. 可获得的最大点数

https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/

package com.walegarrett;

/**
 * @Author WaleGarrett
 * @Date 2020/12/3 20:10
 */

/**
 * 1423. 可获得的最大点数
 * 题目链接:https://leetcode-cn.com/problems/maximum-points-you-can-obtain-from-cards/
 */
public class Maximum_Points_You_Can_Obtain_From_Cards {
    public int maxScore(int[] cardPoints, int k) {
        int sum=0, temp = 0;
        for(int num : cardPoints){
            sum+=num;
        }
        int len=cardPoints.length;
        int mins = Integer.MAX_VALUE;
        //窗口的大小,将该窗口不断向右滑动,始终维护该窗口中的总和最小
        int windowSize = len - k;
        for(int i=0; i<cardPoints.length; i++){
            temp += cardPoints[i];
            if(i >= windowSize){
                temp -= cardPoints[i - windowSize];
            }
            if(i >= windowSize - 1){
                mins = Math.min(mins, temp);
            }
        }
        return sum - mins;
    }
}

posted @ 2020-12-03 20:42  Garrett_Wale  阅读(76)  评论(0编辑  收藏  举报