第十周:[Leetcode]486. Predict the Winner

Camera 基础知识 Raw Sensor输出的Data格式为Raw,图像的效果处理使用BB端的ISP,BB端接收Raw data后进行一系列的图像处理(OB,Shading,AWB,Gamma,EE,ANR等),效果方面由BB端控制,需要针对不同的模组进行效果调试,Raw sensor是目前的主流,数据量比YUV Sensor小(RAW10 格式的sensor 1个pixel 10个bit)使用平台ISP处理,能支持较大的size。不过,相位对焦并不是那么完美,它对光线的要求就比较高,在弱光拍摄环境下,相位对焦就无能威力了。 阅读详情

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.
Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.
Example 1:
Input: [1, 5, 2]
Output: False
Explanation: Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return False.
Example 2:
Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.


解法一:直接模拟。每次两个选手都从可以选的数组两端挑选一个数字(不是最先的就是最后的一个),直接模拟这样一个过程,选手一挑完,选手二挑,直到最后看哪名选手的总和高,使用递归来实现整个模拟过程。


class Solution {
public:
    bool PredictTheWinner(vector<int>& nums) {
    return player1_win(nums, 0, nums.size() - 1, 0, 0);
}

bool player1_win(vector<int>& nums,int start,int end,int sum1,int sum2){
    if(start > end)
        return sum1 >= sum2;
    bool pick_start_player2_win = player2_win(nums,start + 1,end,sum1 + nums[start],sum2);
    bool pick_end_player2_win = player2_win(nums,start,end - 1,sum1 + nums[end],sum2);
    return (!pick_start_player2_win || !pick_end_player2_win);
}

bool player2_win(vector<int>& nums,int start,int end,int sum1, int sum2){
    if(start > end)
        return sum2 > sum1;
    bool pick_start_player1_win = player1_win(nums,start + 1,end,sum1,sum2 + nums[start]);
    bool pick_end_player1_win = player1_win(nums,start,end - 1,sum1,sum2 + nums[end]);
    return (!pick_start_player1_win || !pick_end_player1_win);
}
};

解法二:动态规划。比较最后选手一的数字总和是否大于选手二的,这一过程可以类比成,维护一个特殊变量,选手一选一个数字,在这个变量基础上加该值,选手二选一个数字,在这个变量基础上减该值,最后该变量若不为负,则选手一获胜。dp[i][j]数组中的值代表从位置i到j这段数组中,这个特殊变量的最大值。由于选手一选之前肯定是选手二选,所以dp[i][j] = 该次选的值+(-上一步选手二选完后的dp值),每次选值有两种可能,选择下标为i的第一个元素或者下标为j的最后一个元素,可得动态转移方程为:
dp[i][j] = max(nums[i]-dp[i+1][j],nums[j]-dp[i][j-1])
当i==j时,dp[i][j] = nums[i],这一步应在初始化dp数组时做。


class Solution {
public:
    bool PredictTheWinner(vector<int>& nums) {
    int dp[nums.size()][nums.size()] = {0};
    for(int i = 0;i < nums.size();i++)
        dp[i][i] = nums[i];
    for(int i = nums.size() - 2;i >= 0;i--)
        for(int j = i + 1; j < nums.size();j++)
            dp[i][j] = max(nums[i] - dp[i+1][j],nums[j] - dp[i][j-1]);
    return dp[0][nums.size() - 1] >= 0;
}
};
利用 Docker Distribution 搭建私有的 Docker Registry 1、拓扑 阅读详情

相关推荐

四轮两驱小车(四):STM32驱动5路灰度传感器PID循迹

之前买了一批5路灰度传感器,想用这传感器进行循迹,无奈网上和官方的资料提供的还是比较少,这里还是做一下当初的学习记录。

weixin_51651698的博客 6万+

LeetCode //C - 486. Predict the Winner

【代码】LeetCode //C - 486. Predict the Winner。

Made in Code 1063

ESP32S3开发实战:从Vscode+IDF环境配置到OpenOCD单步调试全流程

本文详细介绍了ESP32S3开发环境的完整搭建流程,重点讲解如何在Vscode中配置ESP-IDF开发框架,并利用OpenOCD工具实现硬件单步调试。内容涵盖从环境准备、工程创建、代码烧录到高级调试技巧的全方位实战指南,旨在帮助开发者快速掌握ESP32S3的高效开发与调试方法。

weixin_29168813的博客 218

Leetcode——486. Predict the Winner

https://leetcode.com/problems/predict-the-winner/?tab=DescriptionDescriptionGiven an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array follo

好记性不如烂笔头的专栏 3288

别再只会调速度了!用STM32的定时器中断精准控制伺服电机转角(避坑指南)

本文详细介绍了如何利用STM32的定时器中断和PWM输出实现伺服电机的精准转角控制,避免常见的速度控制误区。通过硬件配置、脉冲计数逻辑和优化技巧的实战讲解,帮助开发者掌握高精度位置控制方法,适用于工业自动化和机器人控制等场景。

weixin_42531163的博客 425

IEDA激活码失效?教你5分钟快速续期的方法

本文针对IEDA激活码失效问题,提供了一套从快速应急到深度排查的完整解决方案。文章不仅详解了5分钟快速续期与清理缓存等应急方法,更深入剖析了授权机制、离线激活流程,并指导如何构建可持续的授权管理策略,帮助开发者彻底解决激活困扰,确保开发环境稳定。

a0b1c2d3的专栏 279

【Leetcode】486. Predict the Winner(配数学证明)

题目地址: https://leetcode.com/problems/predict-the-winner/ 给定一个非负整数数组。设计一个两人的游戏,比如甲乙两个人,甲可以从数组左右两端点取一个分数,然后乙接着拿,直到拿完为止。如果甲总分高于等于乙,就返回true,否则返回false。甲是先手方。 思路是这样的:如果只剩两个数字了,那么结论可以很轻易的得到。如果多于两个数字,那么就枚举先手方拿...

数学、算法爱好者的博客 3471

vit的cam和注意力图: VIT模型的可解释性

VIT的热力图怎么画

YI_SHU_JIA的博客 1万+

LeetCode 486. Predict the Winner

Predict the Winner - LeetCode Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so ...

zhaohaibo的博客 222

PVE虚拟机运行不流畅,出现卡顿。

🏆本文收录于《CSDN问答解惑-专业版》专栏,主要记录项目实战过程中的Bug之前因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!

**My Coding Family** 3076

Deepseek Engram 论文全面解读

以下是对 DeepSeek 与北京大学于联合发布的论文《》(简称Engram 论文)的。

lpfasd123的博客 372

[LeetCode]486. Predict the Winner

https://leetcode.com/problems/predict-the-winner/ 给一个int数组,两个人依次从数组任意一头拿一个数,判断player1最后拿到的总数是否更大 两人依次拿,如果P1赢,则P1拿的>P2拿的。我们把P1拿的视为“+”,把P2拿的视为“-”,如果最后结果大于等于0则P1赢。 因此对于递归来说,beg ~ end的结果为max(

gqk289的专栏 3042

【即插即用模块】卷积篇 | CVPR 2024 | InceptionDWConv,可替代ViT的自注意力部分,保持性能的同时提高效率!

InceptionDWConv模块是一种高效的多分支深度卷积设计,通过将大核卷积分解为3×3方形核、1×11/11×1条带核和恒等映射四个并行分支,在保持大感受野的同时显著降低计算成本。该模块适用于需要高效大范围感知的视觉任务,可替代Transformer中的自注意力机制。实验表明,其计算复杂度优于标准卷积和深度可分离卷积,在准确率和效率上均超越Swin、ResNet和ConvNeXt等模型。模块实现为即插即用结构,输入输出维度一致,适合作为Token Mixer使用。完整代码展示了多分支特征提取和通道拼接

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 1333

LeetCode486. Predict the Winner

两端选择总分最大获胜

grllery的博客 978

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

【LeetCode】486. Predict the Winner 解题报告(Python)

【LeetCode】486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/predict-the-w...

负雪明烛 2302

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

LeetCode笔记:486. Predict the Winner

两个玩家轮流取分数,谁总分更高?

Minecraft 3026

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

[leetcode] 486. Predict the Winner

Description Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a pl...

Learning from the mistakes 347

[leetcode]486. Predict the Winner

[leetcode]486. Predict the Winner Analysis 唉—— [每天刷题并不难0.0] Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the ...

weixin_32135877的博客 239

LeetCode----486. Predict the Winner 动态规划

最近在整理课本第六章动态规划的课后习题,就顺便找leetcode上的题目做啦,顺便复习。。 1 题目 Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player

xu_xyy的博客 1341

LeetCode 486 Predict the Winner 题解

题目简述:给定非负数组nums。有两个玩家,从玩家1开始交替取出一个数并加到他的分数上,取的时候只能取数组剩余的头尾两个数之一。当所有数都被取完时分数高的玩家获胜。求在给定nums的情况下,预测玩家1是否获胜。假设两个玩家每次操作都是最优的。 输入:非负数组nums。 输出:一个bool值,表示玩家1是否获胜。 示例:对于数组[1,5,2],玩家1必输,因为无论取1还是2,玩家2都会取5而获

MonoTali的博客 934

【leetcode】486. Predict the Winner

题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a playe...

weixin_33752045的博客 116

leetcode 486. Predict the Winner

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks

淡然坊 427

Leetcode 486. Predict the Winner 预测赢家 解题报告

1 解题思想开学了,我又回归了。 这道题的意思是给了一个数组,两个人玩,每个人可以在当前的数组的头部或者尾部选择一个数,作为自己的分数,然后换人,已经被选过的数字不能再用。 现在有两个人玩,Player1和Player2,问给了当前的数组后,Player1能不能保证胜利?解题方法,就是需要做一个决策,看Player1有没有一个必胜的选择决策,这个只需要递归下去选就好。 所谓的必胜,就是当前选到

MebiuW的专栏 4177

LeetCode486. Predict the Winner题解

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on.

SYSU_LBY的博客 740

[Leetcode]486. Predict the Winner

486. Predict the Winner 原题目链接 中文释义 题解 此题目即为Leetcode 877题的一般情况,通过那篇文章的解法2即可解决此题,链接如下。 Leetcode 877 StoneGame

Blurryface_的博客 310

486. Predict the Winner | 486. 预测赢家(博弈论)

题目 https://leetcode.com/problems/predict-the-winner/ 题解 这道题和 leetcode 877. Stone Game | 877. 石子游戏(递归/动态规划/数学解法) 比较像。一开始以为又是一个恒等问题,后来看了答案发现并不是。而且从数据规模上来看,这题用暴力是能通过的。 class Solution { public boolean PredictTheWinner(int[] nums) { if (nums.length

寒泉 4万+
上一篇: 第九周:[Leetcode]494. Target Sum
下一篇: 第十一周:[Leetcode]474. Ones and Zeroes
line02
博客等级 码龄15年 3粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值