楼主: youling_tong
跳转到指定楼层
上一主题 下一主题
收起左侧

2021刷题打卡

🔗
 楼主| youling_tong 2021-7-19 01:18:35 | 只看该作者
全局:
2021/07/18

HashMap tag under FB

939. Minimum Area Rectangle
  • The key is to use diagonal points to determine the other two points
  • How to build hashCode() method ? Objects.hash(...);
  • O(N^2) solution
311. Sparse Matrix Multiplication
  • Wired question. No clear problem statement regarding what is matrix multiplication.
73. Set Matrix Zeroes
  • Can be easilly resolved if no limitations on space complexity. Use a list to store the pair, so O(M+N).
  • For O(1) space complexity solution, use the first row/column as marker. One note here is the data of first row/col will be poluted so we need to record do we need to set zeroes for them before marking the rest of the data. Another trick is we need to set zeroes for row 1 onwards and col 1 onwards, then get back to first row and first col.
380. Insert Delete GetRandom O(1)
  • This question worth revisit. The key problem is how to ensure all the three operations at O(1), and the key is to use HashMap and ArrayList combined. Note to analyze how those data structures would perform on each operation.
325. Maximum Size Subarray Sum Equals k
  • prefix sum, 鬼使神差的没做出来



回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-19 01:20:43 | 只看该作者
全局:
本帖最后由 youling_tong 于 2021-7-19 01:23 编辑

Target for tomorrow: DFS
刷题大纲

✅  HashTable
⭕ Depth-First Search
⭕ Dynamic Programming
⭕ Union Find
⭕ Backtracking
⭕ Divide and Conquer
⭕ Breadth-First Search
⭕ Stack
⭕ Queue
⭕ Heap (Priority Queue)
⭕ Graph
⭕ Two Pointers
⭕ Prefix Sum
回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-21 23:51:19 | 只看该作者
全局:
两天没刷题的我来了。。。
2021/07/21
FB Depth-First Search Tag


Easy
426. Convert Binary Search Tree to Sorted Doubly Linked List
  • So unfamiliar with DFS. The easiest way to solve this problem is use the iterative solution (stack)
  • Another trick is to use a dummy node to acting as the head.
572. Subtree of Another Tree
112. Path Sum

Medium
1305. All Elements in Two Binary Search Trees
  • Merge two BST into a sorted list: using DFS/stack, otherwise, we can do sorted merge (sub-optimal solution)
1382. Balance a Binary Search Tree
230. Kth Smallest Element in a BST
  • O(H+K) iterative solution came out easily once get familiar with the DFS/stack solution
  • Follow up: what if the BST is modified frequently? B+Tree: BST + DoublyLinkedList just like LRU cache (insert/delete relies on BST (O(logN)), search using doubly linkedList)
1026. Maximum Difference Between Node and Ancestor
  • Initially only figured out O(N^2) solution by myself, compare every pair of node. The trick is to find the min/max from each root-to-leaf route to get O(N) solution.



回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-23 10:05:46 | 只看该作者
全局:
2021/07/21
FB Depth-First Search Tag

Medium

419. Battleships in a Board
  • DFS (visited[][]). The trick is to only count up-left-most nodes
1123. Lowest Common Ancestor of Deepest Leaves
  • The same as "865. Smallest Subtree with all the Deepest Nodes"
  • It's a little more complicated than normal DFS question, we need to pass multiple parameters back during DFS call.


回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-24 09:07:46 | 只看该作者
全局:
2021/07/21
FB Depth-First Search Tag

Medium

339. Nested List Weight Sum
  • pass in depth
1110. Delete Nodes And Return Forest

  • Determines the criteria to add nodes to result is 1) the node is root for a tree, 2) the node is not deleted
  • Another trick to solve this problem is when current node is deleted, then the children are all promoted to be the root nodes of trees.
695. Max Area of Island

  • Easy to solve with recursion
515. Find Largest Value in Each Tree Row
  • Use BFS instead would be super easy.


323. Number of Connected Components in an Undirected Graph


  • A good excercise for union find [youtube link], we will revisit this later when doing exercises for union find (path compression, union by rank, etc.)
529. Minesweeper

  • A little bit hard to understand the rules.
199. Binary Tree Right Side View

  • BFS
694. Number of Distinct Islands

  • How to store an island's shape? using the DFS traversal direction as the string representation. One key note here is we need to track the position we do backtracking.

863. All Nodes Distance K in Binary Tree
  • Build a graph first, then use BFS.
1644. Lowest Common Ancestor of a Binary Tree II

  • Not able to solve the problem using recursive solution without hint..

814. Binary Tree Pruning







回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-24 23:40:31 | 只看该作者
全局:
本帖最后由 youling_tong 于 2021-7-24 23:47 编辑

2021/07/21
FB Depth-First Search Tag

Medium

刷得越来越慢了。。graph好难🤯
114. Flatten Binary Tree to Linked List
  • create a helper method that would return the "tail" or the rightmost node from the subtrees. The key logic is to link the left-tree's tail with the right tree's head. O(N), O(N)
  • To reach O(1) space complexity, we can use some trick to link the rightmost node of leftree with the right tree's root with iterative approach.
341. Flatten Nested List Iterator
  • Stack:add/pop, Queue: offer, poll
  • How to leverage Stack to solve this problem
449. Serialize and Deserialize BST
  • This is a follow-up question to 297. Serialize and Deserialize Binary Tree, some more reading here.
  • One key learning here is serialize and deserialize recursively in the same order (preorder for example), then we'll get the result.
  • Another trick here is to eliminate the null nodes from the encoded string, we should leverage BST's attribtue that left child < root < right child.
721. Accounts Merge
  • The tricky part is how to build the graph. This problem can be solved by either DFS / bfs.
  • Can exercise latter with union-find.

Micellaneous (Contest)

回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-25 23:47:17 | 只看该作者
全局:
2021/07/25
Weekly Contest 251
Solved:
1945. Sum of Digits of String After Convert
1946. Largest Number After Mutating Substring
  • Not fully understand the question initially, thought we can mutate any character.
  • Lesson leant: need to clarify the question first before start solving it.
Unsolved:
1947. Maximum Compatibility Score Sum
  • Backtracking + DFS

Monotonic Stack

1944. Number of Visible People in a Queue

Graph, DFS:
827. Making A Large Island
  • Two steps: 1) coloring the island, at the same time count the area for this island; 2) starting with cell value is zero, connect the island in four directions.
  • 2 edge cases to consider: if the entire map is one island, if there is no island at all.
124. Binary Tree Maximum Path Sum
  • Need to consider node.val can be negative.
314. Binary Tree Vertical Order Traversal
  • No bug :)
987. Vertical Order Traversal of a Binary Tree
  • Similar to the question above.
269. Alien Dictionary
  • Topological sorting, graph, BFS
211. Design Add and Search Words Data Structure
  • Trie + DFS
536. Construct Binary Tree from String
  • Hard to follow... need to revisit later.


回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-26 00:04:59 | 只看该作者
全局:
✅ HashTable
✅ Depth-First Search
⏹ Dynamic Programming
⭕ Union Find
⭕ Backtracking
⭕ Divide and Conquer
⭕ Breadth-First Search
⭕ Stack
⭕ Queue
⭕ Heap (Priority Queue)
⭕ Graph
⭕ Two Pointers
⭕ Prefix Sum

Target for July 26: DP
回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-26 21:17:17 | 只看该作者
全局:
本帖最后由 youling_tong 于 2021-7-26 21:21 编辑

2021/07/26

FB - Dynamic Programming - Sort By Frequency

509. Fibonacci Number
53. Maximum Subarray
  • This is my 3rd time trying to solve this problem. However, I am still not able to solve it with minimal efforts and bug free.
  • Good reading for this article about Kadane's algorithm
1884. Egg Drop With 2 Eggs and N Floors
  • A famous DP problem that is hard to implement, worth revisit and read this article again.
139. Word Break
  • Trie + DP





回复

使用道具 举报

🔗
 楼主| youling_tong 2021-7-27 22:55:30 | 只看该作者
全局:
2021/07/27

勤奋工作的一天,只刷了一道explore题
16. 3Sum Closest
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表