From 9f542b4bee2bdc8dc6a3744e1a984ee8ee2fe6d3 Mon Sep 17 00:00:00 2001 From: chandrachood932c <108889158+chandrachood932c@users.noreply.github.com> Date: Mon, 31 Oct 2022 19:40:55 +0530 Subject: [PATCH] Create Jump Game II.cpp You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0]. Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where: 0 <= j <= nums[i] and i + j < n Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1]. --- Jump Game II.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Jump Game II.cpp diff --git a/Jump Game II.cpp b/Jump Game II.cpp new file mode 100644 index 0000000..970eff7 --- /dev/null +++ b/Jump Game II.cpp @@ -0,0 +1,30 @@ +/* +You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0]. + +Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where: + +0 <= j <= nums[i] and +i + j < n +Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1]. + + */ + +class Solution { +public: + int jump(vector& nums) { + int n=nums.size(); + vector dp(n,INT_MAX); + dp[0]=0; + + for(int i=0;i