-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1648.java
More file actions
49 lines (38 loc) · 1.34 KB
/
Copy pathSolution1648.java
File metadata and controls
49 lines (38 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Arrays;
public class Solution1648 {
public int maxProfit(int[] inventory, int orders) {
return 0;
}
public int maxProfitLoop(int[] inventory, int orders) {
if (inventory.length == 1) {
int first = inventory[0] - orders;
int last = inventory[0];
int result = (first + last) / 2 * orders;
return result % (int) (Math.pow(10, 9) + 7);
}
int result = 0;
for (; orders > 0; orders--) {
boolean allZero = true;
for (int j : inventory) {
if (j > 0) {
allZero = false;
break;
}
}
if (allZero) return result;
int maxIndex = 0;
for (int i = 0; i < inventory.length; i++) {
if (inventory[i] > 0 && inventory[i] > inventory[maxIndex]) maxIndex = i;
}
int price = inventory[maxIndex];
inventory[maxIndex]--;
result = (result + price) % (int) (Math.pow(10, 9) + 7);
}
return result % (int) (Math.pow(10, 9) + 7);
}
public static void main(String[] args) {
int[] inventory = new int[]{1000000000};
int orders = 1000000000;
System.out.println(new Solution1648().maxProfitLoop(inventory, orders));
}
}