当前位置:网站首页>递增的三元子序列[贪心训练]
递增的三元子序列[贪心训练]
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 递增的三元组
边栏推荐
- Gorm 读写分离(转)
- 【R语言数据科学】:交叉验证再回首
- 吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行
- 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
- LifeCycle
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- LiveData
- ARouter的使用
- 吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
- sharding key type not supported
猜你喜欢

392. 判断子序列

Install Trinity and solve error reporting
![[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions](/img/7a/9b559313b407f9a12cbaed7bebd4dc.png)
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions

【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value

基于YOLOv1的口罩佩戴检测

瑞吉外卖笔记

小程序直播 + 电商,想做新零售电商就用它吧!

Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test

MySQL8版本免安装步骤教程

Test evaluation of software testing
随机推荐
2022 hoisting machinery command examination simulation 100 questions simulation examination platform operation
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
392. Judgement subsequence
C language programming topic reference
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
What is the real meaning and purpose of doing things, and what do you really want
Secretary of Homeland Security of the United States: domestic violent extremism is one of the biggest terrorist threats facing the United States at present
go vendor 项目迁移到 mod 项目
读取 Excel 表数据
国内酒店交易DDD应用与实践——代码篇
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
QT how to detect whether the mouse is on a control
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
面试拆解:系统上线后Cpu使用率飙升如何排查?
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
392. 判断子序列
使用默认路由作为指向Internet的路由