当前位置:网站首页>跳跃游戏II[贪心练习]
跳跃游戏II[贪心练习]
2022-06-24 06:32:00 【REN_林森】
前言
对于求最小最大问题,一般可与贪心联系起来,比如求最少步数,可以找每步能走的最远距离,以此达到最小步数。
一、跳跃游戏II

二、贪心思想练习
1、大顶堆优先队列
// 跳跃游戏II
public class Jump {
/* target:最少多少步能走到最后一个位置。 找每走一步能走到的最远距离,当走n步最远距离大于nums.length时,则为最小步数。最远距离使步数最小。 */
public int jump(int[] nums) {
if (nums.length == 1) return 0;
// 大顶堆。
PriorityQueue<int[]> que = new PriorityQueue<>((o1, o2) -> o2[0] + o2[1] - o1[0] - o1[1]);
que.offer(new int[]{
0, nums[0]});
int cnt = 1;
while (!que.isEmpty()) {
int[] arr = que.poll();
if (arr[0] + arr[1] >= nums.length - 1) return cnt;
for (int i = arr[0] + 1; i <= arr[0] + arr[1] && i < nums.length; i++) {
que.offer(new int[]{
i, nums[i]});
}
++cnt;
}
return cnt;
}
}
2、改进–记录最大值即可
// 记录跳的最远的位置即可,不需要用优先队列每次offer都logn
class Jump2 {
/* target:最少多少步能走到最后一个位置。 找没走一步能走到的最远距离,当走n步最远距离大于nums.length时,则为最小步数。最远距离使步数最小。 */
public int jump(int[] nums) {
if (nums.length == 1) return 0;
// max(下标 + 跳跃步数)
int[] maxDis = new int[]{
0, nums[0]};
int cur = 1;
int cnt = 1;
while (true) {
if (maxDis[0] + maxDis[1] >= nums.length - 1) return cnt;
int end = maxDis[0] + maxDis[1];
while (cur <= end) {
if (cur + nums[cur] > maxDis[0] + maxDis[1]) {
maxDis[0] = cur;
maxDis[1] = nums[cur];
}
++cur;
}
++cnt;
}
}
}
总结
1)贪心,想找最小,就必须大步往前走,想100米成绩好,就要跑的更快。
参考文献
[1] LeetCode 跳跃游戏II
边栏推荐
- Analysis and treatment of easydss flash back caused by system time
- The installation method of apache+mysql+php running environment under Windows
- "Adobe international certification" in the design industry, why can't big but big designs have good results?
- What is the domain name query network and how it should be used
- 10 year old drivers who have been engaged in software testing tell you what type of software is suitable for automation
- Replacing human eyes -- visual inspection technology
- The errorcontrol registry of the third-party service is 3, which may cause the system to cycle restart. For example, ldpkit introduced by WPS
- The three-year action plan of the Ministry of industry and information technology has been announced, and the security industry has ushered in major development opportunities!
- Network Overview
- What is a secondary domain name primary domain name how to apply for a secondary domain name
猜你喜欢

解读AI机器人产业发展的顶层设计

Manual for automatic testing and learning of anti stepping pits, one for each tester
Fault analysis | using --force to batch import data leads to partial data loss

【二叉树】——二叉树中序遍历

创客教育给教师发展带来的挑战

Enter the software test pit!!! Software testing tools commonly used by software testers software recommendations
Oracle case: ohasd crash on AIX

基于三维GIS系统的智慧水库管理应用

Technology is a double-edged sword, which needs to be well kept

A cigarette of time to talk with you about how novices transform from functional testing to advanced automated testing
随机推荐
How accurate are the two common methods of domain name IP query
Talk about the story behind search engines
What is the domain name query network and how it should be used
Application of O & M work order
Architecture: rest and HATEOAS
Kangaroo cloud: the overall architecture and key technical points of building a real-time computing platform based on Flink
Easyrtc call error `failed to execute'send'on'rtcdatachannel'
Replacing human eyes -- visual inspection technology
When the VPC main network card has multiple intranet IP addresses, the server cannot access the network internally, but the server can be accessed externally. How to solve this problem
Tencent launched the "reassuring agricultural product plan" to support 100 landmark agricultural product brands!
基于三维GIS系统的智慧水库管理应用
How to solve the problem that after Tencent cloud sets static DNS, restarting the machine becomes dynamic DNS acquisition
When should I use Apache Druid
Free and easy-to-use screen recording and video cutting tool sharing
SAP hum unbinds Hu from delivery order
What are the common network protocols
Use of SAP QM inspection points
Analysis of official template of wechat personnel recruitment management system (I)
Analysis on the influence of "network security policy issued successively" on Enterprises
Centos7 deploying mysql-5.7