返回介绍

solution / 2100-2199 / 2188.Minimum Time to Finish the Race / README_EN

发布于 2024-06-17 01:03:09 字数 6740 浏览 0 评论 0 收藏 0

2188. Minimum Time to Finish the Race

中文文档

Description

You are given a 0-indexed 2D integer array tires where tires[i] = [fi, ri] indicates that the ith tire can finish its xth successive lap in fi * ri(x-1) seconds.

  • For example, if fi = 3 and ri = 2, then the tire would finish its 1st lap in 3 seconds, its 2nd lap in 3 * 2 = 6 seconds, its 3rd lap in 3 * 22 = 12 seconds, etc.

You are also given an integer changeTime and an integer numLaps.

The race consists of numLaps laps and you may start the race with any tire. You have an unlimited supply of each tire and after every lap, you may change to any given tire (including the current tire type) if you wait changeTime seconds.

Return_ the minimum time to finish the race._

 

Example 1:

Input: tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4
Output: 21
Explanation: 
Lap 1: Start with tire 0 and finish the lap in 2 seconds.
Lap 2: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
Lap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
Total time = 2 + 6 + 5 + 2 + 6 = 21 seconds.
The minimum time to complete the race is 21 seconds.

Example 2:

Input: tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5
Output: 25
Explanation: 
Lap 1: Start with tire 1 and finish the lap in 2 seconds.
Lap 2: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
Lap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
Lap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second.
Total time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds.
The minimum time to complete the race is 25 seconds. 

 

Constraints:

  • 1 <= tires.length <= 105
  • tires[i].length == 2
  • 1 <= fi, changeTime <= 105
  • 2 <= ri <= 105
  • 1 <= numLaps <= 1000

Solutions

Solution 1

class Solution:
  def minimumFinishTime(
    self, tires: List[List[int]], changeTime: int, numLaps: int
  ) -> int:
    cost = [inf] * 18
    for f, r in tires:
      i, s, t = 1, 0, f
      while t <= changeTime + f:
        s += t
        cost[i] = min(cost[i], s)
        t *= r
        i += 1
    f = [inf] * (numLaps + 1)
    f[0] = -changeTime
    for i in range(1, numLaps + 1):
      for j in range(1, min(18, i + 1)):
        f[i] = min(f[i], f[i - j] + cost[j])
      f[i] += changeTime
    return f[numLaps]
class Solution {
  public int minimumFinishTime(int[][] tires, int changeTime, int numLaps) {
    final int inf = 1 << 30;
    int[] cost = new int[18];
    Arrays.fill(cost, inf);
    for (int[] e : tires) {
      int f = e[0], r = e[1];
      int s = 0, t = f;
      for (int i = 1; t <= changeTime + f; ++i) {
        s += t;
        cost[i] = Math.min(cost[i], s);
        t *= r;
      }
    }
    int[] f = new int[numLaps + 1];
    Arrays.fill(f, inf);
    f[0] = -changeTime;
    for (int i = 1; i <= numLaps; ++i) {
      for (int j = 1; j < Math.min(18, i + 1); ++j) {
        f[i] = Math.min(f[i], f[i - j] + cost[j]);
      }
      f[i] += changeTime;
    }
    return f[numLaps];
  }
}
class Solution {
public:
  int minimumFinishTime(vector<vector<int>>& tires, int changeTime, int numLaps) {
    int cost[18];
    memset(cost, 0x3f, sizeof(cost));
    for (auto& e : tires) {
      int f = e[0], r = e[1];
      int s = 0;
      long long t = f;
      for (int i = 1; t <= changeTime + f; ++i) {
        s += t;
        cost[i] = min(cost[i], s);
        t *= r;
      }
    }
    int f[numLaps + 1];
    memset(f, 0x3f, sizeof(f));
    f[0] = -changeTime;
    for (int i = 1; i <= numLaps; ++i) {
      for (int j = 1; j < min(18, i + 1); ++j) {
        f[i] = min(f[i], f[i - j] + cost[j]);
      }
      f[i] += changeTime;
    }
    return f[numLaps];
  }
};
func minimumFinishTime(tires [][]int, changeTime int, numLaps int) int {
  const inf = 1 << 30
  cost := [18]int{}
  for i := range cost {
    cost[i] = inf
  }
  for _, e := range tires {
    f, r := e[0], e[1]
    s, t := 0, f
    for i := 1; t <= changeTime+f; i++ {
      s += t
      cost[i] = min(cost[i], s)
      t *= r
    }
  }
  f := make([]int, numLaps+1)
  for i := range f {
    f[i] = inf
  }
  f[0] = -changeTime
  for i := 1; i <= numLaps; i++ {
    for j := 1; j < min(18, i+1); j++ {
      f[i] = min(f[i], f[i-j]+cost[j])
    }
    f[i] += changeTime
  }
  return f[numLaps]
}
function minimumFinishTime(tires: number[][], changeTime: number, numLaps: number): number {
  const cost: number[] = Array(18).fill(Infinity);
  for (const [f, r] of tires) {
    let s = 0;
    let t = f;
    for (let i = 1; t <= changeTime + f; ++i) {
      s += t;
      cost[i] = Math.min(cost[i], s);
      t *= r;
    }
  }
  const f: number[] = Array(numLaps + 1).fill(Infinity);
  f[0] = -changeTime;
  for (let i = 1; i <= numLaps; ++i) {
    for (let j = 1; j < Math.min(18, i + 1); ++j) {
      f[i] = Math.min(f[i], f[i - j] + cost[j]);
    }
    f[i] += changeTime;
  }
  return f[numLaps];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文