Skip to content

Commit 7c546ee

Browse files
committed
K번째로 큰 요소 찾기
1 parent eecc523 commit 7c546ee

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

FindKthLargestElement.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.Arrays;
2+
3+
4+
// my soluton_USE Arrays
5+
public class FindKthLargestElement {
6+
public static void main(String[] args) {
7+
System.out.println(new FindKthLargestElement().findKthLargest(new int[]{5, 2, 4, 3, 1},2));
8+
}
9+
public int findKthLargest(int[] nums, int k) {
10+
Arrays.sort(nums);
11+
return nums[nums.length-k];
12+
}
13+
}
14+
15+
/* USE_quickSort
16+
class Solution {
17+
public int findKthLargest(int[] nums, int k) {
18+
if (nums == null) {
19+
return -1;
20+
}
21+
22+
return quickSelect(nums, 0, nums.length - 1, nums.length - k + 1);
23+
}
24+
25+
26+
private int quickSelect(int[] nums, int start, int end, int k) {
27+
if (start == end) {
28+
return nums[start];
29+
}
30+
31+
int mid = start + (end - start) / 2;
32+
int pivot = nums[mid];
33+
34+
int i = start, j = end;
35+
while (i <= j) {
36+
while (i <= j && nums[i] < pivot) {
37+
i++;
38+
}
39+
while (i <= j && nums[j] > pivot) {
40+
j--;
41+
}
42+
if (i <= j) {
43+
int temp = nums[i];
44+
nums[i] = nums[j];
45+
nums[j] = temp;
46+
i++;
47+
j--;
48+
}
49+
}
50+
51+
if (start + k - 1 <= j) {
52+
return quickSelect(nums, start, j, k);
53+
}
54+
55+
if (start + k - 1 >= i) {
56+
return quickSelect(nums, i, end, k-(i-start));
57+
}
58+
59+
return nums[j+1];
60+
}
61+
}
62+
*/

0 commit comments

Comments
 (0)