-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSortKth.java
82 lines (72 loc) · 2.45 KB
/
MergeSortKth.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
class Solution {
public boolean debug = false;
public int findKthLargest(int[] nums, int k) {
int low = 0;
int high = nums.length - 1;
mergesort(nums, low, high);
return nums[high - k + 1];
}
public void mergesort(int[] nums, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
mergesort(nums, low, mid);
mergesort(nums, mid + 1, high);
merge(nums, low, high, mid);
}
}
public void merge(int[] nums, int low, int high, int mid) {
int[] temp = new int[high - low + 1];
int left = low;
int right = mid + 1;
int i = 0;
while ((left <= mid) && (right <= high)) {
if (debug) {
System.out.println("nums.length = " + nums.length);
System.out.println("left = " + left);
System.out.println("right = " + right);
}
if (nums[left] < nums[right]) {
temp[i++] = nums[left++];
} else {
temp[i++] = nums[right++];
}
}
while (left <= mid) {
temp[i++] = nums[left++];
}
while (right <= high) {
temp[i++] = nums[right++];
}
for (int n = low; n <= high; n++) {
nums[n] = temp[n - low];
}
}
}
public class MergeSortKth {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
int[] nums = stringToIntegerArray(line);
line = in.readLine();
int k = Integer.parseInt(line);
int ret = new Solution().findKthLargest(nums, k);
String out = String.valueOf(ret);
System.out.print(out);
}
}
}