Posts

Showing posts with the label data structure

501. Design Twitter

典型的一道看似很复杂,静下来钻进去看,就发现有思路的题目。 考察数据结构的运用,以及要定义一个新的node节点。 Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text) . Post a tweet. getTimeline(user_id) . Get the given user's most recently 10 tweets posted by himself, order by timestamp from most recent to least recent. getNewsFeed(user_id) . Get the given user's most recently 10 tweets in his news feed (posted by his friends and himself). Order by timestamp from most recent to least recent. follow(from_user_id, to_user_id) . from_user_id followed to_user_id. unfollow(from_user_id, to_user_id) . from_user_id unfollowed to to_user_id. Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input: postTweet(1, "LintCode is Good!!!") getNewsFeed(1) getTimeline(1) follow(2, 1) getNewsFeed(2) unfollow(2, 1) getNewsFeed(2) Output: 1 [1] [1] [1] [] Example 2: Input: postTweet(1, "LintCode ...

360. Sliding Window Median

Code ( Language :C++) ( Judger :cloudjudge-cluster-5) Edit class Solution { public : /** * @param nums: A list of integers * @param k: An integer * @return: The median of the element inside the window at each moving */ vector < int > medianSlidingWindow( vector < int > &nums, int k) { // write your code here vector < int > res; multiset < int > small, large; for ( int i = 0 ; i < nums.size(); ++i) { // remove if (i >= k) { if (small.count(nums[i - k])) { small.erase(small.find(nums[i - k])); } else if (large.count(nums[i - k])) { large.erase(large.find(nums[i - k])); } } if (small.size() <= large.size()) { if (large.empty() || nums[i] <= *large.begin()) { small....

81. Find Median from Data Stream

Code ( Language :C++) ( Judger :cloudjudge-cluster-5) Edit class Solution { private : priority_queue< int > small, large; public : /** * @param nums: A list of integers * @return: the median of numbers */ //small和large,分别存小的一半和大的一半。小的一半的个数等于大的一半个数+1或大的一半个数。 void addNum ( int num) { small.push(num); large.push(-small.top()); small.pop(); if (small.size() < large.size()){ small.push(-large.top()); large.pop(); } return ; } int findMedian () { if (small.size() > large.size()){ return small.top(); } else { //return (small.top() - large.top())/2; return small.top(); } } vector < int > medianII( vector < int > &nums) { // write your code here vector < int > res; for ( int i = 0 ; i < nums.size(); i++){ ...

132. Word Search II

Code ( Language :C++) ( Judger :ip-172-31-21-252) Edit class trieNode { public : trieNode *child[ 26 ]; string word; trieNode(){ for ( int i = 0 ; i < 26 ; i++){ child[i] = NULL ; } word = "" ; } }; class trieTree { public : trieNode *root; trieTree(){ root = new trieNode(); } void insert ( string word) { trieNode *copy = root; int size = word.size(); if (size == 0 ){ return ; } for ( int i = 0 ; i < size; i++){ int idx = word[i] - 'a' ; if (copy->child[idx] == NULL ){ copy->child[idx] = new trieNode(); } copy = copy->child[idx]; } copy->word = word; return ; } }; class Solution { public : /** * @param board: A list of lists of character * @param words: A list of string ...

362. Sliding Window Maximum

class Solution { public : /** * @param nums: A list of integers. * @param k: An integer * @return: The maximum number inside the window at each moving. */ vector < int > maxSlidingWindow( vector < int > &nums, int k) { // write your code here // max stack // 开始想用stack,但发现这里是先进先出。 // 要用queue,但为了要左右两端都可以pop,要用double end queue // //stack<int> stk; //stack<int> maxStk; std :: deque < int > dq; const int size = nums.size(); vector < int > res; if (k == 0 ){ return res; } if (size < k){ int maxN = INT_MIN; for ( auto i : nums){ maxN = max(maxN, i); } res.push_back(maxN); return res; } for ( int i = 0 ; i < k; i++){ while (!dq.empty() && nums[i] > dq.back()){ ...

Train Compartment Problem

Description 中文 English There is a railroad track and a transfer station in the middle of the railroad track. You can imagine a structure like "T". You can think of this transfer station as a stack - the carriage is FILO(first-in last-out). There are  n  train carriages arranged on the rails on the right side of the transfer station from  1  to  n . Now we want to transfer these  n  carriages to the rail on the left side of the transfer station, to be in the order of the array  arr . And each carriage enters the transfer station at most once. Your task is to determine if the order of the  arr  can be reached. If possible, return the number of cars in the transfer station. If not, return  -1 . n ≤ 10^5 Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input: arr = [4,5,3,2,1] Output: 3 Explanation: 1 enter the transfer station 2 enter ...