当前位置:网站首页>Longest swing sequence [greedy practice]
Longest swing sequence [greedy practice]
2022-07-05 21:05:00 【REN_ Linsen】
Greedy practice
Preface
greedy / Dynamic programming / Monotonic stack , They are all high-level topics that examine logical ability , These questions need to be analyzed , Need better versatility .
One 、 Longest swing sequence
Two 、 Greedy Algorithm
1、 The conditions that greed needs to meet
Optimal substructure : The solution of the larger problem consists of the solution of the smaller subproblem , The solution of the larger problem is determined only by the solution of one of the smaller subproblems ;
No aftereffect : The solution in the later stage will not modify the results calculated in the previous stage ;
Greedy choice nature : The global optimal solution can be obtained from the local optimal solution .
notes : Among them, it is particularly important to grasp the non aftereffect . Greedy algorithm is to make the best choice in the current state and think that it can solve the problem .
2、 Thought analysis
This question , Just find the peaks and valleys in sequence , Take the peak and trough as the nodes of swing , The greedy value problem of its system .
3、code
package everyday.greed;
public class WiggleMaxLength {
/* target: Find the longest wiggler sequence . Take feifeng / Delete the valley element , But here we only need to count the length of the longest swing sequence , Not the element of the longest swing sequence , So only calculate the peak / Valley node points , It is equivalent to deleting other non peaks in disguise / Valley node . */
public int wiggleMaxLength(int[] nums) {
if (nums.length < 2) return nums.length;
// The core : As long as the peak / The lowest element , That is, the maximum and minimum value , Swing up .
int curDiff = 0, preDiff = 0, ans = 1;
for (int i = 0; i < nums.length - 1; i++) {
curDiff = nums[i + 1] - nums[i];
// Calculate peak || Calculate Valley
if (curDiff > 0 && preDiff <= 0 || curDiff < 0 && preDiff >= 0) {
++ans;
preDiff = curDiff;
}
}
return ans;
}
}
summary
1) Greedy practice .
reference
边栏推荐
- selenium 获取dom内验证码图片
- leetcode:1755. 最接近目标值的子序列和
- EN 438-7 laminated sheet products for building covering decoration - CE certification
- Écrire une interface basée sur flask
- ArcGIS栅格重采样方法介绍
- Five layer network protocol
- 字典树简单入门题(居然是蓝题?)
- 判断横竖屏的最佳实现
- 2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
- MySQL 千万数据量深分页优化, 拒绝线上故障!
猜你喜欢
最长摆动序列[贪心练习]
Interpreting the daily application functions of cooperative robots
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
研学旅游实践教育的开展助力文旅产业发展
【案例】元素的显示与隐藏的运用--元素遮罩
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
第05章_存储引擎
MySQL 千万数据量深分页优化, 拒绝线上故障!
Influence of oscilloscope probe on signal source impedance
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
随机推荐
Five layer network protocol
LeetCode_哈希表_困难_149. 直线上最多的点数
启牛2980有没有用?开户安全吗、
[case] Application of element display and hiding -- element mask
Pytoch practice -- MNIST dataset handwritten digit recognition
Talk about my fate with some programming languages
Which is the best online collaboration product? Microsoft loop, notion, flowus
Cutting edge technology for cultivating robot education creativity
selenium 获取dom内属性值的方法
Sophomore personal development summary
序列联配Sequence Alignment
selenium 查找b或p标签的内容
Wood board ISO 5660-1 heat release rate mapping test
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
Écrire une interface basée sur flask
MySQL InnoDB架构原理
MySQL 千万数据量深分页优化, 拒绝线上故障!
Binary search
ODPS 下一个map / reduce 准备
概率论机器学习的先验知识(上)