当前位置:网站首页>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
边栏推荐
- ClickHouse 复制粘贴多行sql语句报错
- Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
- vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
- XML建模
- MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
- ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
- Generics of TS
- selenium 获取dom内属性值的方法
- ts 之 泛型
- @Validated basic parameter verification, grouping parameter verification and nested parameter verification
猜你喜欢
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
leetcode:1139. 最大的以 1 为边界的正方形
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
第05章_存储引擎
Display DIN 4102-1 Class B1 fire test requirements
Talk about my fate with some programming languages
基於flask寫一個接口
Learning robots have no way to start? Let me show you the current hot research directions of robots
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
EN 438-7 laminated sheet products for building covering decoration - CE certification
随机推荐
Dictionary tree simple introductory question (actually blue question?)
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
Viewrootimpl and windowmanagerservice notes
Aitm 2-0003 horizontal combustion test
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
ODPs next map / reduce preparation
js常用方法封装
教你自己训练的pytorch模型转caffe(二)
Establishment of terminal security capability verification environment and penetration test records
浅聊我和一些编程语言的缘分
How to renew NPDP? Here comes the operation guide!
Duchefa cytokinin dihydrozeatin (DHZ) instructions
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
Hdu2377bus pass (build more complex diagram +spfa)
MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
Add ICO icon to clion MinGW compiled EXE file
Determine the best implementation of horizontal and vertical screens
Simple getting started example of Web Service
示波器探头对信号源阻抗的影响
Careercup its 1.8 serial shift includes problems