当前位置:网站首页>Violence recursion to dynamic programming 02 (very clever game of CARDS)
Violence recursion to dynamic programming 02 (very clever game of CARDS)
2022-07-29 14:24:00 【Taotao can't learn English】
1.题目
What is super smart,such as assignments 50 100 20 10 ,Extremely smart people choose first hand10,Then it doesn't matter which one you choose,This first mover can get it100,Finally get the maximum sum,成为赢家
2. 暴力递归
递归终止条件,If only the last card is left,Take it first,If it is backhand,则为0,Because it was taken first
每次会从 最左边 和 Maximum backhand 之和 与 最右边 和 Maximum backhand 之和 取较大值
After taking it first,The backhand becomes the forehand,The current value of the second hand is the smaller value left by the previous hand.
public int cardsInLine(int[] arr) {
//如果无值
if (arr == null || arr.length == 0) {
return -1;
}
//Returns the winner's score
return Math.max(first(arr, 0, arr.length - 1), second(arr, 0, arr.length - 1));
}
//先手
private int first(int[] arr, int left, int right) {
//If only the last card is left,Take it first,No backhand
if (left == right) {
return arr[left];
}
//The first player takes the maximum value from the cards 50 100 20 10
//If you take it first50,Only get it the second time10 60
int leftCard = arr[left] + second(arr, left + 1, right);
//If you take it first10,Only get it the second time100 110
int rightCard = arr[right] + second(arr, left, right - 1);
//Returns the maximum value that can be selected by the first mover
return Math.max(leftCard, rightCard);
}
//后手
public int second(int[] arr, int left, int right) {
//No cards in the back
if (left == right) {
return 0;
}
//No need to add back arr[left] arr[right] 他没得选择
//At this time, the back hand became the first hand
int leftCard = first(arr, left + 1, right);
int rightCard = first(arr, left, right - 1);
//这个返回值,It's for the first mover,Because the current backhand will leave you a small one
return Math.min(leftCard, rightCard);
}
3. 动态规划
public int cardsInLine2(int[] arr) {
//如果无值
if (arr == null || arr.length == 0) {
return -1;
}
int n = arr.length;
//Starter array
int[][] firstArr = new int[n][n];
//backhand array
int[][] secondArr = new int[n][n];
//Initialized to when no value is filled-1
for (int i = 0; i <n ; i++) {
for (int j = 0; j <n ; j++) {
firstArr[i][j] = -1;
secondArr[i][j] = -1;
}
}
//Returns the winner's score
return Math.max(first2(arr, 0, arr.length - 1, firstArr, secondArr), second2(arr, 0, arr.length - 1, firstArr, secondArr));
}
private int first2(int[] arr, int left, int right, int[][] firstArr, int[][] secondArr) {
//If the element at this position has already been calculated,Returns the current element directly
if (firstArr[left][right] != -1) {
return firstArr[left][right];
}
//If there is only one card
if (left == right) {
firstArr[left][right] = arr[left];
return firstArr[left][right];
}
//否则计算
int getLeft = arr[left] + second2(arr, left + 1, right, firstArr, secondArr);
int getRight = arr[right] + second2(arr, left, right - 1, firstArr, secondArr);
firstArr[left][right] = Math.max(getLeft, getRight);
return firstArr[left][right];
}
private int second2(int[] arr, int left, int right, int[][] firstArr, int[][] secondArr) {
if (secondArr[left][right] != -1) {
return secondArr[left][right];
}
//If there is only one card
if (left == right) {
secondArr[left][right] = 0;
return secondArr[left][right];
}
//The backhand is now the first mover,取值
int getLeft =first2(arr, left+1, right, firstArr, secondArr);
int getRight = first2(arr, left, right-1, firstArr, secondArr);
secondArr[left][right] = Math.min(getLeft, getRight);
return secondArr[left][right];
}
4. 动态规划优化
public int cardsInLine3(int[] arr) {
//如果无值
if (arr == null || arr.length == 0) {
return -1;
}
int n = arr.length;
//Starter array
int[][] firstArr = new int[n][n];
//backhand array
int[][] secondArr = new int[n][n];
//The choice when initializing the diagonal element of the first hand array to the last card,The backhand group is 0
for (int i = 0; i <n ; i++) {
firstArr[i][i] = arr[i];
}
// L为横,R为纵
//The two 2D arrays use only the upper right corner,因为 L<=RHorizontally established
// firstArr[0][0]已经赋值过了
for (int i = 1; i < n; i++) {
int left = 0;
int right = i;
while (right < n) {
firstArr[left][right] = Math.max(arr[left] + secondArr[left + 1][right], arr[right] + secondArr[left][right - 1]);
secondArr[left][right] = Math.min(firstArr[left + 1][right], firstArr[left][right - 1]);
left++;
right++;
}
}
//Returns the winner's score
return Math.max(firstArr[0][n-1],secondArr[0][n-1]);
}
public static void main(String[] args) {
System.out.println(new CardsInLine().cardsInLine(new int[]{
5, 7, 4, 5, 8, 1, 6, 0, 3, 4, 6, 1, 7}));
System.out.println(new CardsInLine().cardsInLine2(new int[]{
5, 7, 4, 5, 8, 1, 6, 0, 3, 4, 6, 1, 7}));
System.out.println(new CardsInLine().cardsInLine3(new int[]{
5, 7, 4, 5, 8, 1, 6, 0, 3, 4, 6, 1, 7}));
}
边栏推荐
猜你喜欢
【论文阅读】Anomaly Detection in Video via Self-Supervised and Multi-Task Learning
BOM系列之Location对象
TCP和UDP的基本认识
C#实现线程管理类
EA&UML日拱一卒-活动图::Feature和StuctualFeature
力扣 206.反转链表--递归解决
The Advanced Guide to the Computer Professional Interview
教育部等五部门联合推荐优质课外资源,腾讯产品青少年模式首发
【pytorch】1.6 tensor 基本运算
【Postman】下载与安装(新手图文教程)
随机推荐
AI全流程开发难题破解之钥
480-82(59、151)
十种实现延迟任务的方案
关于知识付费的一些思考
企业需要知道的5个 IAM 最佳实践
EA&UML日拱一卒-活动图::Variable Actions(续)
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
深开鸿:万物智联的大江上,升起一轮开源鸿蒙月
【Postman】下载与安装(新手图文教程)
这么多年了,还搞不懂正则语法?
威纶通触摸屏制作自定义欢迎界面的几种方法介绍
少儿编程 电子学会图形化编程等级考试Scratch二级真题解析(选择题)2022年6月
plsql连接oracle使用完毕之后关闭问题
线上支付,出款和收款
gdb调试常用概念整理
TCP和UDP的基本认识
Some thoughts on paying for knowledge
【FreeSwitch开发实践】自定义模块创建与使用
全开放式耳机怎么样?不塞耳朵的蓝牙耳机推荐
马尔可夫跳变线性系统最优控制的研究现状与进展