【LEETCODE】67、分治递归,medium&hard级别,题目:215、312
我被这些题整哭了,你呢???
日了狗啊。。。。。。
好难啊。。。。
按照这个样子搞,不用找工作了,回家放牛去。。。。。。。
package y2019.Algorithm.divideandconquer.medium; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.divideandconquer.medium
* @ClassName: FindKthLargest
* @Author: xiaof
* @Description: 215. Kth Largest Element in an Array
* Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order,
* not the kth distinct element.
*
* Example 1:
*
* Input: [3,2,1,5,6,4] and k = 2
* Output: 5
*
* Example 2:
* Input: [3,2,3,1,2,4,5,5,6] and k = 4
* Output: 4
*
* 这题可以直接排序,然后获取第K个位置的数据即可,但是这不是本题的考察点
* 本地用快排,直接找到第k个坑的值
*
* @Date: 2019/8/13 8:59
* @Version: 1.0
*/
public class FindKthLargest { public int solution(int[] nums, int k) {
int l = 0, r = nums.length - 1; //定义快排的左右边界
while (l <= r) {
int pos = partition(nums, l, r);
//找到中间的位置
if (pos == k - 1) {
//找到对应的值
return nums[pos];
} else if (pos > k - 1) {
//如果位置比第k个数要大,那么说明第k位再左边
r = pos - 1;
} else {
l = pos + 1;
}
} return -1;
} public int partition(int[] nums, int l, int r) {
int init = nums[l], l1 = l + 1, r1 = r;
while (l1 <= r1) {
//遍历交换数据
if (nums[l1] < init && nums[r1] > init) {
//交换位置数据
int temp = nums[l1];
nums[l1++] = nums[r1];
nums[r1--] = temp;
} if (nums[l1] >= init) {
++l1;
} if (nums[r1] < init) {
--r1;
}
} //当r1越过l1的时候,交换回来
nums[l] = nums[r1];
nums[r1] = init; return r1;
} public static void main(String[] args) {
int s[] = {3,2,3,1,2,4,5,5,6};
int k = 4;
int s1[] = {1};
int k1 = 1;
int s2[] = {99,99};
int k2 = 1;
int s3[] = {2,1};
int k3 = 1;
int s4[] = {-1, 2, 0};
int k4 = 1; FindKthLargest fuc = new FindKthLargest(); fuc.solution(s4, k4); }
}
package y2019.Algorithm.divideandconquer.hard; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.divideandconquer.hard
* @ClassName: MaxCoins
* @Author: xiaof
* @Description: 312. Burst Balloons
* Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums.
* You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins.
* Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.
* Find the maximum coins you can collect by bursting the balloons wisely.
*
* Note:
* You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
* 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
* Example:
*
* Input: [3,1,5,8]
* Output: 167
* Explanation: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
* coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
* @Date: 2019/8/13 9:43
* @Version: 1.0
*/
public class MaxCoins { public int solution(int[] nums) {
//逆向思维,我们发现每次都是再已经存在的气球中进行爆破,这里尝试改变想法为,本来没有气球,加入气球进行爆破
//我们从最后一步开始思考,每次只需要递归到上一层有+1个气球的时候,进行爆破得到当前环节
//1.首先创建长度+1的数组,因为起始和结束的位置默认为1
int[] resarray = new int[nums.length + 2];
int n = 1;
for (int x : nums) {
//这里排除掉为0的数据
if (x > 0) {
resarray[n++] = x;
}
}
resarray[0] = resarray[n++] = 1; //这个数组表示,再i,j区间内进行爆破的最大值
int[][] back = new int[n][n];
return burst(back, resarray, 0, n - 1);
} public int burst(int[][] back, int[] nums, int left, int right) {
//如果是边界,或两个索引之间没有空隙了,那么就不用插入了
if (left + 1 == right) return 0;
if (back[left][right] > 0) {
//如果已经计算过了,不做重复计算
return back[left][right];
}
//计算每个位置作为最后一个爆破点的时候值
int ans = 0;
for (int i = left + 1; i < right; ++i) {
ans = Math.max(ans, nums[left] * nums[i] * nums[right] + burst(back, nums, left, i) + burst(back, nums, i, right));
}
//修改值内容,继续递归
back[left][right] = ans;
return ans;
} /**
*
* @program: y2019.Algorithm.divideandconquer.hard.MaxCoins
* @description: https://leetcode.com/problems/burst-balloons/discuss/76228/Share-some-analysis-and-explanations
* @auther: xiaof
* @date: 2019/8/13 11:20
*/
public int maxCoins(int[] iNums) {
int[] nums = new int[iNums.length + 2];
int n = 1;
for (int x : iNums) if (x > 0) nums[n++] = x;
nums[0] = nums[n++] = 1; int[][] memo = new int[n][n];
return burst2(memo, nums, 0, n - 1);
} public int burst2(int[][] memo, int[] nums, int left, int right) {
if (left + 1 == right) return 0;
if (memo[left][right] > 0) return memo[left][right];
int ans = 0;
for (int i = left + 1; i < right; ++i)
ans = Math.max(ans, nums[left] * nums[i] * nums[right]
+ burst2(memo, nums, left, i) + burst2(memo, nums, i, right));
memo[left][right] = ans;
return ans;
} public static void main(String[] args) {
int s[] = {3,1,5,8};
int[] s1 = {8,2,6,8,9,8,1,4,1,5,3,0,7,7,0,4,2,2,5};
int[] s2 = {8,3,4,3,5,0,5,6,6,2,8,5,6,2,3,8,3,5,1,0,2}; MaxCoins fuc = new MaxCoins(); // fuc.solution(s2);
fuc.maxCoins(s2); }
}
【LEETCODE】67、分治递归,medium&hard级别,题目:215、312的更多相关文章
- [LeetCode题解]23. 合并K个升序链表 | 分治 + 递归
方法一:分治 + 递归 解题思路 在21. 合并两个有序链表,我们知道如何合并两个有序链表.而本题是合并 k 个有序链表,可以通过大问题拆分成小问题解决,即把 k 个链表,拆分成 k/2 个链表组,俩 ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- CF448C Painting Fence (分治递归)
Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- 【LEETCODE】65、字符分类,medium&easy级别,题目:20、647、3
今天的字符类还比较简单 package y2019.Algorithm.str.easy; import java.util.HashMap; import java.util.Map; import ...
- 【LEETCODE】64、链表分类,medium&hard级别,题目:2,138,142,23
package y2019.Algorithm.LinkedList.medium; import y2019.Algorithm.LinkedList.ListNode; /** * @Projec ...
- LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...
随机推荐
- for、for...in、for...of的区别
当有一个元素未定义时,for和for...of遍历该元素为undefined,for...in遍历不到. 如果是自定义属性,for和for...of无法遍历,for...in可以遍历. for...i ...
- 《Elasticsearch实战》读书笔记
遗留问题: 1._source字段和field字段的区别 2.q和search的区别(查询请求中) 3.输入关键字的大小写,参考prefix查询,match_phrase_prefix查询(4.4.2 ...
- 【luoguP1858】多人背包
链接 对于每个状态\(f[j]\)多记录一个维度,转移的时候利用类似于归并排序的方法合并,以保证时间复杂度可以承受 注意事项:前\(K\)大可以有重复的价值 #include<iostream& ...
- [西软xms]会员卡消费和余额情况表
select * from vipcard; #过滤卡类型财富卡(CFK)权益卡(QYK)幸福卡(XFK) select id from vipcard where (card_class ='XFK ...
- python 获取文件运行路径
import os print(os.getcwd()) print("/".join(os.path.dirname(os.path.abspath(__file__)).spl ...
- IDEA 调试图文教程,让 bug 无处藏身!
阅读本文大概需要 6.2 分钟. 来源:http://t.cn/EoPN7J2 Debug用来追踪代码的运行流程,通常在程序运行过程中出现异常,启用Debug模式可以分析定位异常发生的位置,以及在运行 ...
- RSA后台签名前台验签的应用(前台采用jsrsasign库)
写在前面 安全测试需要, 为防止后台响应数据返给前台过程中被篡改前台再拿被篡改后的数据进行接下来的操作影响正常业务, 决定采用RSA对响应数据进行签名和验签, 于是有了这篇<RSA后台签名前台验 ...
- windows命令行将应用程序加入环境变量
1.命令行方法,最快(推荐): 1.1.获取应用安装绝对路径: 方法一:一层层点进去,然后复制路径栏目:方法二:打开软件执行文件所在目录,按住shift点击鼠标邮件,选择powerShell,现在wi ...
- Vue 项目中断点没有跳转到指定源码的问题
将配置中 devtool 改为以下即可. devtool: 'source-map' 如果是在 vue-cli 2.x ,那么就在 webpack.dev.config.js 中 如果是 vue-cl ...
- 运维笔记--Debian/Ubuntu系统离线安装pymssql,连接SqlServer
场景描述: 开始之前,先对Debian和Ubuntu操作系统做个了解,两者都是Linux阵营中很有影响力的发行版本,可以简单理解成“Ubuntu源自Debian,两者系统操作命令基本相同,相比Ubun ...