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

刷题记录帖子

🔗
 楼主| Myron2017 2021-4-21 05:30:25 | 只看该作者
全局:
429        N-ary Tree Level Order Traversal        Medium

还是基础的 level order traversal
因为 BFS 所以需要 queue 来记录。
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-21 12:13:00 | 只看该作者
全局:
101        Symmetric Tree        Easy       

Always processing two nodes each round



  1. """

  2. Recursive

  3. """

  4. # Definition for a binary tree node.
  5. # class TreeNode:
  6. #     def __init__(self, val=0, left=None, right=None):
  7. #         self.val = val
  8. #         self.left = left
  9. #         self.right = right
  10. class Solution:
  11.     def isSymmetric(self, root: TreeNode) -> bool:
  12.         
  13.         def checkMirror(node1, node2):
  14.             # both None
  15.             if not node1 and not node2: return True
  16.             # One Not None, One None
  17.             if not node1 or not node2 : return False
  18.             # both not None
  19.             return node1.val == node2.val and checkMirror(node1.right, node2.left) and checkMirror(node1.left, node2.right)
  20.         
  21.         return checkMirror(root, root)
  22.         

  23. """

  24. Iterative

  25. """

  26. # Definition for a binary tree node.
  27. # class TreeNode:
  28. #     def __init__(self, val=0, left=None, right=None):
  29. #         self.val = val
  30. #         self.left = left
  31. #         self.right = right
  32. class Solution:
  33.     def isSymmetric(self, root: TreeNode) -> bool:
  34.         queue = deque([root, root])
  35.         
  36.         while queue:
  37.             node1 = queue.popleft()
  38.             node2 = queue.popleft()
  39.             
  40.             # Both None
  41.             if not node1 and not node2: continue
  42.             
  43.             # One None, One not None
  44.             if not node1 or not node2: return False
  45.             
  46.             # Both not None
  47.             if node1.val != node2.val: return False
  48.             # Mirror
  49.             queue.append(node1.left)
  50.             queue.append(node2.right)
  51.             
  52.             queue.append(node1.right)
  53.             queue.append(node2.left)
  54.             
  55.         return True
  56.         
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-22 11:58:19 | 只看该作者
全局:
120. Triangle

Bottom Up Classic

  1. class Solution:
  2.     def minimumTotal(self, triangle: List[List[int]]) -> int:
  3.         n = len(triangle)
  4.         if n == 1: return min(triangle[0])
  5.         
  6.         row_curr = triangle[n-1]
  7.         
  8.         for row in range(n-2, -1, -1):
  9.             row_up = triangle[row]
  10.             for ind in range(len(row_up)):
  11.                 row_up[ind] = min(row_curr[ind] +  triangle[row][ind],
  12.                                   row_curr[ind+1] + triangle[row][ind])
  13.             row_curr = row_up
  14.         return row_up[0]
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-23 07:10:12 | 只看该作者
全局:
554        Brick Wall

真题目还是很经典的,统计 freq of wall index。

  1. class Solution:
  2.     def leastBricks(self, wall: List[List[int]]) -> int:
  3.         freq = defaultdict(int)
  4.         n = len(wall)
  5.         ending = sum(wall[0])
  6.         
  7.         for row in wall:
  8.             c0 = 0
  9.             for c in row:
  10.                 ind = c0 + c
  11.                 if ind != ending: freq[ind] += 1
  12.                 c0 = ind

  13.         if not freq:
  14.             return n
  15.         else:
  16.             return n - max(freq.values())
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-28 04:12:01 | 只看该作者
全局:
326. Power of Three

return n > 0 and 1162261467 % n == 0
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-4-30 23:56:09 | 只看该作者
全局:
34        Find First and Last Position of Element in Sorted Array        Medium        5        bisect_right - 1 / bisect_left
970        Powerful Integers         Medium               
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-5-4 09:27:48 | 只看该作者
全局:
1480        Running Sum of 1d Array        Easy
回复

使用道具 举报

🔗
 楼主| Myron2017 2021-5-14 10:22:44 | 只看该作者
全局:
本帖最后由 Myron2017 于 2021-5-14 10:25 编辑

816        Ambiguous Coordinates        Medium        5        边界条件        for i in range(1, len(x)): if (x[:i] == "0" or x[0] != "0") and x[-1] != "0":



回复

使用道具 举报

🔗
 楼主| Myron2017 2021-5-15 13:10:08 | 只看该作者
全局:
LeetCode 114. Flatten Binary Tree to Linked List (Python)

好经典的题目,真是太漂亮了。如何解决移动的问题,在移动的同时保持 preorder 的结构。

参考这个画图解释, https://leetcode.com/problems/fl ... tremely-Intuitive-O(1)-Space-solution-with-Simple-explanation-Python


回复

使用道具 举报

🔗
 楼主| Myron2017 2021-5-19 05:03:25 | 只看该作者
全局:
609. Find Duplicate File in System

感觉这道题目的 follow up 特别有意思。

BFS vs DFS
BFS explores neighbors first. This means that files which are located close to each other are also accessed one after another. This is great for space locality and that's why BFS is expected to be faster. Also, BFS is easier to parallelize (more fine-grained locking). DFS will require a lock on the root node.

Very large files and false positives
For very large files we should do the following comparisons in this order:

compare sizes, if not equal, then files are different and stop here!
hash them with a fast algorithm e.g. MD5 or use SHA256 (no collisions found yet), if not equal then stop here!
compare byte by byte to avoid false positives due to collisions.
Have you used an IDE in remote development mode?
For example, CLion has some options on how to compare the local files with the remote server files and then decides to synchronize or not.

Complexity
Runtime - Worst case (which is very unlikely to happen): O(N^2 * L) where L is the size of the maximum bytes that need to be compared
Space - Worst case: all files are hashed and inserted in the hashmap, so O(H^2*L), H is the hash code size and L is the filename size
回复

使用道具 举报

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

本版积分规则

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