当前位置:网站首页>递增的三元子序列[贪心训练]
递增的三元子序列[贪心训练]
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 递增的三元组
边栏推荐
- Automatic filling of database public fields
- 学习项目是自己找的,成长机会是自己创造的
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- Summary of recent days (non-technical article)
- 吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件
- [antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
- 中邮科技冲刺科创板:年营收20.58亿 邮政集团是大股东
- markdown 语法之字体标红
- What is the real meaning and purpose of doing things, and what do you really want
- Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
猜你喜欢
【R语言数据科学】:交叉验证再回首
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
如何在 2022 年为 Web 应用程序选择技术堆栈
CVPR 2022 | 大幅减少零样本学习所需的人工标注,提出富含视觉信息的类别语义嵌入(源代码下载)...
【Antd】Antd 如何在 Form.Item 中有 Input.Gourp 时获取 Input.Gourp 的每一个 Input 的value
2022 practice questions and mock exams for the main principals of hazardous chemical business units
SCM polling program framework based on linked list management
JVM memory layout detailed, illustrated, well written!
MySQL5免安装修改
基于PaddleX的智能零售柜商品识别
随机推荐
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
Assertion of unittest framework
做事的真正意义和目的,真正想得到什么
吃透Chisel语言.03.写给Verilog转Chisel的开发者(没有Verilog基础也可以看看)
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
mac redis安装与使用,连接远程服务器 redis
IP lab monthly resumption · issue 5
30: Chapter 3: develop Passport Service: 13: develop [change / improve user information, interface]; (use * * * Bo class to accept parameters, and use parameter verification)
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
Unittest框架中引入TestFixture
Common content type correspondence table
吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行
Automatic filling of database public fields