Tag: leetcode
951 Flip Equivalent Binary Trees
Link: https://leetcode.com/problems/flip-equivalent-binary-trees/ Thought If 2 nodes are equivalent, either their left and right side match, or they are flipped match. So using a recursion(why recursion? It is a Tree!), and try to match each node pairs from root1 and root2. For each recursion level, I will first compare if current node pair have the same value. […]
Read More → 951 Flip Equivalent Binary Trees950 Reveal Cards In Increasing Order
link: https://leetcode.com/problems/reveal-cards-in-increasing-order/ Thought So the rule is if you pop anything from front, then you shall move next head to tail. Reversely, if we want to build the original sequence, we could assume if you want to insert anything in head, you should pop your tail, and push the tail to head Code Runtime Time: O(n)Space: […]
Read More → 950 Reveal Cards In Increasing Order949 Largest Time for Given Digits
link: https://leetcode.com/problems/largest-time-for-given-digits/ Thought Its easy to think use DFS to solve it: it has limit data(4 digits), and DFS will enumerates all possible combinations Code Runtime Time: O(n!)Space: O(n!)
Read More → 949 Largest Time for Given Digits947 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 II