Month: November 2018
947 Most Stones Removed with Same Row or Column
Link: Most Stones Removed with Same Row or Column Thought When I saw the question, I realized its a Union Find question – For any union, I can keep only one stone form it. Then it convert to a count how many unique union finds exist question. So, only need to count all unique union find, […]
Read More → 947 Most Stones Removed with Same Row or Column946 Validate Stack Sequences
Link: validate stack sequences Thought Its pretty straight forward, just stimulate a stack and push/pop as the sequence shows. Code Runtime Time: O(n) Space: O(n)
Read More → 946 Validate Stack Sequences948 Bag of Tokens
Link: Bag of Tokens Thought For this question, I find we could do 2 things: spend any cost form the tokens, and get 1 point. Or, spend 1 point, get any power from the tokens. So it turns to be pretty clear that I will firstly sort all tokens by value, and trying to get […]
Read More → 948 Bag of Tokens945 Minimum Increment to Make Array Unique
Link Minimum Increment to Make Array Unique Thought Firstly, I try to enumerate all existing number, and I got TLE :(. Then I realized I could sort the origin array and maintain a upper boundary, for any number, if it smaller than upper boundary, means I can only move it to upper boundary + 1 […]
Read More → 945 Minimum Increment to Make Array Unique939 Minimum Area Rectangle
link: Minimum Area Rectangle Thought I plan to use sweep line to solve. Firstly I put a dictionary with x-val as key, all y-val with coordinate x-val as value. Then, once I check x1, x2, if they has intersection, means there could be a rectangle. So I could enumerate all possible rectangles and record the […]
Read More → 939 Minimum Area Rectangle938 Range Sum of BST
link: Range Sum of BST Thought When first saw this question, I think using tree traverse could solve it and it did works. Code Runtime Time O(n) Space O(n)
Read More → 938 Range Sum of BST940 Distinct Subsequences II
link Distinct Subsequences II Thought The very first thought here is use DFS to find all distinct subsequences. However it will time out. So my next idea is using dynamic programming to solve it. I’ll define a DP array, where DP[i + 1] stands for how many distinct subsequences if I use 0,1,…i characters to […]
Read More → 940 Distinct Subsequences II933 Number of Recent Calls
link: Number of Recent Calls Thought Question ask for numbers of calls in 3000. So sliding windows is my choice here – it will move and drop calls earlier than 3000 – which I don’t care, and keep tracing incoming calls. Code Runtime Time: O(n)Space: O(n)
Read More → 933 Number of Recent Calls935 Knight Dialer
link Knight Dialer Thought Since it ask how many distinct numbers could be generated, DP jumps to my mind immediately. Firstly, the relation is, if knight stop at number 6, it has to come from 1, 7, or 0. So I keep a DP array, DP[j][i] stands for how many unique numbers it could generate […]
Read More → 935 Knight Dialer934 Shortest Bridge
link: Shortest Bridge Thought Since the question ask for shortest, so BFS probabely the best algorithms to solve it. But notice, I will use 2 bfs funciton, one to find the edge of first island, the other one is to reach another island based on first edge. BFS1: input: start positionstop condition: queue emptygenerate & […]
Read More → 934 Shortest Bridge