当前位置:网站首页>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
边栏推荐
- 木板ISO 5660-1 热量释放速率摸底测试
- 字典树简单入门题(居然是蓝题?)
- The reason why the ncnn converted model on raspberry pie 4B always crashes when called
- ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
- Binary search
- XML建模
- The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application
- MySQL InnoDB架构原理
- Hdu2377bus pass (build more complex diagram +spfa)
- Material design component - use bottomsheet to show extended content (II)
猜你喜欢

Pytoch practice -- MNIST dataset handwritten digit recognition

leetcode:1755. 最接近目标值的子序列和

EN 438-7 laminated sheet products for building covering decoration - CE certification
MySQL fully parses json/ arrays

从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题

第05章_存储引擎

教你自己训练的pytorch模型转caffe(一)

基于AVFoundation实现视频录制的两种方式

Clion configures Visual Studio (MSVC) and JOM multi-core compilation

Using webassembly to operate excel on the browser side
随机推荐
浅聊我和一些编程语言的缘分
MySQL fully parses json/ arrays
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
ODPs next map / reduce preparation
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
js常用方法封装
Modifiers of attributes of TS public, private, protect
Duchefa cytokinin dihydrozeatin (DHZ) instructions
实现浏览页面时校验用户是否已经完成登录的功能
postgis 安装地理信息扩展
Interpreting the daily application functions of cooperative robots
Utils/index TS tool function
研學旅遊實踐教育的開展助力文旅產業發展
Which securities company is better and which platform is safer for stock account opening
Determine the best implementation of horizontal and vertical screens
Is it necessary for bazel to learn
The development of research tourism practical education helps the development of cultural tourism industry
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel
R语言【数据管理】