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

[Leetcode] LC Python 刷题笔记 有志者事竟成

   
🔗
 楼主| 李浩泉 2020-10-22 10:58:24 | 只看该作者
全局:
766. Toeplitz Matrix - 字典法的经典好题 脸熟高频题

  1. '''
  2. A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.

  3. Now given an M x N matrix, return True if and only if the matrix is Toeplitz.


  4. Example 1:

  5. Input:
  6. matrix = [
  7.   [1,2,3,4],
  8.   [5,1,2,3],
  9.   [9,5,1,2]
  10. ]
  11. Output: True
  12. Explanation:
  13. In the above grid, the diagonals are:
  14. "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
  15. In each diagonal all elements are the same, so the answer is True.
  16. Example 2:

  17. Input:
  18. matrix = [
  19.   [1,2],
  20.   [2,2]
  21. ]
  22. Output: False
  23. Explanation:
  24. The diagonal "[1, 2]" has different elements.

  25. Note:

  26. matrix will be a 2D array of integers.
  27. matrix will have a number of rows and columns in range [1, 20].
  28. matrix[i][j] will be integers in range [0, 99].

  29. Follow up:

  30. What if the matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?
  31. What if the matrix is so large that you can only load up a partial row into the memory at once?
  32. '''

  33. def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
  34.         dic = {}
  35.         for i,c in enumerate(matrix):
  36.             for row_i,row_c in enumerate(c):
  37.                 if i - row_i not in dic:
  38.                     dic[i-row_i] = row_c
  39.                 elif dic[i-row_i] != row_c:
  40.                     return False
  41.         return True
复制代码


[/i]
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-23 01:56:27 | 只看该作者
全局:

832. Flipping an Image

微软的最爱,print(2 ^ 1) = 3,print(3 ^ 1) = 2,O(∩_∩)O~

  1. '''
  2. Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.

  3. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].

  4. To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].

  5. Example 1:

  6. Input: [[1,1,0],[1,0,1],[0,0,0]]
  7. Output: [[1,0,0],[0,1,0],[1,1,1]]
  8. Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
  9. Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
  10. Example 2:

  11. Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
  12. Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
  13. Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
  14. Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
  15. '''

  16. def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
  17.         return [[n ^ 1 for n in c[::-1]] for c in A]
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-23 02:29:50 | 只看该作者
全局:

1413. Minimum Value to Get Positive Step by Step Sum

  1. '''
  2. Given an array of integers nums, you start with an initial positive value startValue.

  3. In each iteration, you calculate the step by step sum of startValue plus elements in nums (from left to right).

  4. Return the minimum positive value of startValue such that the step by step sum is never less than 1.

  5. Example 1:

  6. Input: nums = [-3,2,-3,4,2]
  7. Output: 5
  8. Explanation: If you choose startValue = 4, in the third iteration your step by step sum is less than 1.
  9.                 step by step sum
  10.                 startValue = 4 | startValue = 5 | nums
  11.                   (4 -3 ) = 1  | (5 -3 ) = 2    |  -3
  12.                   (1 +2 ) = 3  | (2 +2 ) = 4    |   2
  13.                   (3 -3 ) = 0  | (4 -3 ) = 1    |  -3
  14.                   (0 +4 ) = 4  | (1 +4 ) = 5    |   4
  15.                   (4 +2 ) = 6  | (5 +2 ) = 7    |   2
  16. Example 2:

  17. Input: nums = [1,2]
  18. Output: 1
  19. Explanation: Minimum start value should be positive.
  20. Example 3:

  21. Input: nums = [1,-2,-3]
  22. Output: 5
  23. '''

  24. return min(sum(nums[0:i]) for i in range(len(nums)+1))*-1 + 1
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-23 02:59:27 | 只看该作者
全局:

1022. Sum of Root To Leaf Binary Numbers 【遍历二叉树,branch by branch】

  1. '''
  2. You are given the root of a binary tree where each node has a value 0 or 1.  Each root-to-leaf path represents a binary number starting with the most significant bit.  For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.

  3. For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.

  4. Return the sum of these numbers. The answer is guaranteed to fit in a 32-bits integer.

  5. Example 1:

  6. Input: root = [1,0,1,0,1,0,1]
  7. Output: 22
  8. Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
  9. Example 2:

  10. Input: root = [0]
  11. Output: 0
  12. Example 3:

  13. Input: root = [1]
  14. Output: 1
  15. Example 4:

  16. Input: root = [1,1]
  17. Output: 3
  18. '''

  19. def sumRootToLeaf(self, root: TreeNode) -> int:
  20.         root_to_leaf = 0
  21.         stack = [(root, 0) ]
  22.         
  23.         while stack:
  24.             root, curr_number = stack.pop()
  25.             if root is not None:
  26.                 curr_number = (curr_number << 1) | root.val
  27.                 if root.left is None and root.right is None:
  28.                     root_to_leaf += curr_number
  29.                 else:
  30.                     stack.append((root.right, curr_number))
  31.                     stack.append((root.left, curr_number))
  32.                         
  33.         return root_to_leaf
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-23 07:06:09 | 只看该作者
全局:
276. Paint Fence

  1. '''
  2. There is a fence with n posts, each post can be painted with one of the k colors.

  3. You have to paint all the posts such that no more than two adjacent fence posts have the same color.

  4. Return the total number of ways you can paint the fence.

  5. Note:
  6. n and k are non-negative integers.

  7. Example:

  8. Input: n = 3, k = 2
  9. Output: 6
  10. Explanation: Take c1 as color 1, c2 as color 2. All possible ways are:

  11.             post1  post2  post3      
  12. -----      -----  -----  -----      
  13.    1         c1     c1     c2
  14.    2         c1     c2     c1
  15.    3         c1     c2     c2
  16.    4         c2     c1     c1  
  17.    5         c2     c1     c2
  18.    6         c2     c2     c1
  19. '''

  20. def numWays(self, n: int, k: int) -> int:
  21.         if n == 0: return 0
  22.         if n == 1: return k
  23.         if n == 2: return k*k
  24.         first = k
  25.         second = k*k
  26.         for i in range(3,n+1):
  27.             third = first*(k-1) + second*(k-1)
  28.             first, second = second, third      
  29.         return third
复制代码


回复

使用道具 举报

🔗
xiongml81 2020-10-23 09:01:22 | 只看该作者
全局:
泡妞可以去一下的
回复

使用道具 举报

🔗
leaffly119 2020-10-23 13:24:57 | 只看该作者
全局:
楼主真的是厉害了!效率好高!真心求问有学习那些数据结构的材料推荐吗?我是水ds一个,想要冲击mle,但是感觉硬刷好难啊,尤其是二叉树啊,dp啊那些
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-23 13:45:31 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-10-23 13:54 编辑
leaffly119 发表于 2020-10-23 13:24
楼主真的是厉害了!效率好高!真心求问有学习那些数据结构的材料推荐吗?我是水ds一个,想要冲击mle,但是 ...

我也有刷ML的题,今天做了好几道Maximum Likelihood Estimate (MLE) 和 Maximum A Posteriori (MAP) 的题。为了一份破DE的工作,我已经刷了:

Problem Solving by PYTHON
T-SQL/PL-SQL/NoSQL/Spark SQL
ETL/ELT/Data Pipeline
Data Modular/Schema/
BI Reporting
AI/ML
DS/Statistics
Snowflake Operation
Redshift Operation
Azure Operation
Tableau/Power BI/Qlikview/Looker
Azure Spark
Big Query
JSON
LEETCODE刷了一半,codesignal刷完了,hackerrank也刷完了,checkio刷了一半。

我今天把LINKEDIN上OPEN TO WORK关了,今年是没有跳槽的可能了,欲速则不达,慢慢来吧。
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-23 13:56:17 | 只看该作者
全局:

455. Assign Cookies 这道题很好!

  1. '''
  2. Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.

  3. Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.

  4. Example 1:

  5. Input: g = [1,2,3], s = [1,1]
  6. Output: 1
  7. Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
  8. And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
  9. You need to output 1.
  10. Example 2:

  11. Input: g = [1,2], s = [1,2,3]
  12. Output: 2
  13. Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
  14. You have 3 cookies and their sizes are big enough to gratify all of the children,
  15. You need to output 2.

  16. Constraints:

  17. 1 <= g.length <= 3 * 104
  18. 0 <= s.length <= 3 * 104
  19. 1 <= g[i], s[j] <= 231 - 1
  20. '''

  21. def findContentChildren(self, g: List[int], s: List[int]) -> int:
  22.         g = sorted(g)
  23.         g = g[::-1]
  24.         s = sorted(s)
  25.         start = 0
  26.         end = len(s)-1
  27.         n = len(g)
  28.         ans = 0
  29.         while start<n and end>=0:
  30.             if g[start]<=s[end]:
  31.                 start+=1
  32.                 end-=1
  33.                 ans+=1
  34.             else:
  35.                 start+=1
  36.         return ans
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-23 23:25:41 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-10-23 23:32 编辑

1103. Distribute Candies to People

给大人分糖果,两个循环,提前构筑结果数列
  1. '''
  2. We distribute some number of candies, to a row of n = num_people people in the following way:

  3. We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n candies to the last person.

  4. Then, we go back to the start of the row, giving n + 1 candies to the first person, n + 2 candies to the second person, and so on until we give 2 * n candies to the last person.

  5. This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies.  The last person will receive all of our remaining candies (not necessarily one more than the previous gift).

  6. Return an array (of length num_people and sum candies) that represents the final distribution of candies.



  7. Example 1:

  8. Input: candies = 7, num_people = 4
  9. Output: [1,2,3,1]
  10. Explanation:
  11. On the first turn, ans[0] += 1, and the array is [1,0,0,0].
  12. On the second turn, ans[1] += 2, and the array is [1,2,0,0].
  13. On the third turn, ans[2] += 3, and the array is [1,2,3,0].
  14. On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].
  15. Example 2:

  16. Input: candies = 10, num_people = 3
  17. Output: [5,2,3]
  18. Explanation:
  19. On the first turn, ans[0] += 1, and the array is [1,0,0].
  20. On the second turn, ans[1] += 2, and the array is [1,2,0].
  21. On the third turn, ans[2] += 3, and the array is [1,2,3].
  22. On the fourth turn, ans[0] += 4, and the final array is [5,2,3].
  23. '''

  24. def distributeCandies(self, candies: int, num_people: int) -> List[int]:
  25.         result = [0] * num_people
  26.         r = 0
  27.         while candies > 0:
  28.             for i in range(num_people):
  29.                 portion = r * num_people + (i+1)
  30.                 if portion <= candies:
  31.                     result[i] += portion
  32.                     candies -= portion
  33.                 else:
  34.                     result[i] += candies
  35.                     return result
  36.             r += 1
  37.         return result
复制代码



回复

使用道具 举报

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

本版积分规则

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