[LeetCode]Jump Game

本文探讨了一个经典的编程问题——能否从数组起始位置跳跃至末尾。通过递归方法尝试解决,但遭遇超时问题。最终采用迭代方法,记录可达最远位置,实现高效判断。

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.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

这次挑了一道游戏性质的题来做,看到这道题首先想到的就是枚举,像这种形式明显会想到递归,于是就写了个递归的代码。

(突然想到有个遍历字符串的题,有空做一下)

#include <iostream>

class Solution {
public:
	bool canJump(int A[], int n) {
		int index = 0;
		if (jump(A, n, 0))
			return true;
		else
			return false;
	}
	bool jump(int A[], int n, int index)
	{
		if (index >= n - 1)
			return true;
		for (int i = 1; i <= A[index]; i++)
		{
			if (jump(A, n, index + i))
				return true;
		}
		return false;
	}
};

int main()
{
	Solution sl;
	int A1[5] = { 2, 3, 1, 1, 4 };
	int A2[5] = {3, 2, 1, 0, 4};
	std::cout <<sl.canJump(A1, 5) << std::endl;
	std::cout << sl.canJump(A2, 5) << std::endl;

	system("PAUSE");
	return 0;
}

代码看起来是对的,提交到OJ以后出现了Time Limit Exceeded,然后给出了这时的测试用例

Last executed input: [2,0,6,9,8,4,5,0,8,9,1,2,9,6,8,8,0,6,3,1,2,2,1,2,6,5,3,1,2,2,6,4,2,4,3,0,0,0,3,8,2,4,0,1,2,0,1,4,6,5,8,0,7,9,3,4,6,6,5,8,9,3,4,3,7,0,4,9,0,9,8,4,3,0,7,7,1,9,1,9,4,9,0,1,9,5,7,7,1,5,8,2,8,2,6,8,2,2,7,5,1,7,9,6]

这个看起来不能用递归了,得找另一种方法。

OK,另一种方法来了,其实这个问题可以是检查末尾点是否可以到达的问题,那么我们可以先检查[0, n-2]点中是否有点可以直接到达n-1,如果不是点0,那么检查这些能到达n-1的是否可以到达,如点k是否可以到达就检查[0, k-1]是否存在点可以到达。依然是递归方式:

class Solution {
public:
	bool canJump(int A[], int n) {
		if (CheckPointPossible(A, n - 1))
			return true;
		else
			return false;
	}
	//检查此点是否可以到达
	bool CheckPointPossible(int A[], int index)
	{
		if (index == 0)
		{
			return true;
		}
		for (int i = 0; i <= index - 1; i++)
		{
			if (A[i] >= index - i)
			{
				if (CheckPointPossible(A, i))
					return true;
			}
		}
		return false;
	}
};

居然又是 Time Limit Exceeded,好吧,看来这些点能节省的时间有限,得换种方法。


刚才看了一下discussion,有个comment里面的方法实在是醍醐灌顶啊!最近真的是得充值智商了,那么明显的方法搞得那么复杂。

我们只需要从0开始记录这个点能到达的最远点即i+A[i],当然,如果i+A[i]>=n-1了,自然就OK了,否则就在这段能到达距离内再次寻找,不断更新能到达的最远距离。

代码非常简洁明了!感谢discussion里的Mike


class Solution{
public:
	bool canJump(int A[], int n) {
		int canreach = 0;
		for (int i = 0; i < n - 1 && i <= canreach; i++)
		{
			if (i + A[i] >= n - 1)
				return true;
			if (i + A[i] > canreach)
				canreach = i + A[i];
		}
		return (canreach >= n - 1);
	}
};



### LeetCode Top 100 Popular Problems LeetCode provides an extensive collection of algorithmic challenges designed to help developers prepare for technical interviews and enhance their problem-solving skills. The platform categorizes these problems based on popularity, difficulty level, and frequency asked during tech interviews. The following list represents a curated selection of the most frequently practiced 100 problems from LeetCode: #### Array & String Manipulation 1. Two Sum[^2] 2. Add Two Numbers (Linked List)[^2] 3. Longest Substring Without Repeating Characters #### Dynamic Programming 4. Climbing Stairs 5. Coin Change 6. House Robber #### Depth-First Search (DFS) / Breadth-First Search (BFS) 7. Binary Tree Level Order Traversal[^3] 8. Surrounded Regions 9. Number of Islands #### Backtracking 10. Combination Sum 11. Subsets 12. Permutations #### Greedy Algorithms 13. Jump Game 14. Gas Station 15. Task Scheduler #### Sliding Window Technique 16. Minimum Size Subarray Sum 17. Longest Repeating Character Replacement #### Bit Manipulation 18. Single Number[^1] 19. Maximum Product of Word Lengths 20. Reverse Bits This list continues up until reaching approximately 100 items covering various categories including but not limited to Trees, Graphs, Sorting, Searching, Math, Design Patterns, etc.. Each category contains multiple representative questions that cover fundamental concepts as well as advanced techniques required by leading technology companies when conducting software engineering candidate assessments. For those interested in improving logical thinking through gaming activities outside traditional study methods, certain types of video games have been shown beneficial effects similar to engaging directly within competitive coding platforms [^4]. --related questions-- 1. How does participating in online coding competitions benefit personal development? 2. What specific advantages do DFS/BFS algorithms offer compared to other traversal strategies? 3. Can you provide examples illustrating how bit manipulation improves performance efficiency? 4. In what ways might regular participation in programming contests influence job interview success rates? 5. Are there any notable differences between solving problems on paper versus implementing solutions programmatically?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值