Skip to content

Commit f96350b

Browse files
BarklimBarklim
Barklim
authored and
Barklim
committed
add heap examples
1 parent c5cbab8 commit f96350b

File tree

14 files changed

+292
-1
lines changed

14 files changed

+292
-1
lines changed

example/8.Heap/0023.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode[]} lists
10+
* @return {ListNode}
11+
*/
12+
var mergeKLists = function(lists) {
13+
14+
};
15+
16+
const example1 = mergeKLists([[1,4,5],[1,3,4],[2,6]]); // 1,1,2,3,4,4,5,6]
17+
const example2 = mergeKLists([]); // []
18+
const example3 = mergeKLists([[]]); // []
19+
20+
console.log(example1);
21+
console.log(example2);
22+
console.log(example3);
23+
console.log(example4);

example/8.Heap/0215.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var findKthLargest = function(nums, k) {
7+
8+
};
9+
10+
const example1 = findKthLargest([3,2,1,5,6,4], 2); // 5
11+
const example2 = findKthLargest([3,2,3,1,2,4,5,5,6], 4); // 4
12+
const example3 = findKthLargest([2,3,1,5,4], 2); // 4
13+
const example4 = findKthLargest([2,3,1,1,5,5,4], 3); // 4
14+
15+
console.log(example1);
16+
console.log(example2);
17+
console.log(example3);
18+
console.log(example4);

example/8.Heap/0295.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
var MedianFinder = function() {
3+
4+
};
5+
6+
/**
7+
* @param {number} num
8+
* @return {void}
9+
*/
10+
MedianFinder.prototype.addNum = function(num) {
11+
12+
};
13+
14+
/**
15+
* @return {number}
16+
*/
17+
MedianFinder.prototype.findMedian = function() {
18+
19+
};
20+
21+
const medianFinder = new MedianFinder();
22+
medianFinder.addNum(1); // arr = [1]
23+
medianFinder.addNum(2); // arr = [1, 2]
24+
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
25+
medianFinder.addNum(3); // arr[1, 2, 3]
26+
medianFinder.findMedian(); // return 2.0
27+
28+
const medianFinder2 = new MedianFinder();
29+
medianFinder2.addNum(1); // arr = [1]
30+
medianFinder2.findMedian(); // return 1.0
31+
medianFinder2.addNum(3); // arr = [1, 3]
32+
medianFinder2.findMedian(); // return 2.0
33+
medianFinder2.addNum(2); // arr[1, 2, 3]
34+
medianFinder2.findMedian(); // return 2.0

example/8.Heap/0347.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
var topKFrequent = function(nums, k) {
7+
8+
};
9+
10+
11+
const example1 = topKFrequent([1, 1, 1, 2, 2, 3], 2); // [1,2]
12+
const example2 = topKFrequent([1], 1); // [1]
13+
const example3 = topKFrequent([3, 3, 5, 5, 5, 6], 2); // [3, 5]
14+
15+
console.log(example1);
16+
console.log(example2);
17+
console.log(example3);

example/8.Heap/0355.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
var Twitter = function() {
2+
3+
};
4+
5+
/**
6+
* @param {number} userId
7+
* @param {number} tweetId
8+
* @return {void}
9+
*/
10+
Twitter.prototype.postTweet = function(userId, tweetId) {
11+
12+
};
13+
14+
/**
15+
* @param {number} userId
16+
* @return {number[]}
17+
*/
18+
Twitter.prototype.getNewsFeed = function(userId) {
19+
20+
};
21+
22+
/**
23+
* @param {number} followerId
24+
* @param {number} followeeId
25+
* @return {void}
26+
*/
27+
Twitter.prototype.follow = function(followerId, followeeId) {
28+
29+
};
30+
31+
/**
32+
* @param {number} followerId
33+
* @param {number} followeeId
34+
* @return {void}
35+
*/
36+
Twitter.prototype.unfollow = function(followerId, followeeId) {
37+
38+
};
39+
40+
/**
41+
* Your Twitter object will be instantiated and called as such:
42+
* var obj = new Twitter()
43+
* obj.postTweet(userId,tweetId)
44+
* var param_2 = obj.getNewsFeed(userId)
45+
* obj.follow(followerId,followeeId)
46+
* obj.unfollow(followerId,followeeId)
47+
*/
48+
49+
const twitter = new Twitter();
50+
twitter.postTweet(1, 5); // User 1 posts a new tweet (id = 5).
51+
twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5]. return [5]
52+
twitter.follow(1, 2); // User 1 follows user 2.
53+
twitter.postTweet(2, 6); // User 2 posts a new tweet (id = 6).
54+
twitter.getNewsFeed(1); // User 1's news feed should return a list with 2 tweet ids -> [6, 5]. Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
55+
twitter.unfollow(1, 2); // User 1 unfollows user 2.
56+
twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5], since user 1 is no longer following user 2.
57+
58+
const twitter2 = new Twitter();
59+
twitter2.postTweet(1, 10); // User 1 posts a new tweet with id = 10.
60+
twitter2.postTweet(2, 20); // User 2 posts a new tweet with id = 20.
61+
twitter2.getNewsFeed(1); // User 1's news feed should only contain their own tweets -> [10].
62+
twitter2.getNewsFeed(2); // User 2's news feed should only contain their own tweets -> [20].
63+
twitter2.follow(1, 2); // User 1 follows user 2.
64+
twitter2.getNewsFeed(1); // User 1's news feed should contain both tweets from user 1 and user 2 -> [20, 10].
65+
twitter2.getNewsFeed(2); // User 2's news feed should still only contain their own tweets -> [20].
66+
twitter2.unfollow(1, 2); // User 1 follows user 2.
67+
twitter2.getNewsFeed(1); // User 1's news feed should only contain their own tweets -> [10].

example/8.Heap/0451.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var frequencySort = function(s) {
6+
7+
};
8+
9+
const example1 = frequencySort("tree"); // "eert"
10+
const example2 = frequencySort("cccaaa"); // "aaaccc"
11+
const example3 = frequencySort("Aabb"); // "bbAa"
12+
13+
console.log(example1);
14+
console.log(example2);
15+
console.log(example3);

example/8.Heap/0502.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number} k
3+
* @param {number} w
4+
* @param {number[]} profits
5+
* @param {number[]} capital
6+
* @return {number}
7+
*/
8+
var findMaximizedCapital = function(k, w, profits, capital) {
9+
10+
};
11+
12+
const example1 = findMaximizedCapital(2, 0, [1,2,3], [0,1,1]); // 4
13+
const example2 = findMaximizedCapital(3, 0, [1,2,3], [0,1,2]); // 6
14+
15+
console.log(example1);
16+
console.log(example2);

example/8.Heap/0621.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {character[]} tasks
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
var leastInterval = function(tasks, n) {
7+
8+
};
9+
10+
const example1 = leastInterval(["A","A","A","B","B","B"], 2); // 8
11+
const example2 = leastInterval(["A","C","A","B","D","B"], 1); // 6
12+
const example3 = leastInterval(["A","A","A", "B","B","B"], 3); // 10
13+
const example4 = leastInterval(["X","X","Y","Y"], 2); // 5
14+
const example5 = leastInterval(["A","A","A","B","C"], 3); // 9
15+
16+
console.log(example1);
17+
console.log(example2);
18+
console.log(example3);
19+
console.log(example4);
20+
console.log(example5);

example/8.Heap/0642.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(example1);

example/8.Heap/0703.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number} k
3+
* @param {number[]} nums
4+
*/
5+
var KthLargest = function(k, nums) {
6+
7+
};
8+
9+
/**
10+
* @param {number} val
11+
* @return {number}
12+
*/
13+
KthLargest.prototype.add = function(val) {
14+
15+
};
16+
17+
var obj = new KthLargest(k, nums)
18+
var param_1 = obj.add(val)
19+
20+
const kthLargest = new KthLargest(3, [4, 5, 8, 2]);
21+
kthLargest.add(3); // return 4
22+
kthLargest.add(5); // return 5
23+
kthLargest.add(10); // return 5
24+
kthLargest.add(9); // return 8
25+
kthLargest.add(4); // return 8
26+
27+
const kthLargest2 = new KthLargest(4, [7, 7, 7, 7, 8, 3]);
28+
kthLargest2.add(2); // return 7
29+
kthLargest2.add(10); // return 7
30+
kthLargest2.add(9); // return 7
31+
kthLargest2.add(9); // return 8

example/8.Heap/0973.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[][]} points
3+
* @param {number} k
4+
* @return {number[][]}
5+
*/
6+
var kClosest = function(points, k) {
7+
8+
};
9+
10+
const example1 = kClosest([[1,3],[-2,2]], 1); // [[-2,2]]
11+
const example2 = kClosest([[3,3],[5,-1],[-2,4]], 2); // [[3,3],[-2,4]]
12+
const example3 = kClosest([[0,2],[2,2]], 1); // [[0,2]]
13+
const example4 = kClosest([[0,2],[2,0],[2,2]], 2); // [[[0,2],[2,0]]
14+
15+
console.log(example1);
16+
console.log(example2);
17+
console.log(example3);
18+
console.log(example4);

example/8.Heap/1046.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} stones
3+
* @return {number}
4+
*/
5+
var lastStoneWeight = function(stones) {
6+
7+
};
8+
9+
const example1 = lastStoneWeight([2,7,4,1,8,1]); // 1
10+
const example2 = lastStoneWeight([1]); // 1
11+
const example3 = lastStoneWeight([2,3,6,2,4]); // 1
12+
const example4 = lastStoneWeight([1,2]); // 1
13+
14+
console.log(example1);
15+
console.log(example2);
16+
console.log(example3);
17+
console.log(example4);

example/8.Heap/1962.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number[]} piles
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var minStoneSum = function(piles, k) {
7+
8+
};
9+
10+
const example1 = minStoneSum([5,4,9], 2); // 12
11+
const example2 = minStoneSum([4,3,6,7], 3); // 12
12+
13+
console.log(example1);
14+
console.log(example2);

example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Better order to solve problems
142142
211. Design Add and Search Words Data Structure
143143
212. Word Search II
144144

145-
### Heap Pt.1
145+
### [Heap Pt.1]((https://github.com/Barklim/leetcode-javascript/tree/master/example/8.Heap))
146146

147147
215. Kth Largest Element in an Array (минхипа максхипа)
148148
703. Kth Largest Element in a Stream

0 commit comments

Comments
 (0)