From 6f0d6cdd206a17c6217566a2944e7d39f86ac8a0 Mon Sep 17 00:00:00 2001 From: Jinyoung Jeong <80400463+jyoung2419@users.noreply.github.com> Date: Sat, 17 Oct 2026 23:29:39 +0900 Subject: [PATCH] Create 1311. Get Watched Videos by Your Friends --- .../1311. Get Watched Videos by Your Friends" | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 "leetcode3/\354\240\225\354\247\204\354\230\201/1311. Get Watched Videos by Your Friends" diff --git "a/leetcode3/\354\240\225\354\247\204\354\230\201/1311. Get Watched Videos by Your Friends" "b/leetcode3/\354\240\225\354\247\204\354\230\201/1311. Get Watched Videos by Your Friends" new file mode 100644 index 00000000..be7c9e2d --- /dev/null +++ "b/leetcode3/\354\240\225\354\247\204\354\230\201/1311. Get Watched Videos by Your Friends" @@ -0,0 +1,61 @@ +/** + * @param {string[][]} watchedVideos + * @param {number[][]} friends + * @param {number} id + * @param {number} level + * @return {string[]} + */ +var watchedVideosByFriends = function(watchedVideos, friends, id, level) { + // id 에서 BFS 시작 + // level 만큼 친구 탐색해서 친구 찾기 + // 친구들이 시청한 영상 횟수 map에 저장 후 오름차순 정렬 + + let queue = [id]; + let visited = new Set([id]); + + // 1. BFS로 level 단계까지 친구 탐색 + for (let i = 0; i < level; i++) { + let next = []; + + for (let j = 0; j < queue.length; j++) { + let person = queue[j]; + + for (let k = 0; k < friends[person].length; k++) { + let friend = friends[person][k]; + + if (!visited.has(friend)) { + visited.add(friend); + next.push(friend); + } + } + } + + queue = next; + } + + // 2. 해당 친구들이 본 영상 개수 세기 + let count = new Map(); + + for (let i = 0; i < queue.length; i++) { + let person = queue[i]; + + for (let j = 0; j < watchedVideos[person].length; j++) { + let video = watchedVideos[person][j]; + + count.set(video, (count.get(video) || 0) + 1); + } + } + + // 3. 등장 횟수 → 알파벳순 정렬 + let ans = [...count.keys()]; + + ans.sort((a, b) => { + if (count.get(a) !== count.get(b)) { + return count.get(a) - count.get(b); + } + + return a.localeCompare(b); + }); + + return ans; +};