Skip to content

Commit 3ece4f1

Browse files
havanagrawalfishercoder1534
authored andcommitted
Add solution for 1011 (#59)
1 parent f6a95fe commit 3ece4f1

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Your ideas/fixes/algorithms are more than welcome!
3333
|1020|[Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1020.java) | O(mn) | O(mn) | |Medium|Graph, DFS, BFS, recursion|
3434
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | O(n) | O(1) | |Easy|
3535
|1014|[Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1014.java) | O(n) | O(1) | |Medium|
36+
|1011|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1011.java) | O(nlogn) | O(1) | |Medium|Binary Search|
3637
|1010|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1010.java) | O(n) | O(1) | |Easy|
3738
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java) | O(n) | O(1) | |Easy|
3839
|1003|[Check If Word Is Valid After Substitutions](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1003.java) | O(n) | O(n) | |Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1011. Capacity To Ship Packages Within D Days
5+
*
6+
* A conveyor belt has packages that must be shipped from one port to another within D days.
7+
*
8+
* The i-th package on the conveyor belt has a weight of weights[i].
9+
* Each day, we load the ship with packages on the conveyor belt (in the order given by weights).
10+
* We may not load more weight than the maximum weight capacity of the ship.
11+
*
12+
* Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.
13+
*
14+
* Note:
15+
* * 1 <= D <= weights.length <= 50000
16+
* * 1 <= weights[i] <= 500
17+
*/
18+
19+
public class _1011 {
20+
public static class Solution1 {
21+
public int daysToShip(int[] weights, int capacity) {
22+
int days = 0;
23+
int currentShip = 0;
24+
25+
for (int k : weights) {
26+
if (currentShip + k > capacity) {
27+
currentShip = 0;
28+
days += 1;
29+
}
30+
currentShip += k;
31+
}
32+
33+
return days + 1;
34+
}
35+
36+
public int shipWithinDays(int[] weights, int D) {
37+
38+
int sum = 0;
39+
int max = 0;
40+
41+
for (int k : weights) {
42+
sum += k;
43+
max = Math.max(max, k);
44+
}
45+
46+
// Minimum possible capacity needs to be as much as the heaviest package
47+
int lower = max;
48+
// Maximum possible capacity is the total weight of all packages
49+
int upper = sum;
50+
51+
if (daysToShip(weights, lower) <= D) {
52+
return lower;
53+
}
54+
55+
// Guess is for capacity
56+
int currentGuess;
57+
int bestGuess = -1;
58+
59+
// Binary search
60+
while (lower <= upper) {
61+
currentGuess = (upper + lower)/2;
62+
if (daysToShip(weights, currentGuess) <= D) {
63+
bestGuess = currentGuess;
64+
upper = currentGuess - 1;
65+
} else {
66+
lower = currentGuess + 1;
67+
}
68+
}
69+
70+
return bestGuess;
71+
}
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1011;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1011Test {
10+
private static _1011.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1011.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
int[] weights = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
20+
assertEquals(solution1.shipWithinDays(weights, 5), 15);
21+
}
22+
23+
@Test
24+
public void test2() {
25+
int[] weights = {3, 2, 2, 4, 1, 4};
26+
assertEquals(solution1.shipWithinDays(weights, 3), 6);
27+
}
28+
29+
@Test
30+
public void test3() {
31+
int[] weights = {1, 2, 3, 1, 1};
32+
assertEquals(solution1.shipWithinDays(weights, 4), 3);
33+
}
34+
}

0 commit comments

Comments
 (0)