File tree 2 files changed +49
-1
lines changed
2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,085 LeetCode solutions in JavaScript
1
+ # 1,086 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
819
819
1008|[ Construct Binary Search Tree from Preorder Traversal] ( ./solutions/1008-construct-binary-search-tree-from-preorder-traversal.js ) |Medium|
820
820
1009|[ Complement of Base 10 Integer] ( ./solutions/1009-complement-of-base-10-integer.js ) |Easy|
821
821
1010|[ Pairs of Songs With Total Durations Divisible by 60] ( ./solutions/1010-pairs-of-songs-with-total-durations-divisible-by-60.js ) |Medium|
822
+ 1011|[ Capacity To Ship Packages Within D Days] ( ./solutions/1011-capacity-to-ship-packages-within-d-days.js ) |Medium|
822
823
1022|[ Sum of Root To Leaf Binary Numbers] ( ./solutions/1022-sum-of-root-to-leaf-binary-numbers.js ) |Easy|
823
824
1023|[ Camelcase Matching] ( ./solutions/1023-camelcase-matching.js ) |Medium|
824
825
1028|[ Recover a Tree From Preorder Traversal] ( ./solutions/1028-recover-a-tree-from-preorder-traversal.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1011. Capacity To Ship Packages Within D Days
3
+ * https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/
4
+ * Difficulty: Medium
5
+ *
6
+ * A conveyor belt has packages that must be shipped from one port to another within days days.
7
+ *
8
+ * The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship
9
+ * with packages on the conveyor belt (in the order given by weights). We may not load more weight
10
+ * than the maximum weight capacity of the ship.
11
+ *
12
+ * Return the least weight capacity of the ship that will result in all the packages on the conveyor
13
+ * belt being shipped within days days.
14
+ */
15
+
16
+ /**
17
+ * @param {number[] } weights
18
+ * @param {number } days
19
+ * @return {number }
20
+ */
21
+ var shipWithinDays = function ( weights , days ) {
22
+ let left = Math . max ( ...weights ) ;
23
+ let right = weights . reduce ( ( sum , weight ) => sum + weight , 0 ) ;
24
+
25
+ while ( left < right ) {
26
+ const mid = Math . floor ( ( left + right ) / 2 ) ;
27
+ let currentDays = 1 ;
28
+ let currentWeight = 0 ;
29
+
30
+ for ( const weight of weights ) {
31
+ if ( currentWeight + weight > mid ) {
32
+ currentDays ++ ;
33
+ currentWeight = weight ;
34
+ } else {
35
+ currentWeight += weight ;
36
+ }
37
+ }
38
+
39
+ if ( currentDays > days ) {
40
+ left = mid + 1 ;
41
+ } else {
42
+ right = mid ;
43
+ }
44
+ }
45
+
46
+ return left ;
47
+ } ;
You can’t perform that action at this time.
0 commit comments