码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)


    There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints.

    In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.

    Your score is the sum of the points of the cards you have taken.

    Given the integer array cardPoints and the integer k, return the maximum score you can obtain.

    Example 1:

    Input: cardPoints = [1,2,3,4,5,6,1], k = 3
    Output: 12
    Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
    Example 2:

    Input: cardPoints = [2,2,2], k = 2
    Output: 4
    Explanation: Regardless of which two cards you take, your score will always be 4.

    给一组牌,每次只能从头或尾抽一张,一共要抽k张,
    把抽到的牌的点数加起来,问能达到的最大点数和是多少。

    思路:

    容易被抽牌顺序这个概念迷惑,比如是先抽头还是先抽尾呢,
    其实可以忽略抽牌顺序这个概念,直接就想:头抽几张,尾抽几张。因为总有顺序能实现。

    比如我想要头2张,尾一张,可以通过先头再尾再头这样的顺序实现。
    头1张,尾两张,可通过尾,头,尾这样的顺序实现。

    于是问题变成了数组头部取几个数字,尾部取几个数字,才能使和最大。

    那么用滑动窗口的方法,首先取数组头部k个数字,这是一个窗口,然后依次从窗口右边去掉一个数字,换作数组尾部的一个数字,
    直到数组头部0个数字,尾部k个数字为止。
    记录下这个过程中最大的和。
    在这里插入图片描述

    public int maxScore(int[] cardPoints, int k) {
        int res = 0;
        int i = 0;
        int sum = 0;
        int n = cardPoints.length;
        
        while(i < k) {
            sum += cardPoints[i];
            i ++;
        }
        
        i = k - 1;
        res = sum;
        
        while(i >= 0) {
            sum = sum - cardPoints[i] + cardPoints[n - k + i];
            res = Math.max(res, sum);
            i --;
        }
        return res;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    参考链接

  • 相关阅读:
    【LeetCode-中等】128. 最长连续序列(详解)
    互联网摸鱼日报(2023-11-20)
    vulnhub靶场之VIKINGS: 1
    最新ChatGPT网站源码+支持GPT4.0+支持Midjourney绘画+支持国内全AI模型
    Okhttp连接泄漏警告问题分析
    前端框架 Nextjs 实现React SEO优化
    爱上源码,重学Spring MVC深入
    黑帽python第二版(Black Hat Python 2nd Edition)读书笔记 之 第十章 Windows提权(3)代码注入
    基于 OPLG 从 0 到 1 构建统一可观测平台实践
    《Relation of the Relations A New Paradigm of the Relation Extraction Problem》论文阅读笔记
  • 原文地址:https://blog.csdn.net/level_code/article/details/125488126
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号