zoukankan      html  css  js  c++  java
  • 【leetcode】1311. Get Watched Videos by Your Friends

    题目如下:

    There are n people, each person has a unique id between 0 and n-1. Given the arrays watchedVideos and friends, where watchedVideos[i] and friends[i] contain the list of watched videos and the list of friends respectively for the person with id = i.

    Level 1 of videos are all watched videos by your friends, level 2 of videos are all watched videos by the friends of your friends and so on. In general, the level k of videos are all watched videos by people with the shortest path equal to k with you. Given your id and the level of videos, return the list of videos ordered by their frequencies (increasing). For videos with the same frequency order them alphabetically from least to greatest.  

    Example 1:

    Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 1
    Output: ["B","C"] 
    Explanation: 
    You have id = 0 (green color in the figure) and your friends are (yellow color in the figure):
    Person with id = 1 -> watchedVideos = ["C"] 
    Person with id = 2 -> watchedVideos = ["B","C"] 
    The frequencies of watchedVideos by your friends are: 
    B -> 1 
    C -> 2
    

    Example 2:

    Input: watchedVideos = [["A","B"],["C"],["B","C"],["D"]], friends = [[1,2],[0,3],[0,3],[1,2]], id = 0, level = 2
    Output: ["D"]
    Explanation: 
    You have id = 0 (green color in the figure) and the only friend of your friends is the person with id = 3 (yellow color in the figure).

    Constraints:

    • n == watchedVideos.length == friends.length
    • 2 <= n <= 100
    • 1 <= watchedVideos[i].length <= 100
    • 1 <= watchedVideos[i][j].length <= 8
    • 0 <= friends[i].length < n
    • 0 <= friends[i][j] < n
    • 0 <= id < n
    • 1 <= level < n
    • if friends[i] contains j, then friends[j] contains i

    解题思路:先用BFS求出对应level的friends,然后统计这些friends观看的所有videos,最后排序即可。

    代码如下:

    class Solution(object):
        def watchedVideosByFriends(self, watchedVideos, friends, id, level):
            """
            :type watchedVideos: List[List[str]]
            :type friends: List[List[int]]
            :type id: int
            :type level: int
            :rtype: List[str]
            """
            level_list = [float('inf')] * 101
            dic_video = {}
            queue = [(id,0)]
            level_list[id] = 0
    
            while len(queue) > 0:
                p_inx,p_level = queue.pop(0)
                for friend in friends[p_inx]:
                    if level_list[friend] > p_level + 1:
                        queue.append((friend,p_level+1))
                        level_list[friend] = p_level+1
            for inx in range(len(level_list)):
                if level_list[inx] == level:
                    for video in watchedVideos[inx]:
                        dic_video[video] = dic_video.setdefault(video,0) + 1
    
            res = []
    
            releation_list = []
            for key,val in dic_video.iteritems():
                releation_list.append((key,val))
    
            def cmpf(item1,item2):
                if item1[1] != item2[1]:
                    return item1[1] - item2[1]
                if item1[0] > item2[0]:
                    return 1
                elif item1[0] <item2[0]:
                    return -1
                return 0
    
            releation_list.sort(cmp=cmpf)
            for key,val in releation_list:
                res.append(key)
    
            return res
  • 相关阅读:
    C# 封装miniblink 使用HTML/CSS/JS来构建.Net 应用程序界面和简易浏览器
    C# Winform实现炫酷的透明动画界面
    造轮子了!NETCore跨平台UI框架,CPF
    用HTML,Vue+element-UI做桌面UI
    造轮子,模仿WPF的UI框架,还没完善。。。
    C# Winform开发以及控件开发的需要注意的,被人问怕了,都是基础常识
    sublime 下面开发
    《快学Scala》
    linux中bin与sbin目录的作用及区别介绍
    hadoop深入研究:(十三)——序列化框架
  • 原文地址:https://www.cnblogs.com/seyjs/p/12161298.html
Copyright © 2011-2022 走看看