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

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

   
🔗
 楼主| 李浩泉 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
复制代码



回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-24 01:34:20 | 只看该作者
全局:
1128. Number of Equivalent Domino Pairs - 字典法的终结篇,亚麻神题

下面两个方法,由于使用了defaultdict(),所以就不需要额外对字典中没有的KEY赋初始值了,else: dic[tuple(sorted(c))] = 1。另外列表不能直接在字典里当KEY,所以用tulpe()封装一下。

  1. '''
  2. Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a==c and b==d), or (a==d and b==c) - that is, one domino can be rotated to be equal to another domino.

  3. Return the number of pairs (i, j) for which 0 <= i < j < dominoes.length, and dominoes[i] is equivalent to dominoes[j].

  4. Example 1:

  5. Input: dominoes = [[1,2],[2,1],[3,4],[5,6]]
  6. Output: 1
  7. '''

  8. def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int:
  9.         dic = {}
  10.         count = 0   
  11.         for c in dominoes:
  12.             if tuple(sorted(c)) in dic:
  13.                 count += dic[tuple(sorted(c))]
  14.                 dic[tuple(sorted(c))] += 1
  15.             else:
  16.                 dic[tuple(sorted(c))] = 1
  17.         return count
  18.    
  19. def numEquivDominoPairs(self, dominoes):
  20.         dic = defaultdict(int)
  21.         count = 0   
  22.         for c in dominoes:
  23.             if tuple(sorted(c)) in dic:
  24.                 count += dic[tuple(sorted(c))]
  25.             dic[tuple(sorted(c))] += 1
  26.         return count
复制代码


[/i][/i]
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-24 02:54:28 | 只看该作者
全局:

1237. Find Positive Integer Solution for a Given Equation - 双循环

  1. '''
  2. Given a function  f(x, y) and a value z, return all positive integer pairs x and y where f(x,y) == z.

  3. The function is constantly increasing, i.e.:

  4. f(x, y) < f(x + 1, y)
  5. f(x, y) < f(x, y + 1)
  6. The function interface is defined like this:

  7. interface CustomFunction {
  8. public:
  9.   // Returns positive integer f(x, y) for any given positive integer x and y.
  10.   int f(int x, int y);
  11. };
  12. For custom testing purposes you're given an integer function_id and a target z as input, where function_id represent one function from an secret internal list, on the examples you'll know only two functions from the list.  

  13. You may return the solutions in any order.



  14. Example 1:

  15. Input: function_id = 1, z = 5
  16. Output: [[1,4],[2,3],[3,2],[4,1]]
  17. Explanation: function_id = 1 means that f(x, y) = x + y
  18. Example 2:

  19. Input: function_id = 2, z = 5
  20. Output: [[1,5],[5,1]]
  21. Explanation: function_id = 2 means that f(x, y) = x * y


  22. Constraints:

  23. 1 <= function_id <= 9
  24. 1 <= z <= 100
  25. It's guaranteed that the solutions of f(x, y) == z will be on the range 1 <= x, y <= 1000
  26. It's also guaranteed that f(x, y) will fit in 32 bit signed integer if 1 <= x, y <= 1000
  27. '''

  28. def findSolution(customfunction, z):
  29.         result = []
  30.         x = y = 1
  31.         while customfunction.f(x,y) < z:
  32.             while customfunction.f(x,y) < z:
  33.                 y += 1
  34.             if customfunction.f(x,y) == z:
  35.                 result.append([x,y])
  36.             x += 1
  37.             y = 1
  38.         if customfunction.f(x,y)==z:
  39.             result.append([x,y])
  40.         return result
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-24 04:07:20 | 只看该作者
本帖为密码帖 ,请输入密码 
回复

使用道具 举报

🔗
sz1111 2020-10-24 09:59:10 | 只看该作者
全局:
楼主非常励志!点赞
回复

使用道具 举报

🔗
Anonyknight 2020-10-24 13:02:07 | 只看该作者
全局:
楼主很励志,建议建立一个github 保留自己的代码记录,更方便.

评分

参与人数 1大米 +2 收起 理由
李浩泉 + 2 非常好的建议!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-25 00:23:41 | 只看该作者
全局:
Anonyknight 发表于 2020-10-24 13:02
楼主很励志,建议建立一个github 保留自己的代码记录,更方便.

已经搞定开始上传和全世界的刷子一起共享了,你的建议让三分地每天损失了10多个流量,哈哈哈哈哈。
回复

使用道具 举报

🔗
leaffly119 2020-10-25 03:11:29 | 只看该作者
全局:
李浩泉 发表于 2020-10-23 13:45
我也有刷ML的题,今天做了好几道Maximum Likelihood Estimate (MLE) 和 Maximum A Posteriori (MAP) 的题 ...

可以预见楼主将来成为面霸!如果建了github请分享呀
回复

使用道具 举报

🔗
sevenwonder 2020-10-25 07:30:02 | 只看该作者
全局:
李浩泉 发表于 2020-10-23 13:45
我也有刷ML的题,今天做了好几道Maximum Likelihood Estimate (MLE) 和 Maximum A Posteriori (MAP) 的题 ...

Maximum Likelihood Estimate (MLE) 和 Maximum A Posteriori (MAP) 这些都有啥题啊?
回复

使用道具 举报

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

本版积分规则

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