设置时间的最少代价
难度:
标签:
题目描述
常见的微波炉可以设置加热时间,且加热时间满足以下条件:
- 至少为
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:
输入: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:
输入: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 <= 105
1 <= targetSeconds <= 6039
代码结果
运行时间: 27 ms, 内存: 16.0 MB
/*
* Solution Approach using Java Streams:
* 1. Convert the target seconds into minutes and seconds.
* 2. Generate all possible time representations in MMSS format using IntStream.
* 3. Calculate the cost for each representation using a custom method.
* 4. Return the minimum cost among all representations.
*/
import java.util.stream.IntStream;
public class MicrowaveCostStream {
public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
return IntStream.rangeClosed(0, 99).boxed()
.flatMap(m -> IntStream.rangeClosed(0, 99)
.filter(s -> m * 60 + s == targetSeconds)
.mapToObj(s -> String.format("%02d%02d", m, s)))
.mapToInt(time -> calculateCost(startAt, moveCost, pushCost, time))
.min().orElse(Integer.MAX_VALUE);
}
private int calculateCost(int startAt, int moveCost, int pushCost, String time) {
int cost = 0;
int currentPos = startAt;
for (char c : time.toCharArray()) {
int digit = c - '0';
if (digit != currentPos) {
cost += moveCost;
currentPos = digit;
}
cost += pushCost;
}
return cost;
}
}
解释
方法:
本题的目的是找到设置指定秒数的最小代价。由于输入的数字被转换为分钟和秒,因此有多种方式将秒数转换为微波炉的输入。我们需要计算每种方式的代价,并找出其中的最小值。首先,我们定义一个计算输入字符串代价的辅助函数 calc(s),它计算从开始数字到输入完整个字符串的总代价。然后,根据 targetSeconds,我们考虑以下几种可能的输入方式:1) 直接将秒数转为分钟和秒(如果在可表示范围内),例如 sec = 350,则表示为 '5' '50'。2) 如果 sec < 100,可以直接输入秒数,例如 sec = 76,则表示为 '76'。3) 如果 sec % 60 < 40,可以将一分钟借给秒数,例如 sec = 95,则表示为 '1' '35'(即从 95 秒借 1 分钟变成 35 秒)。最后,我们比较这些可能性的总代价,返回最小值。
时间复杂度:
O(1)
空间复杂度:
O(1)
代码细节讲解
🦆
解题思路中提到,如果秒数小于100可以直接输入秒数。但是如果秒数恰好是两位数,例如`95`,这种情况下的输入方式是否更优于其他可能的分钟和秒数组合?
▷🦆
在将秒数借用一分钟的情况下,解题思路中提到如果`sec % 60 < 40`,才会考虑这种方法。为何选择40作为阈值,而不是其他数字?
▷🦆
题解中提到的代价计算函数`calc(s)`,是否考虑了连续输入同一个数字时,移动代价应该为0的情况?
▷🦆
在计算最小总代价时,题解考虑了三种输入方式。但如果秒数恰好是6000秒,即最大限制,这种情况应该如何处理?
▷