From 35a5e9ea63e03231db3f776125b7f3d0cf10a46c Mon Sep 17 00:00:00 2001 From: Pranjal Date: Tue, 11 Oct 2016 03:06:52 +0530 Subject: [PATCH] Jump Game II --- JumpGameII.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 JumpGameII.cpp diff --git a/JumpGameII.cpp b/JumpGameII.cpp new file mode 100644 index 0000000..e606c59 --- /dev/null +++ b/JumpGameII.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int jump(vector& nums) { + int n = nums.size(); + + if(n == 0 || n == 1){ + return 0; + } + + vector temp(n, INT_MAX); + temp[0] = 0; + int start = 0; + int i = 1; + + while(i < n){ + while(nums[start] - i + start < 0 && start < i){ + start++; + } + if(start == i){ + break; + } + temp[i] = min(temp[i], temp[start] + 1); + i++; + } + + return temp[n-1]; + } +};