【44】Wildcard Matching 

【45】Jump Game II (2018年11月28日,算法群衍生题)

题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步。

题解:

 

【55】Jump Game (2018年11月27日,算法群)

给了一个数组nums,nums[i] = k 代表站在第 i 个位置的情况下, 我最多能往前走 k 个单位。问我能不能到达最后一个 index。

题解:虽然是贪心分类,我还是用dp解了。dp[i] 代表我能不能到达第 i 个位置。 

 1 class Solution {
 2 public:
 3     bool canJump(vector<int>& nums) {
 4         const int n = nums.size();
 5         if (n == 0) {return false;}
 6         vector<int> f(n, 0); //f[i] 代表第i个index是不是可达
 7         f[0] = 1;
 8         for (int i = 0; i < n; ++i) {
 9             if (f[i]) {
10                 const int k = nums[i];
11                 for (int j = 1; j <= k; ++j) {
12                     if (i+j >= n) {break;}
13                     f[i+j] = 1;
14                 }
15             }
16         }
17         return f[n-1] == 1;
18     }
19 };
View Code

相关文章:

  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-10-21
  • 2022-12-23
  • 2022-01-27
  • 2019-03-04
猜你喜欢
  • 2022-01-14
  • 2021-08-09
  • 2022-12-23
  • 2021-10-16
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案