1824. Minimum Sideway Jumps 贪心和DP方法

贪心

  • 如果下一个位置i+1有障碍的话,从当前位置i进行跳跃就有两种情况
    • 只有一个选择
      • 即当前i已经有了一个障碍,算上下一个的障碍,3-2=1 只需要选择剩下唯一的空跳即可
    • 有两个选择
      • 这时候就要判断该选哪个了
      • 方法就是贪心。分别看两个选择往后,在不跳跃的情况下,最远能走多久
  • 时间复杂度 O ( n 2 ) O(n^2) O(n2)
class Solution {
public:
    int minSideJumps(vector<int> &ob) {
        int len = ob.size();
        int res = 0;
        int num = 2;
        for (int i = 0; i < len; i++) {
            if (ob[i + 1] == num) {
                set<int> choice = {1, 2, 3};
                choice.erase(num);
                choice.erase(ob[i]);
                if (choice.size() == 1) {
                    num = *choice.begin();
                    res++;
                } else {
                    map<int, int> cntLen;
                    int a[6], t = 0;
                    for (auto &e: choice) {
                        int tmp = i;
                        while (tmp < len && ob[tmp] != e) tmp++;
                        cntLen[tmp] = e;
                        a[t++] = tmp;
                    }
                    num = cntLen[max(a[0], a[1])];
                    res++;
                }
            }
        }
        return res;
    }
};

DP

  • 本来是 dp[i][1…3]
    • i表示当前位置,3表示三个赛道
    • dp[i][j] 表示到达第i个位置的第j个赛道,需要跳跃的最小次数
    • 状态转移:
      d p [ i ] [ j ] = m i n ( d p [ i − 1 ] [ j ] , d p [ i − 1 ] [ j 2 ] + 1 , d p [ i − 1 ] [ j 3 + 1 ] dp[i][j] = min(dp[i-1][j], dp[i-1][j_2] + 1, dp[i-1][j_3 + 1] dp[i][j]=min(dp[i1][j],dp[i1][j2]+1,dp[i1][j3+1]
  • 后来发现 dp[i][j] 仅仅和 dp[i -1][j] 有关,因此可以只用一维数组来做
  • 时间复杂度 O ( n ) O(n) O(n)
class Solution {
public:
    int minSideJumps(vector<int> &ob) {
        int len = ob.size();
        int dp[4];
        fill(dp, dp + 4, 1);
//        cout << dp[1] << " ";
        dp[2] = 0;
        for (int i = 1; i < len; i++) {

            int pre1 = dp[1];
            int pre2 = dp[2];
            int pre3 = dp[3];
            fill(dp, dp + 4, 0x7f7f7f);
            if (ob[i] != 1) dp[1] = pre1;
            if (ob[i] != 2) dp[2] = pre2;
            if (ob[i] != 3) dp[3] = pre3;

            if (ob[i] != 1) dp[1] = min(dp[1], min(dp[2], dp[3]) + 1);
            if (ob[i] != 2) dp[2] = min(dp[2], min(dp[1], dp[3]) + 1);
            if (ob[i] != 3) dp[3] = min(dp[3], min(dp[2], dp[1]) + 1);
        }
        return min(dp[1], min(dp[2], dp[3]));
    }
};
npm WARN deprecated stable@0.1.8: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility npm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecated npm WARN deprecated @hapi/bourne@1.3.2: This version has been deprecated and is no longer supported or maintained npm WARN deprecated @hapi/topo@3.1.6: This version has been deprecated and is no longer supported or maintained npm WARN deprecated eslint-loader@2.2.1: This loader has been deprecated. Please use eslint-webpack-plugin npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated npm WARN deprecated har-validator@5.1.5: this library is no longer supported npm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecated npm WARN deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies npm WARN deprecated chokidar@2.1.8: Chokidar 2 does not receive security updates since 2019. Upgrade to chokidar 3 with 15x fewer dependencies npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated npm WARN deprecated html-webpack-plugin@3.2.0: 3.x is no longer supported npm WARN deprecated @hapi/address@2.1.4: Moved to 'npm install @sideway/address' npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead. npm WARN deprecated babel-eslint@10.1.0: babel-eslint is now @babel/eslint-parser. This package will no longer receive updates. npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details. npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142 npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained npm WARN deprecated @hapi/joi@15.1.1: Switch to 'npm install joi' npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x. npm WARN deprecated core-js@2.6.12: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js. added 1400 packages in 1m
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值