前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 5986. 设置时间的最少代价(枚举)

LeetCode 5986. 设置时间的最少代价(枚举)

作者头像
Michael阿明
发布2022-03-10 18:36:20
3710
发布2022-03-10 18:36:20
举报

文章目录

1. 题目

常见的微波炉可以设置加热时间,且加热时间满足以下条件:

  • 至少为 1 秒钟。
  • 至多为 99 分 99 秒。

你可以 最多 输入 4 个数字 来设置加热时间。 如果你输入的位数不足 4 位,微波炉会自动加 前缀 0 来补足 4 位。 微波炉会将设置好的四位数中,前 两位当作分钟数,后 两位当作秒数。 它们所表示的总时间就是加热时间。比方说:

  • 你输入 9 5 4 (三个数字),被自动补足为 0954 ,并表示 9 分 54 秒。
  • 你输入 0 0 0 8 (四个数字),表示 0 分 8 秒。
  • 你输入 8 0 9 0 ,表示 80 分 90 秒。
  • 你输入 8 1 3 0 ,表示 81 分 30 秒。

给你整数 startAt ,moveCost ,pushCost 和 targetSeconds 。 一开始,你的手指在数字 startAt 处。 将手指移到 任何其他数字 ,需要花费 moveCost 的单位代价。 每 输入你手指所在位置的数字一次,需要花费 pushCost 的单位代价。

要设置 targetSeconds 秒的加热时间,可能会有多种设置方法。 你想要知道这些方法中,总代价最小为多少。

请你能返回设置 targetSeconds 秒钟加热时间需要花费的最少代价。

请记住,虽然微波炉的秒数最多可以设置到 99 秒,但一分钟等于 60 秒。

示例 1:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
输入:startAt = 1, moveCost = 2, pushCost = 1, 
targetSeconds = 600
输出:6
解释:以下为设置加热时间的所有方法。
- 1 0 0 0 ,表示 10 分 0 秒。
  手指一开始就在数字 1 处,输入 1 (代价为 1),
  移到 0 处(代价为 2),
  输入 0(代价为 1),输入 0(代价为 1),输入 0(代价为 1)。
  总代价为:1 + 2 + 1 + 1 + 1 = 6 。这是所有方案中的最小代价。
- 0 9 6 0,表示 9 分 60 秒。它也表示 600 秒。
  手指移到 0 处(代价为 2),输入 0 (代价为 1),
  移到 9 处(代价为 2),输入 9(代价为 1),
  移到 6 处(代价为 2),输入 6(代价为 1),
  移到 0 处(代价为 2),输入 0(代价为 1)。
  总代价为:2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12 。
- 9 6 0,微波炉自动补全为 0960 ,表示 9 分 60 秒。
  手指移到 9 处(代价为 2),输入 9 (代价为 1),
  移到 6 处(代价为 2),输入 6(代价为 1),移到 0 处(代价为 2),
  输入 0(代价为 1)。
  总代价为:2 + 1 + 2 + 1 + 2 + 1 = 9 。

示例 2:

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
输入:startAt = 0, moveCost = 1, pushCost = 2, 
targetSeconds = 76
输出:6
解释:最优方案为输入两个数字 7 6,表示 76 秒。
手指移到 7 处(代价为 1),输入 7 (代价为 2),
移到 6 处(代价为 1),输入 6(代价为 2)。
总代价为:1 + 2 + 1 + 2 = 6
其他可行方案为 0076 ,076 ,0116 和 116 ,但是它们的代价都比 6 大。
 
提示:
0 <= startAt <= 9
1 <= moveCost, pushCost <= 10^5
1 <= targetSeconds <= 6039

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/minimum-cost-to-set-cooking-time 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

代码语言:javascript
复制
class Solution {
public:
    int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
        int m = targetSeconds/60, n = targetSeconds-m*60;
        vector<string> nums;
        if(m < 100) // 注意这个条件 <100
            to_str(m, n, nums);
        if(n+60 <= 99 && m>=1)
            to_str(m-1, n+60, nums);
        int mincost = INT_MAX;
        for(auto& s : nums)
        {
            char start = char(startAt+'0');
            int cost = 0;
            for(auto c :s)
            {
                if(c == start)
                    cost += pushCost;
                else
                {
                    cost += moveCost+pushCost;
                    start = c;
                }
            }
            mincost = min(mincost, cost);
        }
        return mincost;
    }
    void to_str(int m, int n, vector<string>& nums)
    {
        string a = m > 0 ? to_string(m) : ""; // 0不需要按
        string b = "00";
        if(n >= 10) b = to_string(n); // 两位数
        else if(n > 0 && n <= 9 && m > 0) b = "0"+to_string(n);
        // 分钟数不为0,秒钟需要前置0
        else if(n > 0 && n <= 9 && m == 0) b = to_string(n);
        // 分钟数为0,秒钟不需要前置0
        nums.push_back(a+b);
    }
};

0 ms 6 MB C++


我的CSDN博客地址 https://michael.blog.csdn.net/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档