题目地址:

https://leetcode.com/problems/minimum-sideway-jumps/description/

给定一条宽为333的路,可以视为三条平行线,给定一个长n+1n+1n+1的数组AAA,A[i]=0A[i]=0A[i]=0表示位置iii无障碍物,A[i]=k>0A[i]=k>0A[i]=k>0表示位置iii的第kkk条线有障碍物。一只青蛙在位置000的第二条平行线出发,可以以代价000向右跳一步,也可以以代价111变线,只是不能跳到障碍物上。题目保证A[0]=A[n]=0A[0]=A[n]=0A[0]=A[n]=0,问要跳到位置nnn的最小代价。

可以将所有位置看成是图上的点,代价视为边权,则代价只有0,10,10,1两种,从而是一个010101图上的最短路问题,可以用双端队列BFS来做,参考https://blog.csdn.net/qq_46105170/article/details/114667171。代码如下:

class Solution {public:using PII = pair<int, int>;int minSideJumps(vector<int>& obstacles) {int n = obstacles.size() - 1;deque<PII> dq;dq.push_back({0, 2});bool vis[n + 1][3];memset(vis, 0, sizeof vis);int dist[n + 1][3];memset(dist, 0x3f, sizeof dist);dist[0][1] = 0;while (dq.size()) {auto t = dq.front();dq.pop_front();int x = t.first, y = t.second;if (x == n) return dist[x][y - 1];if (vis[x][y - 1]) continue;vis[x][y - 1] = true;if (obstacles[x + 1] != y && !vis[x + 1][y - 1] &&dist[x + 1][y - 1] > dist[x][y - 1]) {dist[x + 1][y - 1] = dist[x][y - 1];dq.push_back({x + 1, y});}for (int k = 1; k <= 3; k++)if (k != y && k != obstacles[x] && !vis[x][k - 1] &&dist[x][k - 1] > dist[x][y - 1] + 1) {dist[x][k - 1] = dist[x][y - 1] + 1;dq.push_back({x, k});}}return -1;}
};

时空复杂度O(n)O(n)O(n)。

【Leetcode】1824. Minimum Sideway Jumps相关推荐

  1. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  2. 【leetcode】1007. Minimum Domino Rotations For Equal Row

    题目如下: In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  ( ...

  3. 【leetcode】931. Minimum Falling Path Sum

    题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...

  4. 【Leetcode】1526. Minimum Number of Increments on Subarrays to Form a Target Array(配数学证明)

    题目地址: https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array ...

  5. 【Leetcode】1335. Minimum Difficulty of a Job Schedule

    题目地址: https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/ 给定一个长nnn正整数数组AAA和一个正整数ddd, ...

  6. 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

  7. 【Leetcode】871. Minimum Number of Refueling Stops

    题目地址: https://leetcode.com/problems/minimum-number-of-refueling-stops/description/ 有一个小车,初始位置在 0 0 0 ...

  8. python棋盘最短路径_【leetcode】64. Minimum Path Sum 棋盘最短路径

    1. 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  9. 1824. Minimum Sideway Jumps 贪心和DP方法

    贪心 如果下一个位置i+1有障碍的话,从当前位置i进行跳跃就有两种情况 只有一个选择 即当前i已经有了一个障碍,算上下一个的障碍,3-2=1 只需要选择剩下唯一的空跳即可 有两个选择 这时候就要判断该 ...

最新文章

  1. 一款零注解侵入的 API 文档生成工具,你用过吗?
  2. 人工智能、大数据的广泛应用,算法推荐如何守好边界
  3. info replication
  4. sharepoint中一些gridview的使用方法
  5. 文本分类入门(二)文本分类的方法
  6. chrome webdriver_网络爬虫之使用pyppeteer替代selenium完美绕过webdriver检测 阅读目录
  7. hive的一些调优参数
  8. 怎样判断一个网站是不是前后端分离的?
  9. c语言中time 0 返回值,clock()返回值为什么是0
  10. 记录一次linux病毒清除过程
  11. Redis key过期策略
  12. 红米k30pro工程包工厂包
  13. 推流是什么,直播为什么要推流
  14. python word保存图_使用python matplotlib 画图导入到word中如何保证分辨率
  15. ERP是什么?ERP管理系统怎么用?
  16. absolute和fixed的区别
  17. 稳恒(有人透传云)NBIOT模块使用指南(2)
  18. 2021CCPC网络赛榜单
  19. oracle数据库资源高,~Oracle后台进程占用资源过高可能会是什么原因?
  20. 2014 破旧立新,重新启程

热门文章

  1. 求职简历-NLP工程师
  2. 看日光穿过手指 享受芬芳的下午茶
  3. 2011年RSA中国大会报名注册正式启动
  4. Decenber 2004 (再见了我的2004猴年)
  5. 斐波纳契回调线_斐波那契回调线(黄金分割线)全面解析
  6. 薛开宇caffe学习笔记的补充笔记3
  7. MPU6050快速开发使用
  8. RFID物流配送智慧管理解决方案
  9. resip学习笔记之句柄Handle
  10. 独立开发者如何笑傲江湖