最小跳数
Description:
描述:
This problem is a standard interview problem which has been featured in interview rounds of Adobe, Amazon, Oyo rooms etc.
此問題是標(biāo)準(zhǔn)的采訪問題,已在Adobe,Amazon,Oyo房間等的采訪回合中出現(xiàn)。
Problem statement:
問題陳述:
Given an array of integers where each element represents the max number of steps that can be made forward from that element. The task is to find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then there is no way to move from there.
給定一個整數(shù)數(shù)組,其中每個元素代表可以從該元素進(jìn)行的最大步數(shù)。 任務(wù)是找到到達(dá)數(shù)組末尾(從第一個元素開始)的最小跳轉(zhuǎn)數(shù)。 如果元素為0,則無法從那里移動。
Input:The first line is T, total number of test cases. In the following 2*T lines,Each test case has two lines.First line is a number n denoting the size of the array.Next line contains the sequence of integers a1,?a2,?...,?anOutput:For each test case, in a new line, print the minimum number of jumps. If it's not possible to reach the end the print -1Example:
例:
Input:1111 3 5 3 1 2 6 0 6 2 4Output:4Explanation:
說明:
Let's first use greedy method.According to greedy,At first jump, it will move from index 0 to 1 (arr[0]=1, so only 1 step jump is allowed)At second jump,It will move from index 1 to 4 (arr[1]=3, so only 3 step jump is allowed)At third jump,it will move from index 4 to 6 (arr[4]=2, so only 2 step jump is allowed)Now arr[6]=0, so no more jump possible. It would result -1.But, it's not the result. Thus, greedy fails.Since, the array value is maximum jump possible we have to check for all options up to maximum jumps. Obviously, a recursive function which would generate many overlapping sub problems. Let's check how we can solve the above example using recursion.
由于數(shù)組值是可能的最大跳轉(zhuǎn),因此我們必須檢查所有選項,直到最大跳轉(zhuǎn)為止。 顯然,遞歸函數(shù)會產(chǎn)生許多重疊的子問題。 讓我們檢查一下如何使用遞歸解決上面的示例。
After first jump (in this case only one move possible)
第一次跳動后(在這種情況下,只能移動一次)
After second jump (I only took the best child (on solution path) of the recursion tree, you can try all)
第二次跳轉(zhuǎn)后(我只帶了遞歸樹上最好的孩子(在解決方案路徑上),可以嘗試全部)
After third jump (I only took the best child (on solution path) of the recursion tree, you can try all)
第三次跳轉(zhuǎn)后(我只帶了遞歸樹上最好的孩子(在解決方案路徑上),可以嘗試全部)
After fourth jump (That's end)
第四跳之后(結(jié)束)
So, answer is 4.
答案是4。
Let's check the DP approach to solve the above problem.
讓我們檢查一下DP方法來解決上述問題。
Solution approach:
解決方法:
1) If the starting index has value 0 then we can't reach the end if the array size is more than 1, so return -1.2) If array size is 1, we are at the end already, so return 1 which is minimum jump.3) Initialize DP[n] with all elements having value INT_MAX4) for i=0 to n-1for j=i+1 to maximum of(n-1,j+arr[i])//i.e,last index or upto theindex maximum jump can reachdp[j]=minimum(dp[j],dp[i]+1)where dp[i]+1=one jump added with jumps required to reach index i end forend for// not updated in the loop refers that reach end is not possible5) if dp[n-1]==INT_MAX return -1elsereturn dp[n-1]C++ Implementation:
C ++實現(xiàn):
#include <bits/stdc++.h> using namespace std;int min(int x,int y){return (x<y)?x:y; }int minimumjumps(vector<int> arr,int n){if(n==1)return 1;if(arr[0]==0)return -1;int dp[n];for(int i=0;i<n;i++)dp[i]=INT_MAX;dp[0]=0;for(int i=0;i<n;i++){for(int j=i+1;j<=((i+arr[i])>(n-1)?(n-1):(i+arr[i]));j++){dp[j]=min(dp[j],dp[i]+1);}}return dp[n-1]; }int main() {int t,n,item;cout<<"output -1 if reaching end not possible\n";cout<<"Enter number of testcases\n";scanf("%d",&t);for(int i=0;i<t;i++){cout<<"Enter array size\n";scanf("%d",&n);vector<int> a;cout<<"Enter elements:\n";for(int j=0;j<n;j++){scanf("%d",&item);a.push_back(item);}int result=minimumjumps(a,n);cout<<"Result is: ";if(result==INT_MAX)cout<<-1<<endl;elsecout<<result<<endl;}return 0; }Output
輸出量
output -1 if reaching end not possible Enter number of testcases 1 Enter array size 11 Enter elements: 1 3 5 3 1 2 6 0 6 2 4 Result is: 4翻譯自: https://www.includehelp.com/icp/minimum-number-of-jumps.aspx
總結(jié)
- 上一篇: Java Double类hashCode
- 下一篇: count函数里加函数_PHP coun