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

首頁 > 學院 > 開發設計 > 正文

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

2019-11-09 19:08:30
字體:
來源:轉載
供稿:網友

Design Twitter題目描述代碼實現Kth Smallest Element in a Sorted Matrix題目描述代碼實現

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

代碼實現

這種方法排名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.

代碼實現

這種做法可以擊敗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; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郧西县| 阿拉善右旗| 嘉峪关市| 金坛市| 武鸣县| 安化县| 华阴市| 陇川县| 双牌县| 澄城县| 双峰县| 南乐县| 宣威市| 云和县| 乌恰县| 镇赉县| 延川县| 乌鲁木齐市| 承德市| 洞口县| 伽师县| 丽江市| 汉阴县| 娄底市| 金坛市| 丰原市| 尚义县| 鄱阳县| 海兴县| 洛隆县| 江都市| 萨嘎县| 肇东市| 兴山县| 汶川县| 麟游县| 阳东县| 元江| 绥滨县| 长沙市| 洛阳市|