-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimal.java
More file actions
91 lines (72 loc) · 1.66 KB
/
Minimal.java
File metadata and controls
91 lines (72 loc) · 1.66 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.Arrays;
public class Minimal {
public static void main(String[] args){
int[] possible = new int[]{
1,2,3,4
};
System.out.println(getminimal("1000",possible));
}
public static String getminimal(String threshold, int[] possible){
threshold = String.valueOf(Integer.parseInt(threshold)+1);
int len = threshold.length();
Arrays.sort(possible);
System.out.println(Arrays.toString(possible));
char[] thres = threshold.toCharArray();
for(int i = 0; i< len; i++){
thres[i]--;
}
int i = 0;
while(i<len){
System.out.println(String.valueOf(thres));
int the = getnolessthan(thres[i], possible);
if (the == 10){
thres[i] = '0';
i--;
if (i<0){
String result ="";
for(int j =0; j<len; j++){
result +=possible[0];
}
return getfirstnonzero(possible) + result;
}
continue;
}
if (the > thres[i]+1 -'0'){
thres[i] = (char)('0'+the);
i++;
String result="";
for(int j = i;j<len;j++){
result+= possible[0];
}
return String.valueOf(thres, 0, i)+result;
}
if(the == thres[i] +1 - '0'){
thres[i] = (char)('0'+the);
i++;
}
if (i== len){
return String.valueOf(thres);
}
}
return String.valueOf(thres);
}
public static int getnolessthan(char x, int[] possible){
int len = possible.length;
int xint = (int)(x-'0');
for (int i =0; i< len; i++){
if(possible[i]>xint){
return possible[i];
}
}
return 10;
}
public static int getfirstnonzero(int[] possible){
if (possible[0]!=0){
return possible[0];
}
if (possible.length>=2){
return possible[1];
}
return 0;
}
}