Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of realizing the jumping game on C++

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "C++ to achieve jumping game method", the explanation content in the article is simple and clear, easy to learn and understand, please follow the small series of ideas slowly in-depth, together to study and learn "C++ to achieve jumping game method" bar!

Jump Game II Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

Example:

Input: [2,3,1,1,4]

Output: 2

Explanation: The minimum number of jumps to reach the last index is 2.

Jump 1 step from index 0 to 1, then 3 steps to the last index.

Note:

You can assume that you can always reach the last index.

This question is an extension of the previous Jump Game, which asks whether it can reach the last number, and this question only asks for the minimum number of jumps to reach the last position, which seems to be the default to reach the last position. The core method of this problem is to use Greedy's idea to solve it. Think about why? In order to jump to the end faster, want to know the range of each step can jump, here greedy is not to jump in the range of the farthest jump force to choose the position, because this selection is not necessarily the optimal solution, so it feels a bit different from greedy algorithm. In fact, greedy here is the farthest range that can be reached, traversing all the positions that can be jumped to at the current time, and then predicting the farthest distance that can be jumped to next according to the jumping force at this position, greedy for a farthest range, once this range reaches the end, the current number of steps must be the minimum number of steps. Two variables cur and pre are needed to store the current farthest position that can be reached and the farthest position that can be reached before, respectively. As long as cur does not reach the last position, the loop continues. Pre is assigned to cur first, indicating the farthest position that can be reached after the previous loop. If the current position i is less than or equal to pre, it indicates that it is still within the range that can be reached by the previous jump. Update cur according to the current position and jump force. The method of updating cur is to compare the larger value of the current cur and i + A[i]. If the title does not indicate whether it can reach the end, it can also be judged whether pre and cur are equal at this time. If they are equal, it means that cur has not been updated, that is, it cannot reach the end position, and returns-1. The code is as follows:

Solution 1:

class Solution {public: int jump(vector& nums) { int res = 0, n = nums.size(), i = 0, cur = 0; while (cur < n - 1) { ++res; int pre = cur; for (; i = n - 1) break; } } return res; }}; Thank you for reading, the above is the "C++ to achieve the method of jumping games" content, after the study of this article, I believe that we have a deeper understanding of the C++ to achieve the method of jumping games, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report