From f2064d7681107bacd9c72c8ea3b9a5c6b3e7d8fe Mon Sep 17 00:00:00 2001 From: rossy0213 Date: Mon, 3 Jun 2024 01:48:51 +0900 Subject: [PATCH] add: Capacity To Ship Packages Within D Days --- CapacityToShipPackagesWithinDDays/step1.md | 50 ++++++++ CapacityToShipPackagesWithinDDays/step2.md | 43 +++++++ CapacityToShipPackagesWithinDDays/step3.md | 127 +++++++++++++++++++++ 3 files changed, 220 insertions(+) create mode 100644 CapacityToShipPackagesWithinDDays/step1.md create mode 100644 CapacityToShipPackagesWithinDDays/step2.md create mode 100644 CapacityToShipPackagesWithinDDays/step3.md diff --git a/CapacityToShipPackagesWithinDDays/step1.md b/CapacityToShipPackagesWithinDDays/step1.md new file mode 100644 index 0000000..c9929e1 --- /dev/null +++ b/CapacityToShipPackagesWithinDDays/step1.md @@ -0,0 +1,50 @@ +```java +// Using binary search to find the minimum cap that we can ship the packages +// Min(left) will be the max value in the array, Max(right) will be sum of the values(when we need to ship everything in a day) +// During the search, if we can ship it meaning the smallest cap in left ~ mid +// if we can't, meaning we need to search between mid + 1 ~ right +// Time complexity: O(NlogM) -> N: number of weights, M: sum of values - max value +// Space complexity: O(1) +// Time spend: 08:34 +class Solution { + public int shipWithinDays(int[] weights, int days) { + int l = -1; + int r = 0; + for (int w : weights) { + if (w > l) { + l = w; + } + r += w; + } + + while (l < r) { + int mid = (l + r) / 2; + if (canShip(weights, mid, days)) { + r = mid; + } else { + l = mid + 1; + } + } + + return l; + } + + public boolean canShip(int[] weights, int cap, int days) { + int day = 1; + int temp = 0; + for (int w : weights) { + if (temp + w <= cap) { + temp += w; + continue; + } + day++; + if (day > days) { + return false; + } + temp = w; + } + + return true; + } +} +``` \ No newline at end of file diff --git a/CapacityToShipPackagesWithinDDays/step2.md b/CapacityToShipPackagesWithinDDays/step2.md new file mode 100644 index 0000000..33e3135 --- /dev/null +++ b/CapacityToShipPackagesWithinDDays/step2.md @@ -0,0 +1,43 @@ +```java +// Time spend: 03:21 +class Solution { + public int shipWithinDays(int[] weights, int days) { + int l = -1; + int r = 0; + for (int weight : weights) { + if (weight > l) { + l = weight; + } + r += weight; + } + + while (l < r) { + int mid = (l + r) / 2; + if (canShip(weights, mid, days)) { + r = mid; + } else { + l = mid + 1; + } + } + + return l; + } + + public boolean canShip(int[] weights, int cap, int days) { + int daysNeed = 1; + int currWeight = 0; + for (int weight : weights) { + if (currWeight + weight > cap) { + daysNeed++; + if (daysNeed > days) { + return false; + } + currWeight = 0; + } + currWeight += weight; + } + + return true; + } +} +``` \ No newline at end of file diff --git a/CapacityToShipPackagesWithinDDays/step3.md b/CapacityToShipPackagesWithinDDays/step3.md new file mode 100644 index 0000000..9212220 --- /dev/null +++ b/CapacityToShipPackagesWithinDDays/step3.md @@ -0,0 +1,127 @@ +```java +// Time spend: 03:11 +class Solution { + public int shipWithinDays(int[] weights, int days) { + int l = -1; + int r = 0; + for (int weight : weights) { + if (weight > l) { + l = weight; + } + r += weight; + } + + while (l < r) { + int mid = (l + r) / 2; + if (canShip(weights, mid, days)) { + r = mid; + continue; + } + l = mid + 1; + } + + return l; + } + + public boolean canShip(int[] weights, int cap, int days) { + int daysNeed = 1; + int currWeight = 0; + for (int weight : weights) { + if (currWeight + weight > cap) { + currWeight = 0; + daysNeed++; + if (daysNeed > days) { + return false; + } + } + currWeight += weight; + } + + return true; + } +} +``` + +```java +// Time spend: 02:47 +class Solution { + public int shipWithinDays(int[] weights, int days) { + int l = -1; + int r = 0; + for (int w : weights) { + l = Math.max(w, l); + r += w; + } + + while (l < r) { + int mid = (l + r) / 2; + if (canShip(weights, days, mid)) { + r = mid; + continue; + } + l = mid + 1; + } + + return l; + } + + public boolean canShip(int[] weights, int days, int cap) { + int daysNeed = 1; + int currWeight = 0; + for (int w : weights) { + if (currWeight + w > cap) { + currWeight = 0; + daysNeed++; + if (daysNeed > days) { + return false; + } + } + currWeight += w; + } + + return true; + } +} +``` + +```java +// Time spend: 02:21 +class Solution { + public int shipWithinDays(int[] weights, int days) { + int l = -1; + int r = 0; + for (int w : weights) { + l = Math.max(l, w); + r += w; + } + + while (l < r) { + int mid = (l + r) / 2; + if (canShip(weights, days, mid)) { + r = mid; + continue; + } + l = mid + 1; + } + + return l; + } + + public boolean canShip(int[] weights, int days, int cap) { + int daysNeed = 1; + int currWeight = 0; + for (int w : weights) { + if (currWeight + w > cap) { + currWeight = 0; + daysNeed++; + if (daysNeed > days) { + return false; + } + } + currWeight += w; + } + + return true; + } +} +``` \ No newline at end of file