-
Notifications
You must be signed in to change notification settings - Fork 423
/
KthLargestElementInAnArray.js
95 lines (78 loc) · 1.91 KB
/
KthLargestElementInAnArray.js
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
92
93
94
95
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
// 写了个快排和归并。
function sort(arr) {
// 快排
if (arr.length === 0) {
return arr
}
// 选种
let bean = Math.floor((arr.length-1) / 2)
let base = arr[bean]
let bigger = []
let smaller = []
for (let [index, item] of arr.entries()) {
if (index === bean) {
continue
}
if (item >= base) {
bigger.push(item)
} else {
smaller.push(item)
}
}
return [...sort(smaller), base, ...sort(bigger)]
}
// 归并
function split(arr) {
let bean = Math.floor((arr.length-1) / 2)
let left = arr.slice(0, bean)
let right = arr.slice(bean)
return [left, right]
}
function merge(arr, arr2) {
let result = []
let index1 = 0
let index2 = 0
while (index1 <= arr.length - 1 && index2 <= arr2.length - 1) {
if (arr[index1] < arr2[index2]) {
result.push(arr[index1])
index1 += 1
} else if (arr[index1] > arr2[index2]) {
result.push(arr2[index2])
index2 += 1
} else {
result.push(arr[index1], arr2[index2])
index1 += 1
index2 += 1
}
}
// 如果第一个arr
if (index1 <= arr.length - 1) {
result.push(...arr.slice(index1))
}
if (index2 <= arr2.length - 1) {
result.push(...arr2.slice(index2))
}
return result
}
function sort2(arr) {
if (arr.length <= 8) {
return sort(arr)
}
let sp = split(arr)
// return sp.reduce((a,b) => merge(a,b))
return merge(sort2(sp[0]), sort2(sp[1]))
}
var findKthLargest = function(nums, k) {
// nums = nums.sort((a,b) => b-a)
// nums = sort(nums)
// nums = split(nums)
// nums = merge(nums[0], nums[1])
nums = sort2(nums)
// console.log(nums)
return nums[nums.length-k]
};