Skip to content

Commit c58f0bd

Browse files
committed
added problem 2187
1 parent 760e9e0 commit c58f0bd

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

Diff for: problems/problem2187/min_all_trips.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package problem2187
2+
3+
/*
4+
You are given an array time where time[i] denotes the time taken by the ith bus to complete one trip.
5+
Each bus can make multiple trips successively; that is, the next trip can start immediately after completing the current trip.
6+
Also, each bus operates independently; that is, the trips of one bus do not influence the trips of any other bus.
7+
You are also given an integer totalTrips, which denotes the number of trips all buses should make in total.
8+
Return the minimum time required for all buses to complete at least totalTrips trips.
9+
*/
10+
11+
func minimumTime(time []int, totalTrips int) int64 {
12+
var left, right int64 = 1, int64(max(time)) * int64(totalTrips) * 10
13+
for left < right {
14+
mid := left + (right-left)/2
15+
if !calcTime(mid, totalTrips, time) {
16+
left = mid + 1
17+
} else {
18+
right = mid
19+
}
20+
}
21+
return left
22+
}
23+
24+
func calcTime(a int64, total int, times []int) bool {
25+
var sum int64
26+
totalLong := int64(total)
27+
for _, t := range times {
28+
sum += a / int64(t)
29+
if sum >= totalLong {
30+
break
31+
}
32+
}
33+
return sum >= totalLong
34+
}
35+
36+
func max(in []int) int {
37+
var res = in[0]
38+
for i := range in {
39+
if in[i] > res {
40+
res = in[i]
41+
}
42+
}
43+
return res
44+
}

Diff for: problems/problem2187/min_all_trips_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem2187
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input []int
12+
Trips int
13+
Expected int64
14+
}
15+
16+
var Results = []Result{
17+
{[]int{1, 2, 3}, 5, 3},
18+
{[]int{2}, 1, 2},
19+
}
20+
21+
func TestMinimumTimeForAllTrips(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
for _, res := range Results {
25+
want := res.Expected
26+
27+
got := minimumTime(res.Input, res.Trips)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
29+
}
30+
}

Diff for: readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ Each problem is in it's own directory, with test files. There are helper package
397397
| 2136 | [Earliest Possible Day of Full Bloom](https://leetcode.com/problems/earliest-possible-day-of-full-bloom) | [My Solution](./problems/problem2136) ||
398398
| 2148 | [Count Elements With Strictly Smaller and Greater Elements](https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements) | [My Solution](./problems/problem2148) ||
399399
| 2165 | [Smallest Value of the Rearranged Number](https://leetcode.com/problems/smallest-value-of-the-rearranged-number/) | [My Solution](./problems/problem2165) ||
400+
| 2187 | [Minimum Time to Complete Trips](https://leetcode.com/problems/minimum-time-to-complete-trips) | [My Solution](./problems/problem2187) ||
400401
| 2225 | [Find Players With Zero or One Losses](https://leetcode.com/problems/find-players-with-zero-or-one-losses/) | [My Solution](./problems/problem2225) ||
401402
| 2244 | [Minimum Rounds to Complete All Tasks](https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks) | [My Solution](./problems/problem2244) ||
402403
| 2246 | [Longest Path With Different Adjacent Characters](https://leetcode.com/problems/longest-path-with-different-adjacent-characters) | [My Solution](./problems/problem0000) ||

0 commit comments

Comments
 (0)