From e4de63f774cf7eda0172b36eafddbb76db889b4a Mon Sep 17 00:00:00 2001 From: Animax Deng Date: Sat, 28 Dec 2019 00:43:05 -0600 Subject: [PATCH] Add solution for Capacity To Ship Packages Within D days --- .../CapacityToShipPackagesWithinDdays.swift | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Search/CapacityToShipPackagesWithinDdays.swift diff --git a/Search/CapacityToShipPackagesWithinDdays.swift b/Search/CapacityToShipPackagesWithinDdays.swift new file mode 100644 index 00000000..049f5712 --- /dev/null +++ b/Search/CapacityToShipPackagesWithinDdays.swift @@ -0,0 +1,45 @@ +/** + * Question Link: https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/ + * Primary idea: Binary Search, minimum possible value is max number in weights; + * maximum possible value is the sum of weights. canShip function simulate if all item can be ship. + * + * Time Complexity: O(Wlogn), Space Complexity: O(1) + */ + +class Solution { + func shipWithinDays(_ weights: [Int], _ D: Int) -> Int { + if weights.count <= 0 { + return 0 + } + + var l = weights.max()!, r = weights.reduce(0) { $0 + $1 } + + while l <= r { + let mid = Int((r - l) / 2 + l) + if canShip(weights, D, mid) { + r = mid - 1 + } else { + l = mid + 1 + } + } + + return l + } + func canShip(_ weights: [Int], _ D: Int, _ cap:Int) -> Bool { + var visited = Set() + + var rest = cap, day = 0 + for (idx, w) in weights.enumerated() { + if rest - w < 0 { + day += 1 + if day >= D { + return false + } + rest = cap + } + rest -= w + } + + return true + } +}