当前位置:网站首页>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
边栏推荐
- MATLAB中tiledlayout函数使用
- Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
- 吃透Chisel语言.03.写给Verilog转Chisel的开发者(没有Verilog基础也可以看看)
- 吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
- MySQL version 8 installation Free Tutorial
- Golang 使用 JSON unmarshal 数字到 interface{} 数字变成 float64 类型(转)
- 基于PaddleX的智能零售柜商品识别
- TestSuite and testrunner in unittest
- 吃透Chisel语言.07.Chisel基础(四)——Bundle和Vec
- Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
猜你喜欢

Product identification of intelligent retail cabinet based on paddlex

使用默认路由作为指向Internet的路由

華昊中天沖刺科創板:年虧2.8億擬募資15億 貝達藥業是股東

锐成芯微冲刺科创板:年营收3.67亿拟募资13亿 大唐电信是股东

安装Mysql

MySQL 5 installation and modification free

吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
![Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]

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

Use the default route as the route to the Internet
随机推荐
Summary of recent days (non-technical article)
安装Mysql
Hardware Basics - diode Basics
吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程
硬件基础知识-二极管基础
Excel快速合并多行数据
英视睿达冲刺科创板:年营收4.5亿 拟募资9.79亿
php 日志调试
MySQL5免安装修改
软件测试之测试评估
测试流程整理(2)
Basic mode of service mesh
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
游戏出海,全球化运营
Gorm 读写分离(转)
File creation, writing, reading, deletion (transfer) in go language
Blob, text geometry or JSON column'xxx'can't have a default value query question
数据仓库面试问题准备
DDD application and practice of domestic hotel transactions -- Code
Gorm read / write separation (rotation)