Java实现 LeetCode 546 移除盒子(递归,vivo秋招)
546. 移除盒子
给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色。
你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止。每一轮你可以移除具有相同颜色的连续 k 个盒子(k >= 1),这样一轮之后你将得到 k*k 个积分。
当你将所有盒子都去掉之后,求你能获得的最大积分和。
示例 1:
输入:
[1, 3, 2, 2, 2, 3, 4, 3, 1]
输出:
23
解释:
[1, 3, 2, 2, 2, 3, 4, 3, 1]
----> [1, 3, 3, 4, 3, 1] (3*3=9 分)
----> [1, 3, 3, 3, 1] (1*1=1 分)
----> [1, 1] (3*3=9 分)
----> [] (2*2=4 分)
提示:盒子的总数 n 不会超过 100。
PS:
网友大哥:2019.9.11 vivo秋招, 数据分析, 第三题原题, 多么痛的领悟
class Solution {
public int removeBoxes(int[] boxes) {
int[][][] scores = new int[boxes.length][boxes.length][boxes.length];
return calcMaxScore(boxes, 0, boxes.length - 1, 0, scores);
}
private int calcMaxScore(int[] boxes, int left, int right, int sameCount, int[][][] scores) {
if (left > right) {
return 0;
}
int maxScore = scores[left][right][sameCount];
if (maxScore != 0) {
return maxScore;
}
int pos = left - 1;
for (int i = right - 1; i >= left; --i) {
if (boxes[i] != boxes[right]) {
pos = i;
break;
}
}
int mergeSameCount = sameCount + right - pos;
maxScore = calcMaxScore(boxes, left, pos, 0, scores) + mergeSameCount * mergeSameCount;
for (int i = pos - 1; i >= left; --i) {
if (boxes[i] == boxes[right] && boxes[i + 1] != boxes[right]) {
int score = calcMaxScore(boxes, left, i, mergeSameCount, scores) + calcMaxScore(boxes, i + 1, pos, 0, scores);
maxScore = Math.max(maxScore, score);
}
}
scores[left][right][sameCount] = maxScore;
return maxScore;
}
}
Java实现 LeetCode 546 移除盒子(递归,vivo秋招)的更多相关文章
- Leetcode 546.移除盒子
移除盒子 给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色.你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止.每一轮你可以移除具有相同颜色的连续 k 个盒子(k > ...
- Java实现 LeetCode 27 移除元素
27. 移除元素 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额 ...
- Java实现 LeetCode 402 移掉K位数字
402. 移掉K位数字 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零. 示 ...
- Java实现 LeetCode 203 移除链表元素
203. 移除链表元素 删除链表中等于给定值 val 的所有节点. 示例: 输入: 1->2->6->3->4->5->6, val = 6 输出: 1->2 ...
- Java实现 LeetCode 753 破解保险箱(递归)
753. 破解保险箱 有一个需要密码才能打开的保险箱.密码是 n 位数, 密码的每一位是 k 位序列 0, 1, -, k-1 中的一个 . 你可以随意输入密码,保险箱会自动记住最后 n 位输入,如果 ...
- Java实现 LeetCode 654 最大二叉树(递归)
654. 最大二叉树 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左子树是通过数组中最大值左边部分构造出的最大二叉树. 右子树是通过数组中最 ...
- Java for LeetCode 044 Wildcard Matching
Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- 设计模式之GOF23原型模式01
原型模式prototype 原型模式: - 通过new产生一个对象需要非常繁琐的数据准备或者访问权限,则可以使用原型模式,比如如果new对象所需时间过长,可以通过克隆产生相同的副本 - Java中的克 ...
- [hdu5249]动态中位数
题意:3种操作分别为入队,出队,查询当前队列的中位数.操作数为1e5数量级. 思路:先考虑离线算法,可以离散+线段树,可以划分树,考虑在线算法,则有treap名次树,SBtree(size balan ...
- Flutter RenderBox指南——绘制篇
本文基于1.12.13+hotfix.8版本源码分析. 0.大纲 RenderBox的用法 通过RenderObjectWidget把RenderBox塞进界面 1.RenderBox 在flutte ...
- fastadmin后台:在表单操作项添加操作按钮并控制弹出页面的大小
1.进入对应目录:eg(public/assets/js/backend/conmpany.js) 2.在field:'operate' 中添加buttons 源码: {field: 'operate ...
- 8.1Go并发
第八章 Go并发 Go语言区别于其他语言的一大特点就是出色的并发性能,最重要的一个特性那就是go关键字. 并发场景: UI小姐姐一边开着PS软件,一边微信疯狂的和产品经理打字交流,后台还听着网易云音乐 ...
- linux常用命令---用户相关操作
用户相关操作
- 计算两点间的距离(hdu2001)
注意:在C语言中,double->lf,结果保留两位小数->0.2lf #include<stdio.h> #include<math.h> using names ...
- Lowest Common Multiple Plus(hdu2028)
思考: 乘法爆咋数据.把int换成unsigned就过了,同时%d换成%u.求最大公约数和最小公倍数. #include<stdio.h> int gcd(unsigned x, unsi ...
- SpringBoot2.1电商通用(微信+支付宝)支付系统实战
『课程目录』: ├─第10章 全模块电商系统之商品模块 │ 10-1_商品列表-上.mp4 │ 10-2_商品列表-中.mp4 │ 10-3_商品列表-下.mp4 │ ...
- MarkDown语法使用(效果版本)
function syntaxHighlighting() { var n = 33; var s = "hello, こんにちは"; console.log(s); } plai ...