-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path1_Gloves.java
48 lines (42 loc) · 1.36 KB
/
1_Gloves.java
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
import java.util.*;
public class Gloves {
public static int findMinimum(int n, int[] left, int[] right) {
// write code here
int selectNum = 0;
if(n==0||left==null||right==null||left.length==0||right.length==0)
return selectNum;
int tmpSelectNum = 0;
tmpSelectNum = findOneColorCost(left,right)+getAllColorCost(left,right);
selectNum = findOneColorCost(right,left)+getAllColorCost(right,left);
if(tmpSelectNum<selectNum)
return tmpSelectNum;
return selectNum;
}
public static int findOneColorCost(int[] array1,int[] array2){
int cost = 0;
for(int i=0; i<array1.length; i++)
if(array2[i]==0)
cost += array1[i];
cost++;
return cost;
}
public static int getAllColorCost(int[] array1,int[] array2){
int min = -1;
int cost = 0;
for(int j=0; j<array1.length; j++){
if(min==-1&&array2[j]!=0)
min = array2[j];
else if(min!=-1&&array2[j]<min&&array2[j]!=0)
min = array2[j];
cost += array2[j];
}
if(min>1)
cost = cost - min + 1;
return cost;
}
public static void main(String[] args){
int [] left = {1,2,0,1,3,1};
int [] right= {0,0,0,2,0,1};
System.out.println(findMinimum(6,left,right));
}
}