摘要:A natural thought is brutal-force. But as you may have already thought of, there must be a smarter one. And yes there is.Think like this: XOR gives yo...
阅读全文
08 2014 档案
摘要:Welcome, T******7! You have solved151 / 151problems.It is only the first step...
阅读全文
摘要:Man it took me numerous submissions to get AC, and I referred to this link:http://yihuad.blogspot.com/2013/11/word-ladder-ii-leetcode.htmlIdea is pret...
阅读全文
摘要:My intuition is flood-fill the BFS solution, which is O(n^4); and then I figured out a DP solution which is O(n^4).. So I googled some hints: it can b
阅读全文
摘要:I have to admit that I'm still an algorithm rookie: in front of a problem, I got lost on what the nature of the problem is.. usually I made everything...
阅读全文
摘要:A really classic 2D DP problem. And I'm glad that I figured out the transfer function without checking any other resources.After that, everything is s...
阅读全文
摘要:First I thought of a DP solution.. but TLE. So there must be a O(n). I thought of Mono-Queue.. but looks too complex.So what is the naive solution? O(...
阅读全文
摘要:1A! We get median of each array and compare them, then we know which half should be disguarded and how many should be disguarded.class Solution {publi...
阅读全文
摘要:A classic and representative DP problem. To be revised later. Quite a few interesting details to think about.class Solution {public: int numDistinc...
阅读全文
摘要:1A! This is actually a basic question in Combinatorics: if at digit[k] = n, it contributes (k-1)! * n to the targeted index.class Solution {public: ...
阅读全文
摘要:找到自己切题的主要障碍了:试图刚读完题就foresee到最后的solution,其实就是妄图利用比较省事的感性直接超越逻辑推导的理性:其实就是懒。超验感性实际上是建立在大量理性的思考与实践上的。这话不是我说的,出自一位顶尖数学家之口。更别说偶了。
阅读全文
摘要:Nothing but admire: http://fisherlei.blogspot.com/2013/11/leetcode-linked-list-cycle-ii-solution.htmlclass Solution {public: ListNode *detectCycle(...
阅读全文
摘要:Refer: http://blog.unieagle.net/2012/12/05/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-and-sell-stock-iii%EF%BC%8C%E4%B8%80%E7%BB%B4%E5%8A%A8%...
阅读全文
摘要:Similar strategy could be applied to 4Sum, as 2sum to 3sum, but that'll be O(n^3). Instead of 3 + 1, we can also divide it into 2 + 2. So the problem ...
阅读全文
摘要:Simply a variation of 3Sum:class Solution {public: int threeSumClosest(vector &num, int target) { int ret = 99999999999; std::sort(nu...
阅读全文
摘要:Now that you've solved 2sum, 3sum can be easily converted as 2sum.Also please note the while loops for i1 and i2: they are to make sure no duplicates....
阅读全文
摘要:O(n^2) is a naive solution. As rule of thumb, there must be O(n) soluion. Yes - Greedy.We assume widest container contains the most water, greedily, b...
阅读全文
摘要:-!5. Trie\Suffix Tree:http://blog.csdn.net/v_july_v/article/details/6897097 Trie: think about how you used King Dict by typing; Suffix Tree: all suff...
阅读全文
摘要:Simply DFS + Backtrace, as N-Queenclass Solution {public: vector> rows; vector> cols; vector>> subboxHm; bool isValid(char c, int i, int j...
阅读全文
摘要:Here another memory for speed implementation:class Solution {public: bool isValidSudoku(vector > &board) { size_t row_cnt = board.size(); ...
阅读全文
摘要:Main reference: http://zhaohongze.com/wordpress/2013/12/10/leetcode-candy/I was confused by the constraint on BOTH sides of an element. Strategy is: s...
阅读全文
摘要:http://yucoding.blogspot.com/2012/12/leetcode-question-13-binary-tree.htmlUsually for tree problems, it is either recursion or traversal. For this one...
阅读全文
摘要:I got a DP solution first which is O(n^2). Apparently it is not a optimized one - think about: it is linear value space. There must be O(n) solution.A...
阅读全文
摘要:1A. It is a design problem actually. It tests your ability to connect your knowledge of data structures & algorithms, with real world problems.class L...
阅读全文
摘要:Just take care of corner cases!class Solution {public: vector fullJustify(vector &words, int L) { vector ret; int startInx = 0; ...
阅读全文
摘要:It is not as easy as I thought it to be, mostly because of time\space limitation. And actually that's the punch line of this problemMy intuition was D...
阅读全文
摘要:Main reference:http://zhaohongze.com/wordpress/2014/01/04/leetcode-minimum-window-substring/The ART of counting. So difficult and beautiful problem. I...
阅读全文
摘要:It took me numerous submissions to get AC.... until I checked this article:http://leetcode.com/2011/09/regular-expression-matching.htmlVery clean idea...
阅读全文
摘要:Problem is, find all values that share the same key value. Similar as another LeetCode problem, hashmap is a good choice.http://yucoding.blogspot.com/...
阅读全文
摘要:I briefly took a look at:http://yucoding.blogspot.com/2013/01/leetcode-question-27-interleaving-string.htmlAnd I got 1A :)This is the best problem to ...
阅读全文
摘要:A so juicy and interesting problem! Recommend to everyone!A final solution may not jump into your mind immediately, of course, DP is not a trivial top...
阅读全文
摘要:What a juicy problem to solve :) My intuition is DP, but failed with TLE. Then I referred to:http://leetcode.com/2011/05/longest-substring-without-rep...
阅读全文
摘要:Here is the most elegant code I've seen to this problem:http://yucoding.blogspot.com/2013/02/leetcode-question-123-wildcard-matching.htmlSimply wow. I...
阅读全文
摘要:No limit to transaction count, so it is a recursive sum calculation. Take care of boundary condition.class Solution {public: int maxProfit(vector &...
阅读全文
摘要:Also the very first problem on EPI.class Solution {public: int maxProfit(vector &prices) { size_t len = prices.size(); if (len = pric...
阅读全文
摘要:"that are all of the same length" is the key. This statement makes everything much simpler. And, please take care that L may contain duplicated string...
阅读全文
摘要:Since it is a "return all" problem, we can use DFS. But brutal DFS got a TLE. So pruning is required.I referred tohttp://fisherlei.blogspot.com/2013/1...
阅读全文
摘要:My first solution was DFS - TLE. Apparently, DFS with TLE usually means DP.1D DP + Rolling Array:class Solution {public: bool wordBreak(string s, u...
阅读全文
摘要:I think it is natural for an algorithm rookie that when facing a challenging problem, a rookie may fall into her\his algorithm dictionary - she focuse...
阅读全文
摘要:Your intuition would tell you that there's a O(n) solution. Actually it is another stack-based problem to solve.class Solution {public: struct Rec ...
阅读全文
摘要:It took me +20 submissions to get AC... Actually the statement is too vague. I would rather ask for requirements f2f.Apparently the code below can be ...
阅读全文
摘要:2D DP is an intuitive solution of course, but I got an MLE error, so I simplified it into a 1D DP:class Solution {public: void goDp(vector &dp, int...
阅读全文
摘要:Another list manipulation problem.class Solution {public: ListNode *reverseKGroup(ListNode *head, int k) { if (!head) return head; if...
阅读全文
摘要:An example of in-order traversal application. My intuition is that, we have to serialize it into an array and check, but in-order traversal does exact...
阅读全文
摘要:DFS + Backtrace + Pruningclass Solution {public: vector > visited; bool checkPos(vector > &board, char c, int currX, int currY) { ...
阅读全文
摘要:An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1])class Solution {public: int minPathSum(vector > &grid) ...
阅读全文
摘要:Corner cases!class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if (!head) return head; if (!head->next) return hea...
阅读全文
摘要:Nothing special. A typical list manipulation problem.class Solution {public: ListNode *partition(ListNode *head, int x) { if (!head) return ...
阅读全文
摘要:It is all about corner cases.class Solution {public: vector retv; bool isValid(string &sub) { unsigned cnt = sub.length(); if (...
阅读全文
摘要:Lexicographicallyalgorithms:1. Iterate array from back to front, and find the first decreasing point: 1,2,4,3 -- 42. Iterate array from back to front,...
阅读全文
摘要:First I implemented it by QuickSort, but got a TLE:class Solution {public: struct Pair { Pair(ListNode *pS, ListNode *pE) : pStart(pS), p...
阅读全文
摘要:Greedy, Greedy, Greedy.. It is all about maximum interval update.One trick is, we start looping over each element from the one nearest to end to farth...
阅读全文
摘要:A simulation problem. We simple walk through all reachable items until we cannot proceed.class Solution {public: bool canJump(int A[], int n) { ...
阅读全文
摘要:Next time you see a numeric problem has a too straightforward solution, think about optimized one.Like this one: recursion\iteration is tooo slow. SoD...
阅读全文

浙公网安备 33010602011771号