aboutsummaryrefslogtreecommitdiffstats
path: root/1
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2023-02-22 13:51:46 +0000
committerterminaldweller <thabogre@gmail.com>2023-02-22 13:51:46 +0000
commite27e913cedd5b017e19fa3de17d9a375800aff3a (patch)
tree0de0d639170c2a54b014fdf81c69e1702e756dfd /1
parent540 (diff)
downloadleetcode-e27e913cedd5b017e19fa3de17d9a375800aff3a.tar.gz
leetcode-e27e913cedd5b017e19fa3de17d9a375800aff3a.zip
1011
Diffstat (limited to '')
-rwxr-xr-x1011/main.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/1011/main.py b/1011/main.py
new file mode 100755
index 0000000..bec3d20
--- /dev/null
+++ b/1011/main.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+from typing import List
+
+
+class Solution:
+ def shipWithinDays(self, weights: List[int], days: int) -> int:
+ start = max(weights)
+ end = sum(weights)
+
+ def feasible(weights, c, days):
+ daysNeeded = 1
+ currentLoad = 0
+ for weight in weights:
+ currentLoad += weight
+ if currentLoad > c:
+ daysNeeded += 1
+ currentLoad = weight
+
+ return daysNeeded <= days
+
+ while start < end:
+ mid = start + (end - start) // 2
+ if feasible(weights, mid, days):
+ end = mid
+ else:
+ start = mid + 1
+
+ return start
+
+
+if __name__ == "__main__":
+ solution = Solution()
+ print(solution.shipWithinDays([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5))
+ print(solution.shipWithinDays([3, 2, 2, 4, 1, 4], 3))
+ print(solution.shipWithinDays([1, 2, 3, 1, 1], 4))