From 845ea7c204d2ee452d1d8e3eb9ce68964d317701 Mon Sep 17 00:00:00 2001 From: Havan Date: Fri, 26 Apr 2019 20:54:55 -0700 Subject: [PATCH] Add solution for 1011 --- README.md | 1 + .../java/com/fishercoder/solutions/_1011.java | 73 +++++++++++++++++++ src/test/java/com/fishercoder/_1011Test.java | 34 +++++++++ 3 files changed, 108 insertions(+) create mode 100644 src/main/java/com/fishercoder/solutions/_1011.java create mode 100644 src/test/java/com/fishercoder/_1011Test.java diff --git a/README.md b/README.md index 4fade5b33d..17b4312a6b 100644 --- a/README.md +++ b/README.md @@ -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| diff --git a/src/main/java/com/fishercoder/solutions/_1011.java b/src/main/java/com/fishercoder/solutions/_1011.java new file mode 100644 index 0000000000..d28cedd123 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_1011.java @@ -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; + } + } +} diff --git a/src/test/java/com/fishercoder/_1011Test.java b/src/test/java/com/fishercoder/_1011Test.java new file mode 100644 index 0000000000..b5e4aae6bb --- /dev/null +++ b/src/test/java/com/fishercoder/_1011Test.java @@ -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); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy