返回介绍

solution / 2100-2199 / 2187.Minimum Time to Complete Trips / README_EN

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

2187. Minimum Time to Complete Trips

中文文档

Description

You are given an array time where time[i] denotes the time taken by the ith bus to complete one trip.

Each bus can make multiple trips successively; that is, the next trip can start immediately after completing the current trip. Also, each bus operates independently; that is, the trips of one bus do not influence the trips of any other bus.

You are also given an integer totalTrips, which denotes the number of trips all buses should make in total. Return _the minimum time required for all buses to complete at least _totalTrips_ trips_.

 

Example 1:

Input: time = [1,2,3], totalTrips = 5
Output: 3
Explanation:
- At time t = 1, the number of trips completed by each bus are [1,0,0]. 
  The total number of trips completed is 1 + 0 + 0 = 1.
- At time t = 2, the number of trips completed by each bus are [2,1,0]. 
  The total number of trips completed is 2 + 1 + 0 = 3.
- At time t = 3, the number of trips completed by each bus are [3,1,1]. 
  The total number of trips completed is 3 + 1 + 1 = 5.
So the minimum time needed for all buses to complete at least 5 trips is 3.

Example 2:

Input: time = [2], totalTrips = 1
Output: 2
Explanation:
There is only one bus, and it will complete its first trip at t = 2.
So the minimum time needed to complete 1 trip is 2.

 

Constraints:

  • 1 <= time.length <= 105
  • 1 <= time[i], totalTrips <= 107

Solutions

Solution 1

class Solution:
  def minimumTime(self, time: List[int], totalTrips: int) -> int:
    mx = min(time) * totalTrips
    return bisect_left(
      range(mx), totalTrips, key=lambda x: sum(x // v for v in time)
    )
class Solution {
  public long minimumTime(int[] time, int totalTrips) {
    int mi = time[0];
    for (int v : time) {
      mi = Math.min(mi, v);
    }
    long left = 1, right = (long) mi * totalTrips;
    while (left < right) {
      long cnt = 0;
      long mid = (left + right) >> 1;
      for (int v : time) {
        cnt += mid / v;
      }
      if (cnt >= totalTrips) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  }
}
class Solution {
public:
  long long minimumTime(vector<int>& time, int totalTrips) {
    int mi = *min_element(time.begin(), time.end());
    long long left = 1, right = (long long) mi * totalTrips;
    while (left < right) {
      long long cnt = 0;
      long long mid = (left + right) >> 1;
      for (int v : time) cnt += mid / v;
      if (cnt >= totalTrips)
        right = mid;
      else
        left = mid + 1;
    }
    return left;
  }
};
func minimumTime(time []int, totalTrips int) int64 {
  left, right := 1, slices.Min(time)*totalTrips
  for left < right {
    mid := (left + right) >> 1
    cnt := 0
    for _, v := range time {
      cnt += mid / v
    }
    if cnt >= totalTrips {
      right = mid
    } else {
      left = mid + 1
    }
  }
  return int64(left)
}

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

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

发布评论

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