当前位置:网站首页>Jumping game ii[greedy practice]
Jumping game ii[greedy practice]
2022-06-24 06:41:00 【REN_ Linsen】
Greedy practice
Preface
For the problem of finding the minimum and maximum , Generally, it can be associated with greed , For example, find the minimum number of steps , You can find the longest distance you can walk every step , To achieve the minimum number of steps .
One 、 Jumping game II

Two 、 Practice greedy thinking
1、 Large top heap priority queue
// Jumping game II
public class Jump {
/* target: How many steps can you take to the last position . Find the farthest you can reach with each step , When go n The maximum distance of steps is greater than nums.length when , Is the minimum number of steps . The longest distance minimizes the number of steps . */
public int jump(int[] nums) {
if (nums.length == 1) return 0;
// Big pile top .
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、 improvement – Record the maximum value
// Record the farthest position of the jump , There is no need to use a priority queue every time offer all logn
class Jump2 {
/* target: How many steps can you take to the last position . Find the farthest you can reach without taking a step , When go n The maximum distance of steps is greater than nums.length when , Is the minimum number of steps . The longest distance minimizes the number of steps . */
public int jump(int[] nums) {
if (nums.length == 1) return 0;
// max( Subscript + Jump steps )
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;
}
}
}
summary
1) greedy , Looking for the smallest , We must stride forward , Want to 100 Good performance in meters , To run faster .
reference
边栏推荐
- 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
- Easy car Interviewer: talk about MySQL memory structure, index, cluster and underlying principle!
- Easynvr is optimized when a large number of videos are not online or unstable due to streaming failure
- CloudCompare&PCL 点云裁剪(基于裁剪盒)
- What is the difference between level 1, level 2 and level 3 domain names? How to register domain names
- Centos7 deploying mysql-5.7
- Authoritative recognition! Tencent cloud data security Zhongtai was selected as the 2021 pioneer practice case
- The product layout is strengthened, the transformation of digital intelligence is accelerated, and FAW Toyota has hit 2022million annual sales
- How to give full play to the advantages of Internet of things by edge computing intelligent gateway
- About Stacked Generalization
猜你喜欢

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

leetcode:剑指 Offer 26:判断t1中是否含有t2的全部拓扑结构

【JUC系列】Executor框架之CompletionFuture

缓存操作rockscache原理图

Manual for automatic testing and learning of anti stepping pits, one for each tester

Record -- about the method of adding report control to virtual studio2017 -- reportview control

35岁危机?内卷成程序员代名词了

leetcode:1856. 子数组最小乘积的最大值

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

Leetcode: Sword finger offer 26: judge whether T1 contains all topologies of T2
随机推荐
What is Druid
线程安全与实现方法
记录--关于virtual studio2017添加报表控件的方法--Reportview控件
目标5000万日活,Pwnk欲打造下一代年轻人的“迪士尼乐园”
Actual combat | how to deploy flask project using wechat cloud hosting
Centos7 deploying mysql-5.7
What is domain name resolution? How to resolve domain name resolution errors
Do you want to research programming? I got six!
Skips allowed on SAP QM material master inspection type
创客教育给教师发展带来的挑战
Coding platform project construction guide
[5 minutes to play lighthouse] take you to the light kubernetes release k3s
Produce kubeconfig with permission control
How long does the domain name filing take and what materials need to be prepared
Manual for automatic testing and learning of anti stepping pits, one for each tester
Record -- about the method of adding report control to virtual studio2017 -- reportview control
WordPress pill applet build applet from zero to one [pagoda panel environment installation]
Enter the software test pit!!! Software testing tools commonly used by software testers software recommendations
Kangaroo cloud: the overall architecture and key technical points of building a real-time computing platform based on Flink
leetcode:1856. Maximum value of minimum product of subarray