File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ public class Solution {
4
+ public static void main (String [] args ) {
5
+ Scanner sc = new Scanner (System .in );
6
+
7
+ int n = sc .nextInt ();
8
+ int goal = sc .nextInt ();
9
+ int [] machines = new int [n ];
10
+ for (int i = 0 ; i < machines .length ; i ++) {
11
+ machines [i ] = sc .nextInt ();
12
+ }
13
+ System .out .println (solve (machines , goal ));
14
+
15
+ sc .close ();
16
+ }
17
+
18
+ static long solve (int [] machines , int goal ) {
19
+
20
+ // User a modified binary search to find required day
21
+ long result = 0 ;
22
+ long lower = 1 ;
23
+ long upper = 1000000000000000000L ;
24
+
25
+ while (lower <= upper ) {
26
+ long mid = (lower + upper ) / 2 ;
27
+
28
+ int totalProd = getProduction (machines , mid );
29
+
30
+ if (totalProd >= goal ) {
31
+ result = mid ;
32
+ upper = mid - 1 ;
33
+ } else {
34
+ lower = mid + 1 ;
35
+ }
36
+ }
37
+ return result ;
38
+ }
39
+
40
+ static int getProduction (int [] machines , long days ) {
41
+ long itemNumber = 0 ;
42
+ for (int machine : machines ) {
43
+ itemNumber += days / machine ;
44
+
45
+ if (itemNumber > Integer .MAX_VALUE ) {
46
+ return Integer .MAX_VALUE ;
47
+ }
48
+ }
49
+ return (int ) itemNumber ;
50
+ }
51
+ }
You can’t perform that action at this time.
0 commit comments