摘要:A trickier binary search one.//////////////class Solution { int lower_bound_vert(vector>& m, int col, int i, int j, int target) { int...
阅读全文
07 2015 档案
摘要:Classic.. just classic. It is a mix of Greedy and DP. The naive DP is, iterate over all [1..Bi] which is O(n^3). However, deeper thought into the prob...
阅读全文
摘要:Intuition: 2D DP. Basic idea: compose square at dp[i][j] from dp[i-1][j-1]. You need 2 facility 2D matrix: accumulated horizontal\vertical number of 1...
阅读全文
摘要:Interesting one.. It is more about data structure design actually. After you figure out how to represent cells, the DP formula will be very intuitive ...
阅读全文
摘要:Monotonic Queue is getting more popular, and finally LeetCode put it on.Typical Solution: element in array will be pushed\popped in\from a sorted data...
阅读全文
摘要:Actually I think it should belong to category of 'Bits Manipulation'.. but still, a really good one.My first reaction was, how to derive from a ^ (b #...
阅读全文
摘要:Question 1: without division. We can simply compose left\right accumulated product arrays:typedef long long LL;class Solution {public: vector produ...
阅读全文
摘要:Brutal-force solution is not hard to think about. But linear space input usually indicates O(n) DP solution.State design: dp[i]: total sum until index...
阅读全文
摘要:An interview problem I encountered years ago.. 1ACclass Solution {public: void deleteNode(ListNode* node) { if(!node) return; Lis...
阅读全文
摘要:My first thought made it unnecessarily difficult. Well, the correct solution falls into a typical pattern: use DP to calc cumulative results of each c...
阅读全文
摘要:First please note, it is BST. So it is about value compare.class Solution {public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, Tree...
阅读全文
摘要:A typical bucketing strategy, but it is probably the most complex one..Basically I refer tothissubmission.
阅读全文
摘要:O(n) time O(n) space solution using stack:class Solution {public: bool isPalindrome(ListNode* head) { if (!head || !head->next) return t...
阅读全文
摘要:Marked as mathematics problem, but if you are able to divide 1s into several cases, the AC code becomes natural.class Solution { unordered_map memo...
阅读全文
摘要:First I was stuck at how to represent state of "add all except i".. but after checking editorial, it is simply inverted Coin Change problem..#include ...
阅读全文
摘要:Very similar as "Implement Stack using Queues".class Queue { stack _s0; stack _s1; int _front; public: // Push element x to the back of...
阅读全文
摘要:Just one line. Bit-wise ops:class Solution {public: bool isPowerOfTwo(int n) { return (n > 0) && (n & (n - 1)) == 0; }};
阅读全文
摘要:1AC. 1D DP + Sieving#include #include #include #include #include #include #include #include #include #include #include using namespace std;int ways(in...
阅读全文
摘要:Good one.. marked as DP, so I was striking to work out a DP formula... well, not all DP have a formula - one type of DP is called memorized search: th...
阅读全文
摘要:Simply to utilize BST's property. The two-pass code below can be extended to support frequent insert\delete.struct SNode{ SNode(int rcnt) : cnt(rcn...
阅读全文
摘要:First I thought it should be solved using DP, and I gave a standard O(n^2) solution:#include #include #include #include using namespace std;#define RE...
阅读全文