当前位置:网站首页>Incremental ternary subsequence [greedy training]
Incremental ternary subsequence [greedy training]
2022-07-04 14:15:00 【REN_ Linsen】
Greedy practice
Preface
Greed requires careful analysis + Clever thinking , Therefore, we need the accumulation of detailed thinking during daily practice + Breadth of knowledge , It is a good type of problem to investigate the ability of algorithm problem , And monotone stack /dp equally .
One 、 Increasing ternary subsequences
Two 、 Greedy practice
package everyday.greed;
// Increasing ternary subsequences
public class IncreasingTriplet {
/* target: Front middle back , Whether there is an increasing sequence . It is possible to record */
// 4 5 1 2 3
// 4 5 1 8
// Violence to find feelings
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;
}
/* Small middle right , In the middle mid For reference point . If there is the smallest element on the left minLeft & minLeft < mid. The largest element on the right maxRight & maxRight > mid, There are such triples . You can record the minimum left and maximum right in advance , Space for time . */
public boolean increasingTriplet2(int[] nums) {
int n = nums.length;
int[] minLeft = new int[n];
int[] maxRight = new int[n];
// Fill in the smallest element on the left .
// bug1:1 << 31 yes Integer.MIN_VALUE;1 << 30 Not at all 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]);
}
// Traverse nums, Make it nums[i] For the center , Determine whether there are qualified nodes on both sides .
for (int i = 0; i < n; i++) if (nums[i] > minLeft[i] && maxRight[i] > nums[i]) return true;
// Such triples cannot be found .
return false;
/* summary : Why can't I think of this practice ? First of all , The accumulation of algorithm knowledge is less , Too little knowledge . second , Due to accumulation , still get Not to the key point of the problem . Third , This multi-element relative problem , The reference point is too rigid , For example, if you come up, you can find Zuo first -> Find the center larger than the left -> Find the right greater than the middle . If I decide first , Just find the left smaller than the middle -> Think of this left as the minimum value of left ; Then find the right greater than the middle -> Think of this right as the maximum value of the right . Thus, I think of using space for time , Record left -> The minimum value of right ; Right -> The maximum value of the left . */
}
/* The above ideas , Give us : The left side is the smallest , The right side is the largest . To the left left in mid For reference , Look for right right, If right > mid, Then find the legal triples ; If right > left, The replacement mid = right, Let the smaller value be in . If right < left, Then let right Become Zuo Xiao , Fix the left smallest , Isn't that right mid Need to change , After all mid The subscript of is less than right Of ! Unwanted , The original left < mid The relationship still exists ,right displacement left, Just make sure that the following values can be replaced slowly mid, And then find the truth that meets the conditions right. as long as left Position in , It saves what once existed left < mid Relationship , and left = right, It also keeps the current minimum value information on the left , In order to find . */
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;
}
// Not found
return false;
/* summary : This greed , It really needs some analysis & understand & Sense & Only through training can you think . So I want to learn algorithm knowledge & See the algorithm ideas there are many first 500 -> 1000 -> 2000 It's a question , There is still a long way , There's still a lot to learn , analysis & The ability to understand problems needs more training . */
}
}
summary
1) Why can't I think of this practice ?
First of all , The accumulation of algorithm knowledge is less , Too little knowledge ;
second , Due to accumulation , still get Not to the key point of the problem ;
Third , This multi-element relative problem , The reference point is too rigid , For example, if you come up, you can find Zuo first -> Find the center larger than the left -> Find the right greater than the middle .
If I decide first , Just find the left smaller than the middle -> Think of this left as the minimum value of left ; Then find the right greater than the middle -> Think of this right as the maximum value of the right .
Thus, I think of using space for time , Record left -> The minimum value of right ; Right -> The maximum value of the left .2) Why can't such clever greed think of ?
It really needs some analysis & understand & Sense & Only through training can you think .
So I want to learn algorithm knowledge & See the algorithm ideas there are many first 500 -> 1000 -> 2000 It's a question , There is still a long way , There's still a lot to learn , analysis & The ability to understand problems needs more training .
reference
边栏推荐
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
- Test evaluation of software testing
- Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
- 2022游戏出海实用发行策略
- [matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
- mac redis安装与使用,连接远程服务器 redis
- Fs4059c is a 5V input boost charging 12.6v1.2a. Inputting a small current to three lithium battery charging chips will not pull it dead. The temperature is 60 ° and 1000-1100ma is recommended
- MongoDB常用28条查询语句(转)
- 测试流程整理(2)
- What is the real meaning and purpose of doing things, and what do you really want
猜你喜欢
DDD application and practice of domestic hotel transactions -- Code
1200. 最小绝对差
华昊中天冲刺科创板:年亏2.8亿拟募资15亿 贝达药业是股东
The font of markdown grammar is marked in red
華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東
学内核之三:使用GDB跟踪内核调用链
Qt如何实现打包,实现EXE分享
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
sharding key type not supported
随机推荐
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
How to package QT and share exe
qt 怎么检测鼠标在不在某个控件上
使用默认路由作为指向Internet的路由
10.(地图数据篇)离线地形数据处理(供Cesium使用)
R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
MATLAB中tiledlayout函数使用
JVM memory layout detailed, illustrated, well written!
Mask wearing detection based on yolov1
QT how to detect whether the mouse is on a control
【C 题集】of Ⅶ
Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
Blob, text geometry or JSON column'xxx'can't have a default value query question
读取 Excel 表数据
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
vscode 常用插件汇总
File creation, writing, reading, deletion (transfer) in go language
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
Deming Lee listed on Shenzhen Stock Exchange: the market value is 3.1 billion, which is the husband and wife of Li Hu and Tian Hua