当前位置:网站首页>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
边栏推荐
- On BOM and DOM (4): dom0/dom2 event handling analysis
- 【二叉树】——二叉树中序遍历
- What is the role of website domain name
- In the half year, there were 2.14 million paying users, a year-on-year increase of 62.5%, and New Oriental online launched its private domain
- CloudCompare&PCL 点云裁剪(基于裁剪盒)
- 缓存操作rockscache原理图
- 项目Demo
- About Stacked Generalization
- Kubernets traifik proxy WS WSS application
- C language student management system - can check the legitimacy of user input, two-way leading circular linked list
猜你喜欢

leetcode:85. Max rectangle

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

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

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

数据库 存储过程 begin end

leetcode:85. 最大矩形

Leetcode: Sword finger offer 26: judge whether T1 contains all topologies of T2

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

The product layout is strengthened, the transformation of digital intelligence is accelerated, and FAW Toyota has hit 2022million annual sales

记录--关于JSP前台传参数到后台出现乱码的问题
随机推荐
typescript vscode /bin/sh: ts-node: command not found
Reasons for automatic allocation failure of crawler agent IP
leetcode:85. Max rectangle
Deploy DNS server using dnsmasq
Urban Waterlogging Monitoring and early warning system
How does easyplayer RTSP configure sending heartbeat information to the server?
The new version of Tencent Youtu ncnn is suitable for domestic CPUs, and the maximum speed is increased by 70 times
Leetcode: Sword finger offer 26: judge whether T1 contains all topologies of T2
Spirit information development log (3)
Distributed cache breakdown
I want to say "three no" to digital transformation
On BOM and DOM (1): overview of BOM and DOM
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!
Word cannot copy and paste processing method
C language student management system - can check the legitimacy of user input, two-way leading circular linked list
Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.
How accurate are the two common methods of domain name IP query
Domain name, resolution, SSL certificate FAQ
What I regret most when I learn programming!
What is domain name resolution? How to resolve domain name resolution errors