Posts

Showing posts with the label 有意思

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

94. Binary Tree Maximum Path Sum

class Solution { public : /** * @param root: The root of binary tree. * @return: An integer */ struct resType { int root2Any; int any2Any; }; int maxPathSum (TreeNode * root) { // write your code here if (root == NULL ){ return 0 ; } resType res = pathSum(root); return res.any2Any; } resType pathSum (TreeNode *root) { resType res; res.root2Any = INT_MIN; res.any2Any = INT_MIN; if (root == NULL ){ return res; } resType left = pathSum(root->left); resType right = pathSum(root->right); res.root2Any = max(max(left.root2Any, right.root2Any), 0 ) + root->val; res.any2Any = max(max(left.any2Any, right.any2Any), root->val + max(left.root2Any, 0 ) + max(right.root2Any, 0 )); return res; } };

105. Copy List with Random Pointer

不用hash map节省 space!!! Code ( Language :C++) ( Judger :ip-172-31-12-4) Edit /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) : label(x), next(NULL), random(NULL) {} * }; */ class Solution { public : /** * @param head: The head of linked list with a random pointer. * @return: A new head of a deep copy of the list. */ RandomListNode * copyRandomList (RandomListNode *head) { // write your code here RandomListNode *copy = head; if (head == NULL ){ return head; } // add new node while (copy != NULL ){ RandomListNode *tmp = copy->next; copy->next = new RandomListNode(copy->label); copy->next->next = tmp; copy = tmp; } copy = head; // add random pointer while...

AM 1252. Queue Reconstruction by Height

Code ( Language :C++) ( Judger :cloudjudge-cluster-4) Edit class Solution { public : /** * @param people: a random list of people * @return: the queue that be reconstructed */ vector < vector < int >> reconstructQueue( vector < vector < int >> &people) { // write your code here sort(people.begin(), people.end(), comparator); vector < vector < int >> resultVector; for ( vector < int > cur : people) { resultVector.insert(resultVector.begin() + cur[ 1 ], cur); } return resultVector; } static bool comparator ( vector < int > p1, vector < int > p2) { return (p1[ 0 ] > p2[ 0 ]) || (p1[ 0 ] == p2[ 0 ] && p1[ 1 ] < p2[ 1 ]); } }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Close

AM 1189. Minesweeper

Code ( Language :C++) ( Judger :cloudjudge-cluster-7) Edit BFS iteration class Solution { public : /** * @param board: a board * @param click: the position * @return: the new board */ const vector < int > dx = { -1 , 1 , 0 , 0 , -1 , -1 , 1 , 1 }; const vector < int > dy = { 0 , 0 , -1 , 1 , -1 , 1 , -1 , 1 }; const int dir = 8 ; vector < vector < char >> updateBoard( vector < vector < char >>& board, vector < int >& click) { // Write your code here const int m = board.size(); if (m == 0 ){ return board; } const int n = board[ 0 ].size(); // 'M' // use BFS std :: queue < vector < int >> q; q.push(click); while (!q.empty()){ click = q.front(); q.pop(); if (board[click[ 0 ]][click[ 1 ]] == 'M' ){ ...

AM 395. Coins in a Line II

Code ( Language :C++) ( Judger :cloudjudge-cluster-5) Edit class Solution { public : /** * @param values: a vector of integers * @return: a boolean which equals to true if the first player will win */ bool firstWillWin ( vector < int > &values) { // write your code here // 这个DP有些不一般。 // 1,从后往前。 // 2,博弈 const int size = values.size(); if (size <= 2 ){ return true ; } int sum = 0 ; for ( auto i : values){ sum += i; } vector < int > dp(size, 0 ); // 从该index开始能取得最大值。 dp[size - 1 ] = values[size - 1 ]; dp[size - 2 ] = values[size - 2 ] + values[size - 1 ]; dp[size - 3 ] = values[size - 3 ] + values[size - 2 ]; for ( int i = size - 4 ; i >= 0 ; i--){ // pick one int pick1 = values[i] + min(dp[i + 2 ], dp[i + 3 ]); int pick2 = 0 ; ...