05 2018 档案
摘要:// TODO: need improve!!! class Log { public: int id; bool start; int timestamp; int comp; // compasation Log() { id = 0; timestamp = 0; comp = 0; ...
阅读全文
摘要:class Solution { public: int leastInterval(vector& tasks, int n) { int freq[26] = {0}; for (auto t : tasks) { freq[t-'A']++; } int max_freq = 0, max_fr...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:class Solution { public: int leastBricks(vector>& wall) { unordered_map m; for (int i = 0; i < wall.size(); i++) for (int j = 0, t = 0; j < wall[i].size() - 1; j++) { ...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:class Solution { public: long max_id = 0; unordered_map id_long; // Encodes a URL to a shortened URL. string encode(string longUrl) { id_long[max_id++] = longUrl; ...
阅读全文
摘要:class Solution { public: bool checkSubarraySum(vector& nums, int k) { unordered_map m; // 从头元素开始的sum,取模为key的index。 m[0] = -1; int _sum = 0; for (int i = 0; i < n...
阅读全文
摘要:class Solution { public: unordered_map dp; int findTargetSumWays(vector& nums, int S) { return helper(nums, S, 0); } int helper(vector& nums, int S, int start) { strin...
阅读全文
摘要:class Solution { public: int totalHammingDistance(vector& nums) { int res = 0; for (int i = 0; i < 32; i++) { int ones = 0; for (int n : nums) { ...
阅读全文
摘要:class Solution { public: int hammingDistance(int x, int y) { int r = x ^ y; return bitCount(r); } int bitCount(int x) { int res = 0; while (x) { ...
阅读全文
摘要:class Solution { public: int splitArray(vector& nums, int m) { int _max = -1, _sum = 0; for (auto n : nums) { if (n > _max) _max = n; _sum += n; ...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:class RandomizedSet { public: vector data; unordered_map m; // /** Initialize your data structure here. */ RandomizedSet() { } /** Inserts a value to the set....
阅读全文
摘要:/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * class NestedInteger { * public: * // Return ...
阅读全文
摘要:class Solution { public: bool increasingTriplet(vector& nums) { int i = INT_MAX, j = INT_MAX; for (int n : nums) { if (n <= i) { i = n; } ...
阅读全文
摘要:class Solution { public: unordered_set res; // DFS may produce dup result, use set to filter. vector removeInvalidParentheses(string s) { // Calculate rmL and rmR for the numbers o...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:class Solution { public: vector res; vector addOperators(string num, int target) { if (num.length() == 0) return res; helper(num, target, "", 0, 0, 0); return res; ...
阅读全文
摘要:class Solution { public: int hIndex(vector& citations) { if (citations.size() == 0) return 0; int i = 0, j = citations.size(); while (i citations[pos]) j...
阅读全文
摘要:class Solution { public: int hIndex(vector& citations) { int n = citations.size(); vector buck(n+1, 0); for (auto c : citations) { if (c > n) c...
阅读全文
摘要:class Solution { public: vector productExceptSelf(vector& nums) { if (nums.size() == 0) return vector(); vector res(nums.size(), 1); for (int i = 1; i = 0; i--) { ...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:Recurision, TLE. DP:
阅读全文
摘要:class Solution { public: int maximalSquare(vector>& matrix) { int m = matrix.size(); if (m == 0) return 0; int n = matrix[0].size(); if (n == 0) return 0; vector> dp(m+1,...
阅读全文
摘要:class Solution { public: vector> getSkyline(vector>& buildings) { vector> v; for (const auto& b : buildings) { v.push_back({b[0], -b[2]}); v.push_back({b[1...
阅读全文
摘要:class Solution { public: int findKthLargest(vector& nums, int k) { int left = 0, right = nums.size() - 1; while (true) { int p = partition(nums, left, right); ...
阅读全文
摘要:class Solution { public: int minSubArrayLen(int s, vector& nums) { int w = 0, _sum = 0, res = INT_MAX; for (int i = 0; i = s) { res = min(res, i-w+1); ...
阅读全文
摘要:class Node { public: char c; bool isWord; unordered_map children; Node(char c) { this->c = c; isWord = false; } }; class Trie { Node* root; Node* find...
阅读全文
摘要:/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class BSTIt...
阅读全文
摘要:class LRUCache { public: list> q; unordered_map>::iterator> m; int cap; LRUCache(int capacity) { cap = capacity; } int get(int key) { if (!m.count(key)) ...
阅读全文
摘要:class Solution { public: bool wordBreak(string s, vector& wordDict) { int n = s.length(); if (n == 0) return false; unordered_set wordSet(wordDict.begin(), wordDict.end()); ...
阅读全文
摘要:/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */ class Solution { public: ...
阅读全文
摘要:class Solution { public: int longestConsecutive(vector& nums) { unordered_set s(nums.begin(), nums.end()); int res = 0; while (!s.empty()) { int p = *(s.begin(...
阅读全文
摘要:class Solution { public: int ladderLength(string beginWord, string endWord, vector& wordList) { unordered_set s(wordList.begin(), wordList.end()); if (!s.count(endWord)) return 0...
阅读全文
摘要:class Solution { public: bool isPalindrome(string s) { int i = 0, j = s.length() - 1; while (i < j) { if (!isalnum(s[i])) i++; else if (!is...
阅读全文
摘要:class Solution { public: int maxProfit(vector& prices) { if (prices.size() == 0) return 0; int res = 0, _min = prices[0]; for (auto p : prices) { if (p < _min)...
阅读全文
摘要:/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL)...
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...
阅读全文
摘要:https://leetcode.com/problems/decode-ways/description/
阅读全文
摘要:https://leetcode.com/problems/subsets-ii/description/
阅读全文
摘要:https://leetcode.com/problems/maximal-rectangle/description/
阅读全文
摘要:https://leetcode.com/problems/word-search/description/
阅读全文
摘要:https://leetcode.com/problems/subsets/description/
阅读全文
摘要:https://leetcode.com/problems/minimum-window-substring/description/
阅读全文
摘要:https://leetcode.com/problems/sort-colors/description/
阅读全文
摘要:https://leetcode.com/problems/simplify-path/description/
阅读全文
摘要:https://leetcode.com/problems/merge-intervals/description/
阅读全文
摘要:https://leetcode.com/problems/powx-n/description/
阅读全文
摘要:https://leetcode.com/problems/group-anagrams/description/
阅读全文
摘要:https://leetcode.com/problems/multiply-strings/description/
阅读全文
摘要:https://leetcode.com/problems/count-and-say/description/
阅读全文
摘要:https://leetcode.com/problems/reverse-nodes-in-k-group/description/
阅读全文
摘要:https://leetcode.com/problems/merge-k-sorted-lists/description/
阅读全文
摘要:https://leetcode.com/problems/valid-parentheses/description/
阅读全文
摘要:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
阅读全文
摘要:https://leetcode.com/problems/3sum/description/
阅读全文
摘要:https://leetcode.com/problems/regular-expression-matching/description/ DP: recursion
阅读全文
摘要:https://leetcode.com/problems/excel-sheet-column-title/description/
阅读全文
摘要:https://leetcode.com/problems/merge-sorted-array/description/
阅读全文
摘要:https://leetcode.com/problems/most-profit-assigning-work/description/
阅读全文
摘要:https://leetcode.com/submissions/detail/153740275/class Solution { public: int fourSumCount(vector& A, vector& B, vector& C, vector& D) { unordered_map CD_freq; for (auto i : C) ...
阅读全文
摘要:https://leetcode.com/problems/path-sum-iii/description/
阅读全文
摘要:https://leetcode.com/problems/partition-equal-subset-sum/description/
阅读全文
摘要:https://leetcode.com/problems/pacific-atlantic-water-flow/description/
阅读全文
摘要:https://leetcode.com/problems/can-place-flowers/description/ First version: V2:
阅读全文
摘要:https://leetcode.com/problems/network-delay-time/description/, we need to go to every node from K, calculate the max weight. Dijkstra: BFS: Bellman Fo
阅读全文
摘要:DFS: Topological sort:
阅读全文
摘要:https://leetcode.com/problems/course-schedule/description/ DFS, detect cycle. For each node, we have 3 states during DFS: 1) un-visited, 2) visiting,
阅读全文
摘要:https://leetcode.com/problems/course-schedule-ii/description/ DFS:
阅读全文
摘要:https://leetcode.com/problems/random-pick-index/description/ Reservoir Sampling
阅读全文
摘要:https://leetcode.com/problems/daily-temperatures/description/
阅读全文
摘要:https://leetcode.com/problems/k-th-symbol-in-grammar/description/
阅读全文
摘要:https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/ Use DFS. We need to choose k group of numbers (each group sum to s). The s
阅读全文
摘要:https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/
阅读全文
摘要:https://leetcode.com/problems/accounts-merge/description/
阅读全文
摘要:https://leetcode.com/problems/friend-circles/description/
阅读全文
摘要:https://leetcode.com/problems/number-of-islands/description/
阅读全文
摘要:https://leetcode.com/problems/redundant-connection/description/ Use map to do Union Find.
阅读全文
摘要:https://leetcode.com/problems/sort-characters-by-frequency/description/
阅读全文
摘要:https://leetcode.com/problems/non-overlapping-intervals/description/ Greedy. Sort by (start,-end). Scan from left to right, if we see 2 intervals over
阅读全文
摘要:When we see ordering, consider stack. When we process item one by one, but I can't process current item, we move to next item (and that item might dep
阅读全文
摘要:https://leetcode.com/problems/next-greater-element-ii/description/
阅读全文
摘要:https://leetcode.com/problems/next-greater-element-i/description/
阅读全文
摘要:https://leetcode.com/problems/shortest-completing-word/description/
阅读全文
摘要:https://leetcode.com/problems/01-matrix/description/ Use BFS to search. First I tried to BFS every 0, the problem is each 1 may need to be visited for
阅读全文
摘要:https://leetcode.com/problems/find-eventual-safe-states/description/
阅读全文
摘要:https://leetcode.com/problems/maximum-width-of-binary-tree/description/
阅读全文
摘要:https://leetcode.com/problems/top-k-frequent-words/description/
阅读全文
摘要:https://leetcode.com/problems/expressive-words/description/
阅读全文
摘要:https://leetcode.com/problems/valid-parenthesis-string/description/ 这个题的难点在增加了*,*可能是(也可能是)。是(的前提是:右边有多余的)。是)的前提是:左边有多余的(。所以他们的位置信息是重要的。 Valid Parenthe
阅读全文
摘要:Brute force (time out) Preprocess S:
阅读全文
摘要:DP
阅读全文
摘要:class Solution { public: double largestSumOfAverages(vector& A, int K) { int n = A.size(); // dp[k][i]: largestSumOfAverages of [0,i] with up to k partitions // d...
阅读全文
摘要:https://leetcode.com/problems/cheapest-flights-within-k-stops/description/ DFS (slow) BFS (seems not able to use BFS as below, we may have to record e
阅读全文
摘要:https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/
阅读全文
摘要:When to use binary search: * 我们知道解空间在一个区间 [i,j] * valid解是解空间的一个点(也可以看作是只有一个点的range)或者一个range的起始点或者结束点 * range之外的点都不是valid解 想法一般都两种: * 直接找到解 (比如classic
阅读全文