当前位置:网站首页>递增的三元子序列[贪心训练]
递增的三元子序列[贪心训练]
2022-07-04 12:51:00 【REN_林森】
前言
贪心需要认真分析+巧妙的思考,所以需要平时练习时细节思考的积累+见识广度,是考察算法题能力的好类型题,和单调栈/dp一样。
一、递增的三元子序列
二、贪心练习
package everyday.greed;
// 递增的三元子序列
public class IncreasingTriplet {
/* target:前中后,是否存在递增序列。 可记录以每个位置结尾的 */
// 4 5 1 2 3
// 4 5 1 8
// 暴力找找感觉
public boolean increasingTriplet(int[] nums) {
int n = nums.length;
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
if (nums[i] >= nums[j]) continue;
for (int k = j + 1; k < n; k++) {
if (nums[j] >= nums[k]) continue;
return true;
}
}
}
return false;
}
/* 小中右,以中mid为参考点。如果存在左边最小元素minLeft & minLeft < mid。右边最大元素maxRight & maxRight > mid,则存在这样的三元组。 可提前把最小左和最大右记录好,空间换时间。 */
public boolean increasingTriplet2(int[] nums) {
int n = nums.length;
int[] minLeft = new int[n];
int[] maxRight = new int[n];
// 填充左边最小元素。
// bug1:1 << 31是Integer.MIN_VALUE;1 << 30并不是Integer.MAX_VALUE
int min = 0x7FFFFFFF;
// int min = -((1 << 31) + 1);
for (int i = 0; i < n; i++) {
minLeft[i] = min;
min = Math.min(min, nums[i]);
}
int max = 1 << 31;
for (int i = n - 1; i >= 0; i--) {
maxRight[i] = max;
max = Math.max(max, nums[i]);
}
// 遍历nums,使其nums[i]为中心点,判定两边的是否有符合条件的节点。
for (int i = 0; i < n; i++) if (nums[i] > minLeft[i] && maxRight[i] > nums[i]) return true;
// 找不到这样的三元组。
return false;
/* 总结:为什么我想不到这做法? 第一,算法知识量积累少,见识太少。 第二,由于积累量,还是get不到问题的关键点。 第三,这种多元素相对问题,参考点过于死板,比如一上来就算先找左->找大于左的中->找大于中的右。 如果我先确定中,只需找小于中的左 ->联想到这个左应该是左的最小值;再找大于中的右->联想到这个右应该是右的最大值。 从而联想到用空间换时间,记录左->右的最小值;右->左的最大值。 */
}
/* 上面的思路,提供给我们:左边放最小,右边放最大。 以左left 中mid 为参考,寻找右right, 如果right > mid,则找到合法三元组; 如果right > left,则替换mid = right,让小点的值作中。 如果right < left,则让right成为左小,固定好左最小,这样岂不是mid需要换,毕竟mid的小标是小于right的! 不需要,原来的left < mid关系依然存在,right顶替left,只是保证后面的值能慢慢替换mid,进而找到符合条件的真right。 只要left位置在,就保存了曾经存在的left < mid关系,而且left = right,还保持了当前左边的最小值信息,以便后面寻找。 */
public boolean increasingTriplet3(int[] nums) {
int left = nums[0], mid = 0x7FFFFFFF;
for (int i = 1; i < nums.length; i++) {
int right = nums[i];
if (right > mid) return true;
if (right > left) mid = right;
else left = right;
}
// 没有寻找到
return false;
/* 总结:这种贪心,确实需要一定的分析&理解&见识&训练才能想得到。 所以我要学的算法知识&见的算法思路还有很多先500 -> 1000 -> 2000题吧,路还很长,要学的还很多,分析&理解问题的能力还得多训练。 */
}
}
总结
1)为什么我想不到这做法?
第一,算法知识量积累少,见识太少;
第二,由于积累量,还是get不到问题的关键点;
第三,这种多元素相对问题,参考点过于死板,比如一上来就算先找左->找大于左的中->找大于中的右。
如果我先确定中,只需找小于中的左 ->联想到这个左应该是左的最小值;再找大于中的右->联想到这个右应该是右的最大值。
从而联想到用空间换时间,记录左->右的最小值;右->左的最大值。2)这种巧妙的贪心为什么想不到?
确实需要一定的分析&理解&见识&训练才能想得到。
所以我要学的算法知识&见的算法思路还有很多先500 -> 1000 -> 2000题吧,路还很长,要学的还很多,分析&理解问题的能力还得多训练。
参考文献
[1] LeetCode 递增的三元组
边栏推荐
- What is the real meaning and purpose of doing things, and what do you really want
- 以房抵债能否排除强制执行
- Golang 使用 JSON unmarshal 数字到 interface{} 数字变成 float64 类型(转)
- China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
- 2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation
- Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
- 中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东
- Read excel table data
- JVM memory layout detailed, illustrated, well written!
- Excel快速合并多行数据
猜你喜欢
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
使用默认路由作为指向Internet的路由
【Antd踩坑】Antd Form 配合Input.Group时出现Form.Item所占据的高度不对
205. 同构字符串
Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
Use the default route as the route to the Internet
华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
392. Judgement subsequence
随机推荐
30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
The font of markdown grammar is marked in red
奇妙秘境 码蹄集
Five "potential errors" in embedded programming
吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件
Use the default route as the route to the Internet
MySQL version 8 installation Free Tutorial
Worried about "cutting off gas", Germany is revising the energy security law
Unittest框架之断言
One of the solutions for unity not recognizing riders
Detailed explanation of Fisher information quantity detection countermeasure sample code
吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
Introducing testfixture into unittest framework
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
R语言使用dplyr包的group_by函数和summarise函数基于分组变量计算目标变量的均值、标准差
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
Product identification of intelligent retail cabinet based on paddlex