Skip to content

Add solution for 1011 #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Your ideas/fixes/algorithms are more than welcome!
|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|
|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|
|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|
|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|
|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|
|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|
|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|
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1011.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.fishercoder.solutions;

/**
* 1011. Capacity To Ship Packages Within D Days
*
* A conveyor belt has packages that must be shipped from one port to another within D days.
*
* 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.
*
* Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.
*
* Note:
* * 1 <= D <= weights.length <= 50000
* * 1 <= weights[i] <= 500
*/

public class _1011 {
public static class Solution1 {
public int daysToShip(int[] weights, int capacity) {
int days = 0;
int currentShip = 0;

for (int k : weights) {
if (currentShip + k > capacity) {
currentShip = 0;
days += 1;
}
currentShip += k;
}

return days + 1;
}

public int shipWithinDays(int[] weights, int D) {

int sum = 0;
int max = 0;

for (int k : weights) {
sum += k;
max = Math.max(max, k);
}

// Minimum possible capacity needs to be as much as the heaviest package
int lower = max;
// Maximum possible capacity is the total weight of all packages
int upper = sum;

if (daysToShip(weights, lower) <= D) {
return lower;
}

// Guess is for capacity
int currentGuess;
int bestGuess = -1;

// Binary search
while (lower <= upper) {
currentGuess = (upper + lower)/2;
if (daysToShip(weights, currentGuess) <= D) {
bestGuess = currentGuess;
upper = currentGuess - 1;
} else {
lower = currentGuess + 1;
}
}

return bestGuess;
}
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/fishercoder/_1011Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fishercoder;

import com.fishercoder.solutions._1011;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class _1011Test {
private static _1011.Solution1 solution1;

@BeforeClass
public static void setup() {
solution1 = new _1011.Solution1();
}

@Test
public void test1() {
int[] weights = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assertEquals(solution1.shipWithinDays(weights, 5), 15);
}

@Test
public void test2() {
int[] weights = {3, 2, 2, 4, 1, 4};
assertEquals(solution1.shipWithinDays(weights, 3), 6);
}

@Test
public void test3() {
int[] weights = {1, 2, 3, 1, 1};
assertEquals(solution1.shipWithinDays(weights, 4), 3);
}
}