Skip to content

Commit dea452c

Browse files
committed
Solution of divide array into group with min sum group questions
1 parent 5eee547 commit dea452c

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

1000-1100q/1014.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
'''
2+
A conveyor belt has packages that must be shipped from one port to another within D days.
3+
4+
The i-th package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
5+
6+
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.
7+
8+
9+
10+
Example 1:
11+
12+
Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5
13+
Output: 15
14+
Explanation:
15+
A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
16+
1st day: 1, 2, 3, 4, 5
17+
2nd day: 6, 7
18+
3rd day: 8
19+
4th day: 9
20+
5th day: 10
21+
22+
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
23+
Example 2:
24+
25+
Input: weights = [3,2,2,4,1,4], D = 3
26+
Output: 6
27+
Explanation:
28+
A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
29+
1st day: 3, 2
30+
2nd day: 2, 4
31+
3rd day: 1, 4
32+
33+
Note:
34+
35+
1 <= D <= weights.length <= 50000
36+
1 <= weights[i] <= 500
37+
38+
'''
39+
40+
41+
class Solution(object):
42+
def shipWithinDays(self, weights, D):
43+
"""
44+
:type weights: List[int]
45+
:type D: int
46+
:rtype: int
47+
"""
48+
high, low = sum(weights)+1, max(weights)
49+
50+
while(low < high):
51+
mid = (high+low)/2
52+
temp_left = mid
53+
packet_at_left = D-1
54+
for weight in weights:
55+
if weight <= mid:
56+
if temp_left < weight:
57+
if packet_at_left == 0:
58+
low = mid+1
59+
break
60+
packet_at_left -= 1
61+
temp_left = mid-weight
62+
else:
63+
temp_left -= weight
64+
else:
65+
high = mid
66+
67+
return low
68+
69+
70+
class Solution(object):
71+
def shipWithinDays(self, weights, D):
72+
"""
73+
:type weights: List[int]
74+
:type D: int
75+
:rtype: int
76+
"""
77+
left, right = max(weights), sum(weights)
78+
79+
while left < right:
80+
curr_sum, groups, invalid = 0, 0, True
81+
mid = left + ((right-left) >> 1)
82+
for weight in weights:
83+
if weight > mid:
84+
invalid = False
85+
break
86+
if curr_sum + weight > mid:
87+
groups += 1
88+
curr_sum = 0
89+
curr_sum += weight
90+
if invalid and groups < D:
91+
right = mid
92+
else:
93+
left = mid + 1
94+
return left

400-500Q/410.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'''
2+
Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.
3+
4+
Note:
5+
If n is the length of array, assume the following constraints are satisfied:
6+
7+
1 ≤ n ≤ 1000
8+
1 ≤ m ≤ min(50, n)
9+
Examples:
10+
11+
Input:
12+
nums = [7,2,5,10,8]
13+
m = 2
14+
15+
Output:
16+
18
17+
18+
Explanation:
19+
There are four ways to split nums into two subarrays.
20+
The best way is to split it into [7,2,5] and [10,8],
21+
where the largest sum among the two subarrays is only 18.
22+
'''
23+
24+
class Solution(object):
25+
def splitArray(self, nums, m):
26+
"""
27+
:type nums: List[int]
28+
:type m: int
29+
:rtype: int
30+
"""
31+
32+
left, right = max(nums), sum(nums)
33+
34+
while left < right:
35+
mid = left + ((right-left) >> 1)
36+
curr_sum, invalid, groups = 0, True, 0
37+
for num in nums:
38+
if num > mid:
39+
inalid = False
40+
break
41+
if num + curr_sum > mid:
42+
groups += 1
43+
curr_sum = 0
44+
curr_sum += num
45+
if invalid and groups < m:
46+
right = mid
47+
else:
48+
left = mid + 1
49+
return left
50+

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
88
| # | Title | Solution | Difficulty |
99
|---| ----- | -------- | ---------- |
1010
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters)|[Python](./1000-1100q/1002.py)|Easy|
11+
|1014|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/) | [Python](./1000-1100q/1014.py) | Medium|
1112

1213
##### [Problems 900-1000](./900-1000q/)
1314
| # | Title | Solution | Difficulty |
@@ -29,6 +30,11 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
2930
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Python](./900-1000q/977.py)|Easy|
3031

3132

33+
##### [Problems 400-500](./400-500q/)
34+
| # | Title | Solution | Difficulty |
35+
|---| ----- | -------- | ---------- |
36+
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/) | [Python](./400-500q/410.py)|Hard|
37+
3238
##### [Problems 300-400](./300-400q/)
3339
| # | Title | Solution | Difficulty |
3440
|---| ----- | -------- | ---------- |

0 commit comments

Comments
 (0)