leetcode 1406
简介
国服第一的刷题视频
参考链接
https://www.bilibili.com/video/BV1W54y197NM?from=search&seid=16875469481128889064
思路
主要用了倒序的思想,因为正序对于求解的不确定性太多了
定义f[n] 表示在第n次开始拿先手可以比后手多拿多少分
DP
倒推
代码
string stoneGameIII(vector<int>& a) {
int n = a.size();
// 主要用了倒序的思想,因为正序对于求解的不法确定性太多了
// 定义f[n] 表示在第n次开始拿先手可以比后手多拿多少分
vector<int> f;
f.resize(n+1);
f[n] = 0; // 原本长度是n-1的长度,扩充一个的长度表示在最最开始先手和后手持平当然你也可以
// 扩充三堆的样子,也就是后面判断条件的该改变
for(int i=n-1; i>=0; i--){
f[i] = -1e9;
int s = 0;
for(int j = i; j<(i+3) && j < n; j++){
s += a[j];
f[i] = max(f[i], s - f[j+1]);// 表示 选取1,2,3和前一次先手最好的比较。即这一次先手的最优值
}
}
if(f[0] > 0){
return "Alice";
}else if(f[0] == 0){
return "Tie";
}else{
return "Bob";
}
}
leetcode 1406的更多相关文章
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- HL7消息编辑器的使用手册
REDISANT 提供互联网与物联网开发测试套件 # 互联网与中间件: Redis Assistant ZooKeeper Assistant Kafka Assistant RocketMQ Ass ...
- MySQL 三层 B+ 树能存多少数据?
MySQL 三层 B+ 树能存多少数据? MySQL 的 B+ 树索引结构广泛用于存储引擎(如 InnoDB)中,用于实现高效的查询.B+ 树的高度和每一层的节点数量直接影响其存储能力.以下是计算三层 ...
- freeswitch配置视频对接
概述 freeswitch是一款简单好用的VOIP开源软交换平台. 随着4G/5G网络的完善,视频呼叫的需求慢慢变多,本文介绍使用fs对接视频线路的配置方案. 环境 CentOS 7.9 freesw ...
- StarBlog和Masuit.MyBlogs博客程序学习使用日记(二)
使用中发现管理平台的博客列表无法查看,发现是echarts.min.js引用路径无效,找到route.config.js之后找到对应的菜单配置项修改对应的路径为https://cdn.staticfi ...
- 使用C#构建一个同时问多个LLM并总结的小工具
前言 在AI编程时代,如果自己能够知道一些可行的解决方案,那么描述清楚交给AI,可以有很大的帮助. 但是我们往往不知道真正可行的解决方案是什么? 我自己有过这样的经历,遇到一个需求,我不知道有哪些解决 ...
- 莫想到有一天得重新写个 etcd client
莫想到有一天得重新写个 etcd client 其实8年前搞过一个, 不过经过8年时间,etcd 多了很多功能 ,原来的多半不行了 虽然暂时我也没啥需求,但是怕kv和watch有变化 而且其实通过 g ...
- TVM Pass优化 -- 移除无用函数(Remove Unused Function)
定义 移除无用函数,Remove Unused Function,顾名思义,就是删除Module中定义但未用到的函数 当然,它也是一个模块级的优化, 举例子: def get_mod(): mod = ...
- C#实现MergeSort算法
public class MergeSortLearn { /// <summary> /// 分治递归 /// </summary> /// <param name=& ...
- Docker不装C盘
Docker默认安装在C盘,这未来随着docker使用必定会导致C盘空间吃紧.所以本文提前进行空间布局,将docker默认安装路径软链接到D盘.软链接D盘Docker默认安装路径为C:\Program ...
- 来个好玩的,用手机随时随地指挥你的 Cursor!
告别束缚,用手机随时随地指挥你的 Cursor! 嘿,各位 Cursor 的忠实用户和效率达人们! 你是否曾经遇到过这样的场景:人不在Mac旁边,却突然灵感迸发,想要让 Cursor 帮你写点代码.整 ...