Design Twitter題目描述代碼實現Kth Smallest Element in a Sorted Matrix題目描述代碼實現
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user’s news feed. Your design should support the following methods:
postTweet(userId, tweetId): Compose a new tweet.getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user’s news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.follow(followerId, followeeId): Follower follows a followee.unfollow(followerId, followeeId): Follower unfollows a followee.Example:Twitter twitter = new Twitter();// User 1 posts a new tweet (id = 5).twitter.postTweet(1, 5);// User 1's news feed should return a list with 1 tweet id -> [5].twitter.getNewsFeed(1);// User 1 follows user 2.twitter.follow(1, 2);// User 2 posts a new tweet (id = 6).twitter.postTweet(2, 6);// User 1's news feed should return a list with 2 tweet ids -> [6, 5].// Tweet id 6 should PRecede tweet id 5 because it is posted after tweet id 5.twitter.getNewsFeed(1);// User 1 unfollows user 2.twitter.unfollow(1, 2);// User 1's news feed should return a list with 1 tweet id -> [5],// since user 1 is no longer following user 2.twitter.getNewsFeed(1);這種方法排名50%。
class Twitter {private: map<int, vector<int>> user_database; // follow map vector<pair<int, int>> post2user; // public: /** Initialize your data structure here. */ Twitter() { } /** Compose a new tweet. */ void postTweet(int userId, int tweetId) { post2user.push_back(make_pair(userId, tweetId)); } /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */ vector<int> getNewsFeed(int userId) { int p_len = post2user.size(); vector<int> res; int f_len = user_database[userId].size(); int count = 0; for(vector<pair<int, int>>::reverse_iterator it = post2user.rbegin(); it != post2user.rend(); it++) { if(count == 10) break; if(it->first == userId) {res.push_back(it->second); count++;} else { for(auto i:user_database[userId]) if(i == it->first) { res.push_back(it->second); count++; break; } } } return res; } /** Follower follows a followee. If the Operation is invalid, it should be a no-op. */ void follow(int followerId, int followeeId) { user_database[followerId].push_back(followeeId); } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ void unfollow(int followerId, int followeeId) { int f_len = user_database[followerId].size(); for(int i = 0; i < f_len; i++) { if(user_database[followerId][i] == followeeId) { user_database[followerId].erase(user_database[followerId].begin() + i); break; } } }};/** * Your Twitter object will be instantiated and called as such: * Twitter obj = new Twitter(); * obj.postTweet(userId,tweetId); * vector<int> param_2 = obj.getNewsFeed(userId); * obj.follow(followerId,followeeId); * obj.unfollow(followerId,followeeId); */這道題目我是用歸并排序來求得。
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:matrix = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15]],k = 8,return 13.Note: You may assume k is always valid, 1 ≤ k ≤
這種做法可以擊敗50%的代碼。
class Solution {public: int kthSmallest(vector<vector<int>>& matrix, int k) { int row = matrix.size(); int col = matrix[0].size(); vector<int> ind(row); int res = INT_MAX; while(k) { res = INT_MAX; int rec = 0; for(int i = 0; i < row; i++) { if(ind[i] < col && matrix[i][ind[i]] < res) { res = matrix[i][ind[i]]; rec = i; } } ind[rec]++; k--; } return res; }};當然這里的思路可以使用隊列來做,這里使用一種叫做優先級隊列的東西,就是會根據自定義的優先級進行插入。
class Solution {public: struct compare { bool operator()(const pair<int,pair<int, int> >& a, const pair<int,pair<int, int> >& b) { return a.first>b.first; } }; int kthSmallest(vector<vector<int>>& arr, int k) { int n=arr.size(),m=arr[0].size(); priority_queue< pair<int,pair<int, int> >, vector<pair<int, pair<int, int> > >, compare > p; for(int i=0;i<n;i++) p.push(make_pair(arr[i][0],make_pair(i,0))); int x=k,ans; while(x--) { int e=p.top().first; int i=p.top().second.first; int j=p.top().second.second; ans=e; p.pop(); if(j!=m-1) p.push(make_pair(arr[i][j+1],make_pair(i,j+1))); } return ans; }};新聞熱點
疑難解答