Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9c670a4

Browse files
committedDec 27, 2020
add js solution for leetcode 23 215 347
1 parent 1801335 commit 9c670a4

File tree

4 files changed

+246
-3
lines changed

4 files changed

+246
-3
lines changed
 

‎README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
|357|[Count Numbers with Unique Digits](https://leetcode.com/problems/count-numbers-with-unique-digits/) | |Medium|
141141
|350|[Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [js](./algorithms/intersectionOfTwoArraysII/intersectionOfTwoArraysII.js) |Easy|
142142
|349|[Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [js](./algorithms/intersectionOfTwoArrays/intersectionOfTwoArrays.js) |Easy|
143-
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | |Medium|
143+
|347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [js](./algorithms/topKFrequentElements/Solution.js) |Medium|
144144
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | |Easy|
145145
|344|[Reverse String](https://leetcode.com/problems/reverse-string/) [java](./algorithms/resverseString/Solution.java), [js](./algorithms/resverseString/resverseString.js) | |Easy|
146146
|343|[Integer Break](https://leetcode.com/problems/integer-break/) | |Medium|
@@ -223,7 +223,7 @@
223223
|218|[The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/)| |Hard|
224224
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)| |Easy|
225225
|216|[Combination Sum III](https://leetcode.com/problems/combination-sum-iii/)| |Medium|
226-
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)| |Medium|
226+
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)| [js](./algorithms/KthLargestElementInAnArray/KthLargestElementInAnArray.js) |Medium|
227227
|214|[Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/)| |Hard|
228228
|213|[House Robber II](https://leetcode.com/problems/house-robber-ii/)| |Medium|
229229
|212|[Word Search II](https://leetcode.com/problems/word-search-ii/)| |Hard|
@@ -409,7 +409,7 @@
409409
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [java](./algorithms/removeDuplicatesFromSortedArray/Solution.java) |Easy|
410410
|25|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)| |Hard|
411411
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| [js](./algorithms/swapNodesInPairs/swapNodesInPairs.js) |Medium|
412-
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| |Hard|
412+
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| [js](./algorithms/mergeKSortedLists/Solution.js) |Hard|
413413
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [js](./algorithms/generateParentheses/generateParentheses.js) |Medium|
414414
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| [java](./algorithm/mergeTwoSortedLists/Solution.java), [js](./algorithms/mergeTwoSortedLists/mergeTwoSortedLists.js) |Easy|
415415
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [js](./algorithms/validParentheses/validParentheses.js) |Easy|
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
6+
getParentIndex(i) {
7+
return (i - 1) >> 1;
8+
}
9+
10+
getLeftIndex(i) {
11+
return 2 * i + 1;
12+
}
13+
14+
getRightIndex(i) {
15+
return 2 * i + 2;
16+
}
17+
18+
swap(i1, i2) {
19+
const temp = this.heap[i1];
20+
this.heap[i1] = this.heap[i2];
21+
this.heap[i2] = temp;
22+
}
23+
24+
shiftUp(index) {
25+
if ( index === 0) return;
26+
const parentIndex = this.getParentIndex(index);
27+
if (this.heap[parentIndex] > this.heap[index]) {
28+
this.swap(parentIndex, index);
29+
this.shiftUp(parentIndex);
30+
}
31+
}
32+
33+
shiftDown(index) {
34+
if (index > this.heap.length - 1) return;
35+
const leftIndex = this.getLeftIndex(index);
36+
const rightIndex = this.getRightIndex(index);
37+
if (this.heap[leftIndex] < this.heap[index]) {
38+
this.swap(leftIndex, index);
39+
this.shiftDown(leftIndex);
40+
}
41+
if (this.heap[rightIndex] < this.heap[index]) {
42+
this.swap(rightIndex, index);
43+
this.shiftDown(rightIndex);
44+
}
45+
}
46+
47+
insert(value) {
48+
this.heap.push(value);
49+
this.shiftUp(this.heap.length - 1);
50+
}
51+
52+
pop() {
53+
this.heap[0] = this.heap.pop();
54+
this.shiftDown(0)
55+
}
56+
57+
peek() {
58+
return this.heap[0];
59+
}
60+
61+
size() {
62+
return this.heap.length;
63+
}
64+
}
65+
66+
var findKthLargest = function(nums, k) {
67+
const h = new MinHeap();
68+
nums.forEach(num => {
69+
h.insert(num);
70+
if (h.size() > k) {
71+
h.pop();
72+
}
73+
});
74+
return h.peek();
75+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
6+
getParentIndex(i) {
7+
return (i - 1) >> 1;
8+
}
9+
10+
getLeftIndex(i) {
11+
return 2 * i + 1;
12+
}
13+
14+
getRightIndex(i) {
15+
return 2 * i + 2;
16+
}
17+
18+
swap(i1, i2) {
19+
const temp = this.heap[i1];
20+
this.heap[i1] = this.heap[i2];
21+
this.heap[i2] = temp;
22+
}
23+
24+
shiftUp(index) {
25+
if ( index === 0) return;
26+
const parentIndex = this.getParentIndex(index);
27+
if (this.heap[parentIndex] && this.heap[parentIndex].val > this.heap[index].val) {
28+
this.swap(parentIndex, index);
29+
this.shiftUp(parentIndex);
30+
}
31+
}
32+
33+
shiftDown(index) {
34+
if (index > this.heap.length - 1) return;
35+
const leftIndex = this.getLeftIndex(index);
36+
const rightIndex = this.getRightIndex(index);
37+
if (this.heap[leftIndex] && this.heap[leftIndex].val < this.heap[index].val) {
38+
this.swap(leftIndex, index);
39+
this.shiftDown(leftIndex);
40+
}
41+
if (this.heap[rightIndex] && this.heap[rightIndex].val < this.heap[index].val) {
42+
this.swap(rightIndex, index);
43+
this.shiftDown(rightIndex);
44+
}
45+
}
46+
47+
insert(value) {
48+
this.heap.push(value);
49+
this.shiftUp(this.heap.length - 1);
50+
}
51+
52+
pop() {
53+
if (this.size() === 1) return this.heap.shift();
54+
const top = this.heap[0];
55+
this.heap[0] = this.heap.pop();
56+
this.shiftDown(0);
57+
return top;
58+
}
59+
60+
peek() {
61+
return this.heap[0];
62+
}
63+
64+
size() {
65+
return this.heap.length;
66+
}
67+
}
68+
69+
var mergeKLists = function(lists) {
70+
const h = new MinHeap();
71+
const res = new ListNode(null);
72+
let p = res;
73+
74+
lists.forEach(l => {
75+
!!l && h.insert(l);
76+
})
77+
78+
while (h.size()) {
79+
const node = h.pop();
80+
p.next = node;
81+
p = p.next;
82+
if (node.next) {
83+
h.insert(node.next)
84+
}
85+
}
86+
87+
return res.next;
88+
};
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
class MinHeap {
2+
constructor() {
3+
this.heap = [];
4+
}
5+
6+
getParentIndex(i) {
7+
return (i - 1) >> 1;
8+
}
9+
10+
getLeftIndex(i) {
11+
return 2 * i + 1;
12+
}
13+
14+
getRightIndex(i) {
15+
return 2 * i + 2;
16+
}
17+
18+
swap(i1, i2) {
19+
const temp = this.heap[i1];
20+
this.heap[i1] = this.heap[i2];
21+
this.heap[i2] = temp;
22+
}
23+
24+
shiftUp(index) {
25+
if ( index === 0) return;
26+
const parentIndex = this.getParentIndex(index);
27+
if (this.heap[parentIndex] && this.heap[parentIndex].value > this.heap[index].value) {
28+
this.swap(parentIndex, index);
29+
this.shiftUp(parentIndex);
30+
}
31+
}
32+
33+
shiftDown(index) {
34+
if (index > this.heap.length - 1) return;
35+
const leftIndex = this.getLeftIndex(index);
36+
const rightIndex = this.getRightIndex(index);
37+
if (this.heap[leftIndex] && this.heap[leftIndex].value < this.heap[index].value) {
38+
this.swap(leftIndex, index);
39+
this.shiftDown(leftIndex);
40+
}
41+
if (this.heap[rightIndex] && this.heap[rightIndex].value < this.heap[index].value) {
42+
this.swap(rightIndex, index);
43+
this.shiftDown(rightIndex);
44+
}
45+
}
46+
47+
insert(value) {
48+
this.heap.push(value);
49+
this.shiftUp(this.heap.length - 1);
50+
}
51+
52+
pop() {
53+
this.heap[0] = this.heap.pop();
54+
this.shiftDown(0)
55+
}
56+
57+
peek() {
58+
return this.heap[0];
59+
}
60+
61+
size() {
62+
return this.heap.length;
63+
}
64+
}
65+
66+
var topKFrequent = function(nums, k) {
67+
const map = new Map();
68+
const h = new MinHeap();
69+
nums.forEach(num => {
70+
map.set(num, map.has(num) ? map.get(num) + 1 : 1)
71+
});
72+
map.forEach((value, key) => {
73+
h.insert({value, key});
74+
if (h.size() > k) {
75+
h.pop();
76+
}
77+
});
78+
79+
return h.heap.map(a => a.key);
80+
};

0 commit comments

Comments
 (0)
Please sign in to comment.