LintCode 808: Movie Network (BFS和堆好题)

  1. Movie Network

Give some rating of movie (number starting from 0) and their relationship, and relationships can be passed (a and b are related, b and c are related, a and c are also considered to be related). Give every movie’s relationship list.Given a movie numbered S, find the top K movies with the highest rating in the movies associated with S(When the number of movies which associated with S is less than K, output all the movies .You can output them in any order). Does not include this movie.

Example
Example 1:

Input:
ratingArray = [10,20,30,40],
contactRelationship = [[1,3],[0,2],[1],[0]],
S = 0, K = 2
Output: [2,3]
Explanation:
In contactRelationship, [1,3] is associated with 0,[0,2] is associated with 1,[1] is associated 2,[0] is associated with 3.
Finally,Movies numbered [1,2,3] are associated with movie 0, and the order which according to their rating from high to low is [3,2,1], so the output [2,3].
Example 2:

Input:
ratingArray = [10,20,30,40,50,60,70,80,90],
contactRelationship = [[1,4,5],[0,2,3],[1,7],[1,6,7],[0],[0],[3],[2,3],[]],
S = 5, K = 3
Output: [6,7,4]
Explanation:
In contactRelationship,[1,4,5] is associated with 0,[0,2,3] is associated with 1,[1,7] is associated with 2,[1,6,7] is is associated with 3,[0] is associated with 4,[0] is associated with 5,[3] is associated with 6,[2,3] is associated with 7,no moive is associated with 8.
Finally,Movies numbered [0,1,2,3,4,6,7] are associated with movie 5, and the order which according to their rating from high to low is [7,6,4,3,2,1,0]. Notice that movie 8 is not related to movie 5, so it has the highest rating but does not count towards the answer.
Notice
the number of movies is n, and n <= 20000.
We guarantee that the number is 0 ~ n-1.
We guarantee that the numbers of the 2 vertices of an edge both belong to 0 ~ n-1.
We guarantee that the numbers of the edges is m, and m <= 100000.
We guarantee that the rating of each movie is not the same.

解法1:
我用的BFS。注意求最大K个值用minHeap,省空间和时间。
代码如下:

struct Node {
    int index;
    int rating;
    Node (int id = 0, int rt = 0) : index(id), rating(rt) {}
    bool operator < (const Node & a) const {    //minHeap
        return rating > a.rating;
    }
};

class Solution {
public:
    /**
     * @param rating: the rating of the movies
     * @param G: the realtionship of movies
     * @param S: the begin movie
     * @param K: top K rating 
     * @return: the top k largest rating moive which contact with S
     */
    vector<int> topKMovie(vector<int> &rating, vector<vector<int>> &G, int S, int K) {
        int n = rating.size();
        vector<bool> visited(n, false);
        
        int m = G.size(); 
        if (m == 0) return {};
        
        priority_queue<Node> minHeap;
        queue<Node> q;
        
        visited[S] = true;
        
        for (int i = 0; i < G[S].size(); ++i) {
            visited[G[S][i]] = true;
            q.push(Node(G[S][i], rating[G[S][i]]));
        }
        
        while(!q.empty()) {
            Node top = q.front();
            q.pop();
            if (minHeap.size() < K) {
                minHeap.push(top);
            } else if (top.rating > minHeap.top().rating) {
                minHeap.pop();
                minHeap.push(top);
            }
            for (int i = 0; i < G[top.index].size(); ++i) {
                if (!visited[G[top.index][i]]) {
                    q.push(Node(G[top.index][i], rating[G[top.index][i]]));
                    visited[G[top.index][i]] = true;
                }
            }
        }
        vector<int> result;
        while(!minHeap.empty()) {
            result.push_back(minHeap.top().index);
            minHeap.pop();
        }
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值