forked from loveLynch/wsn_share
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetGreatNumber.java
88 lines (83 loc) · 3.43 KB
/
GetGreatNumber.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
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
import java.util.ArrayList;
import java.util.Scanner;
public class GetGreatNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String NK = input.nextLine();
String originalNum = input.nextLine();
Integer N = Integer.valueOf(NK.split(" ")[0]);
Integer K = Integer.valueOf(NK.split(" ")[1]);
if (K <= N) {
if (originalNum.length() == N) {
ArrayList<String> greatNum = getSpeedAndNum(originalNum, K);
System.out.println(greatNum.get(0));
System.out.println(greatNum.get(1));
} else {
System.out.println("the input num length is error!");
}
} else {
System.out.println("the great num length over the sum");
}
}
private static ArrayList<String> getSpeedAndNum(String originalNum, Integer k) {
ArrayList<String> result = new ArrayList<>();
int[] countNum = getNumCount(originalNum);
int res = Integer.MAX_VALUE;
String ans = "A";
for (int i = 0; i < 10; i++) {
String tmpNum = originalNum;
int need = k - countNum[i];
int cost = 0;
int gap = 1;
while (need > 0) {
if (i + gap <= 9) {
if (countNum[i + gap] < need) {
tmpNum = tmpNum.replace(String.valueOf(i + gap), String.valueOf(i));
cost += countNum[i + gap] + gap;
need -= countNum[i + gap];
} else {
tmpNum = tmpNum.replace(String.valueOf(i + gap), String.valueOf(need));
cost += need * gap;
break;
}
}
if (i - gap >= 0) {
if (countNum[i - gap] < need) {
tmpNum = tmpNum.replace(String.valueOf(i - gap), String.valueOf(i));
cost += countNum[i - gap] + gap;
need -= countNum[i - gap];
} else {
StringBuilder builder1 = new StringBuilder(tmpNum);
builder1.reverse();
tmpNum = builder1.toString();
tmpNum = tmpNum.replace(String.valueOf(i - gap), String.valueOf(need));
StringBuilder builder2 = new StringBuilder(tmpNum);
builder2.reverse();
tmpNum = builder2.toString();
cost += need * gap;
break;
}
}
System.out.println(tmpNum);
gap += 1;
}
if (cost < res) {
ans = tmpNum;
res = cost;
} else if (cost == res && Integer.valueOf(tmpNum) < Integer.valueOf(ans)) {
ans = tmpNum;
}
}
result.add(String.valueOf(res));
result.add(ans);
return result;
}
public static int[] getNumCount(String originalNum) {
int[] countNum = new int[10];
char[] numArray = originalNum.toCharArray();
for (int i = 0; i < numArray.length; i++) {
countNum[numArray[i] - '0'] = countNum[numArray[i] - '0'] + 1;
}
return countNum;
}
}