File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ package task_1011 ;
2
+
3
+ public class Solution {
4
+ public int shipWithinDays (int [] weights , int days ) {
5
+ int minimalCapacity = 0 ;
6
+ int maximalCapacity = 0 ;
7
+ for (int i = 0 ; i < weights .length ; i ++) {
8
+ if (weights [i ] > minimalCapacity ) {
9
+ minimalCapacity = weights [i ];
10
+ }
11
+ maximalCapacity += weights [i ];
12
+ }
13
+ int middle = (maximalCapacity + minimalCapacity ) / 2 ;
14
+ int spentDaysAmount = countDaysSpentAmount (weights , middle );
15
+ while (minimalCapacity != maximalCapacity ) {
16
+ if (spentDaysAmount < days ) {
17
+ maximalCapacity = middle ;
18
+ } else {
19
+ minimalCapacity = middle + 1 ;
20
+ }
21
+ middle = (maximalCapacity + minimalCapacity ) / 2 ;
22
+ spentDaysAmount = countDaysSpentAmount (weights , middle );
23
+ }
24
+ return minimalCapacity ;
25
+ }
26
+
27
+ public int countDaysSpentAmount (int [] weights , int shipCapacity ) {
28
+ int dayCounter = 0 ;
29
+ int currentCapacity = 0 ;
30
+ for (int i = 0 ; i < weights .length ; i ++) {
31
+ if (currentCapacity + weights [i ] > shipCapacity ) {
32
+ dayCounter ++;
33
+ currentCapacity = weights [i ];
34
+ } else {
35
+ currentCapacity += weights [i ];
36
+ }
37
+ }
38
+ return dayCounter ;
39
+ }
40
+
41
+ }
You can’t perform that action at this time.
0 commit comments