Skip to content

Commit 0e0231d

Browse files
committed
Add solution #1311
1 parent ba38df9 commit 0e0231d

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,222 LeetCode solutions in JavaScript
1+
# 1,223 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -992,6 +992,7 @@
992992
1307|[Verbal Arithmetic Puzzle](./solutions/1307-verbal-arithmetic-puzzle.js)|Hard|
993993
1309|[Decrypt String from Alphabet to Integer Mapping](./solutions/1309-decrypt-string-from-alphabet-to-integer-mapping.js)|Easy|
994994
1310|[XOR Queries of a Subarray](./solutions/1310-xor-queries-of-a-subarray.js)|Medium|
995+
1311|[Get Watched Videos by Your Friends](./solutions/1311-get-watched-videos-by-your-friends.js)|Medium|
995996
1313|[Decompress Run-Length Encoded List](./solutions/1313-decompress-run-length-encoded-list.js)|Easy|
996997
1317|[Convert Integer to the Sum of Two No-Zero Integers](./solutions/1317-convert-integer-to-the-sum-of-two-no-zero-integers.js)|Easy|
997998
1318|[Minimum Flips to Make a OR b Equal to c](./solutions/1318-minimum-flips-to-make-a-or-b-equal-to-c.js)|Medium|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1311. Get Watched Videos by Your Friends
3+
* https://leetcode.com/problems/get-watched-videos-by-your-friends/
4+
* Difficulty: Medium
5+
*
6+
* There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos
7+
* and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the
8+
* list of friends respectively for the person with id = i.
9+
*
10+
* Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched
11+
* videos by the friends of your friends and so on. In general, the level k of videos are all
12+
* watched videos by people with the shortest path exactly equal to k with you. Given your id
13+
* and the level of videos, return the list of videos ordered by their frequencies (increasing).
14+
* For videos with the same frequency order them alphabetically from least to greatest.
15+
*/
16+
17+
/**
18+
* @param {string[][]} watchedVideos
19+
* @param {number[][]} friends
20+
* @param {number} id
21+
* @param {number} level
22+
* @return {string[]}
23+
*/
24+
var watchedVideosByFriends = function(watchedVideos, friends, id, level) {
25+
const visited = new Set([id]);
26+
let queue = [id];
27+
let currentLevel = 0;
28+
29+
while (queue.length && currentLevel < level) {
30+
const nextQueue = [];
31+
queue.forEach(person => friends[person].forEach(friend => {
32+
if (!visited.has(friend)) {
33+
visited.add(friend);
34+
nextQueue.push(friend);
35+
}
36+
}));
37+
queue = nextQueue;
38+
currentLevel++;
39+
}
40+
41+
const videoFrequency = new Map();
42+
queue.forEach(person => watchedVideos[person].forEach(video => {
43+
return videoFrequency.set(video, (videoFrequency.get(video) || 0) + 1);
44+
}));
45+
46+
return [...videoFrequency.entries()]
47+
.sort((a, b) => a[1] === b[1] ? a[0].localeCompare(b[0]) : a[1] - b[1])
48+
.map(([video]) => video);
49+
};

0 commit comments

Comments
 (0)