-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion6.java
More file actions
65 lines (62 loc) · 2.21 KB
/
Copy pathQuestion6.java
File metadata and controls
65 lines (62 loc) · 2.21 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package practice1_Impl;
public class Question6 {
public int getMin(int[] fruit){
int min = 100;
for(int x : fruit){
min = Math.min(min, x);
}
return min;
}
public Boolean isMinUnique(int[] fruit){
int cnt = 0;
int min = getMin(fruit);
for(int x : fruit){
if(x == min) cnt++;
}
return cnt == 1;
}
public int getMinIndex(int[] fruit){
int min = getMin(fruit);
for(int i = 0; i < 3; i++){
if(fruit[i] == min) return i;
}
return 0;
}
public int solution(int[][] fruit){
int answer = 0;
int n = fruit.length;
int[] ch = new int[n];
for(int i = 0; i < n; i++){
if(ch[i] == 1) continue;
if(isMinUnique(fruit[i]) == false) continue;
for(int j = i+1; j < n; j++){
if(ch[j] == 1) continue;
if(isMinUnique(fruit[j]) == false) continue;
int a = getMinIndex(fruit[i]);
int b = getMinIndex(fruit[j]);
if(a != b && fruit[i][b] > 0 && fruit[j][a] > 0){
if(fruit[i][a] + 1 <= fruit[i][b] - 1 && fruit[j][b] + 1 <= fruit[j][a] - 1){
fruit[i][a]++;
fruit[i][b]--;
fruit[j][b]++;
fruit[j][a]--;
ch[i] = 1;
ch[j] = 1;
break;
}
}
}
}
for(int[] x : fruit){
answer += getMin(x);
}
return answer;
}
public static void main(String[] args){
Question6 T = new Question6();
System.out.println(T.solution(new int[][]{{10, 20, 30}, {12, 15, 20}, {20, 12, 15}, {15, 20, 10}, {10, 15, 10}}));
System.out.println(T.solution(new int[][]{{10, 9, 11}, {15, 20, 25}}));
System.out.println(T.solution(new int[][]{{0, 3, 27}, {20, 5, 5}, {19, 5, 6}, {10, 10, 10}, {15, 10, 5}, {3, 7, 20}}));
System.out.println(T.solution(new int[][]{{3, 7, 20}, {10, 15, 5}, {19, 5, 6}, {10, 10, 10}, {15, 10, 5}, {3, 7, 20}, {12, 12, 6}, {10, 20, 0}, {5, 10, 15}}));
}
}