From 0b98cb49b705e23f1fc59e9a49a4411c0231c33c Mon Sep 17 00:00:00 2001 From: Rakshit Gondwal <98955085+rakshitgondwal@users.noreply.github.com> Date: Mon, 3 Oct 2022 17:42:50 +0530 Subject: [PATCH] added solution to q45 --- .../Q45_JumpGame2/Q45_JumpGame2.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 leetCode Solutions/Q45_JumpGame2/Q45_JumpGame2.cpp diff --git a/leetCode Solutions/Q45_JumpGame2/Q45_JumpGame2.cpp b/leetCode Solutions/Q45_JumpGame2/Q45_JumpGame2.cpp new file mode 100644 index 0000000..5b0e8a6 --- /dev/null +++ b/leetCode Solutions/Q45_JumpGame2/Q45_JumpGame2.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int jump(vector& nums) { + // support variables + int len = nums.size(), steps[len]; + // presetting steps + for (int i = 1; i < len; i++) { + steps[i] = INT_MAX; + } + steps[0] = 0; + // populating steps with the lowest possible values + for (int i = 0; i < len; i++) { + for (int lmt = i, j = min(len - 1, i + nums[i]); j > lmt; j--) { + if (steps[j] > steps[i] + 1) steps[j] = steps[i] + 1; + else break; + } + } + return steps[len - 1]; + } +}; \ No newline at end of file