Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][javascript] design-twitter #1588

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 9 additions & 83 deletions 多语言解法代码/solution_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -19037,26 +19037,28 @@ var Twitter = function() {
const followedUserSet = user.followedUserSet;
// 每个用户的 tweet 是一条按时间排序的链表
// 现在执行合并多条有序链表的逻辑,找出时间线中的最近 10 条动态
const pq = new PriorityQueue((a, b) => {
// 按照每条 tweet 的发布时间降序排序(最近发布的排在事件流前面)
return b.timestamp - a.timestamp;
const pq = new PriorityQueue({
compare: (a, b) => {
// 按照每条 tweet 的发布时间降序排序(最近发布的排在事件流前面)
return b.timestamp - a.timestamp;
}
});
// 该用户自己的 tweet 也在时间线内
if (user.tweetHead !== null) {
pq.offer(user.tweetHead);
pq.enqueue(user.tweetHead);
}
for (const other of followedUserSet) {
if (other.tweetHead !== null) {
pq.offer(other.tweetHead);
pq.enqueue(other.tweetHead);
}
}
// 合并多条有序链表
let count = 0;
while (!pq.isEmpty() && count < 10) {
const tweet = pq.poll();
const tweet = pq.dequeue();
res.push(tweet.id);
if (tweet.next !== null) {
pq.offer(tweet.next);
pq.enqueue(tweet.next);
}
count++;
}
Expand Down Expand Up @@ -19088,82 +19090,6 @@ var Twitter = function() {
follower.unfollow(followee);
};
};

// 优先队列实现
class PriorityQueue {
constructor(comparator) {
this.heap = [];
this.comparator = comparator;
}

/**
* 上浮操作
* @param {number} index - 上浮节点的下标
*/
swim(index) {
let currentIndex = index;
while (currentIndex > 0) {
const parentIndex = Math.floor((currentIndex - 1) / 2);
if (this.comparator(this.heap[currentIndex], this.heap[parentIndex]) >= 0) {
break;
}
[this.heap[currentIndex], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[currentIndex]];
currentIndex = parentIndex;
}
}

/**
* 下沉操作
* @param {number} index - 下沉节点的下标
*/
sink(index) {
let currentIndex = index;
while (currentIndex * 2 + 1 < this.heap.length) {
const leftIndex = currentIndex * 2 + 1;
const rightIndex = currentIndex * 2 + 2 < this.heap.length ? currentIndex * 2 + 2 : leftIndex;
const smallerIndex = this.comparator(this.heap[leftIndex], this.heap[rightIndex]) <= 0 ? leftIndex : rightIndex;
if (this.comparator(this.heap[currentIndex], this.heap[smallerIndex]) <= 0) {
break;
}
[this.heap[currentIndex], this.heap[smallerIndex]] = [this.heap[smallerIndex], this.heap[currentIndex]];
currentIndex = smallerIndex;
}
}

/**
* 插入元素
* @param {*} value - 插入的值
*/
offer(value) {
this.heap.push(value);
this.swim(this.heap.length - 1);
}

/**
* 弹出堆顶元素
* @return {*} 堆顶元素
*/
poll() {
if (this.size() === 0) {
return null;
}
if (this.size() === 1) {
return this.heap.pop();
}
const top = this.heap[0];
this.heap[0] = this.heap.pop();
this.sink(0);
return top;
}

/**
* 获取堆大小
* @return {number} 堆大小
*/
size() {
return this.heap.length;
}
}
```

```python
Expand Down