Author: andy
948 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 Bridge931 Minimum Falling Path Sum
Link 931.Minimum Falling Path Sum Thought DFS When first seeing this question, DFS prompt into my mind directly. Take Any node from first level as start, and search down stream. Until reach node in last level. For any path, it will have a value. Find the minimum value and it will be the answer However, for […]
Read More → 931 Minimum Falling Path Sum929. Unique Email Addresses
Link: Unique Email Addresses Thought Brutal Force SolutionThis question is pretty simple (and it actually is an esay one). A naive approache is, split the name and domain. For the name, remove every thing after first ‘+’, and then replace all ‘.’ as empty. For the domain, just keep it is. Then, use a set […]
Read More → 929. Unique Email Addresses