commit 79f04f1e7c8a523b9946aa34eb26030b99c3e4d5 parent d637f07e6e8ba2c95b64b031e61cbfd174566213 Author: Dimitrije Dobrota <mail@dimitrijedobrota.com> Date: Tue, 24 Jan 2023 13:41:23 +0100 Dynamic Programming I: Day 4 Diffstat:
A | Problems/0045.cpp | | | 14 | ++++++++++++++ |
M | README.md | | | 1 | + |
2 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/Problems/0045.cpp b/Problems/0045.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int jump(vector<int> &nums) { + int n = nums.size(), limit = 0; + vector<int> num(n); + num[0] = 0; + for (int i = 0; i <= limit && i < n; i++) { + for (int j = limit + 1; j <= i + nums[i] && j < n; j++) + num[j] = num[i] + 1; + limit = max(limit, i + nums[i]); + } + return num.back(); + } +}; diff --git a/README.md b/README.md @@ -44,6 +44,7 @@ for solving problems. | 0036 | Medium | [Valid Sudoku](Problems/0036.cpp) | | 0042 | Medium | [Trapping Rain Water](Problems/0011.cpp) | | 0043 | Medium | [Multiply Strings](Problems/0043.cpp) | +| 0045 | Medium | [Jump Game II](Problems/0045.cpp) | | 0049 | Medium | [Group Anagrams](Problems/0049.cpp) | | 0053 | Medium | [Maximum Subarray](Problems/0053.cpp) | | 0054 | Medium | [Spiral Matrix](Problems/0054.cpp) |