国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

355. Design Twitter / 378. Kth Smallest Element in a Sorted Matrix

2019-11-10 17:09:44
字體:
供稿:網(wǎng)友

Design Twitter題目描述代碼實(shí)現(xiàn)Kth Smallest Element in a Sorted Matrix題目描述代碼實(shí)現(xiàn)

355. Design Twitter

題目描述

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);

代碼實(shí)現(xiàn)

這種方法排名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); */

378. Kth Smallest Element in a Sorted Matrix

這道題目我是用歸并排序來求得。

題目描述

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 ≤ n2.

代碼實(shí)現(xiàn)

這種做法可以擊敗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; }};

當(dāng)然這里的思路可以使用隊列來做,這里使用一種叫做優(yōu)先級隊列的東西,就是會根據(jù)自定義的優(yōu)先級進(jìn)行插入。

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; }};
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 禄丰县| 漾濞| 朔州市| 三原县| 定边县| 玉山县| 孟连| 镇赉县| 怀化市| 南城县| 扎赉特旗| 富源县| 新津县| 商洛市| 司法| 禹州市| 太仆寺旗| 镇坪县| 凉城县| 平塘县| 巧家县| 弥渡县| 阿拉善左旗| 阳西县| 芒康县| 阿合奇县| 菏泽市| 横峰县| 长春市| 夏津县| 宝丰县| 乌兰浩特市| 鄂州市| 六枝特区| 九龙坡区| 威宁| 平南县| 嘉黎县| 隆德县| 高淳县| 福安市|