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

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

   
🔗
 楼主| 李浩泉 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) 这些都有啥题啊?
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-26 00:06:12 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-10-26 00:09 编辑
sevenwonder 发表于 2020-10-25 07:30
Maximum Likelihood Estimate (MLE) 和 Maximum A Posteriori (MAP) 这些都有啥题啊?

一个DE岗位,被问到了Machine Learning问题。让你给出它们的应用场景。
When do you use Maximum Likelihood Estimate, and what are the differences between Maximum Likelihood Estimate (MLE) and Maximum A Posteriori (MAP)。

回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-26 07:08:22 | 只看该作者
全局:
本帖最后由 李浩泉 于 2020-10-26 07:55 编辑

关于ML的问题和知识点:

k邻近算法( k-nearest neighbors ),是一种基本分类和回归方法。给定一个训练数据集,对新的输入实例,在训练数据集中找到与该实例最邻近的K个实例,这K个实例的多数属于某个类,就把该输入实例分类到这个类中。(这就类似于现实生活中少数服从多数的思想)。有点类似美国选举制度,赢者通吃,多数代表少数。

If DS request to improve the accuracy of its K-nearest neighbor result. As the DE what should you do?

Stacking by running on top of a naive Bayes result.

减小方差(bagging)
减小偏差(boosting)
改进预测(stacking)

Principal Component Analysis is a statistical technique that takes the axes of greatest variance of the data and essentially creates new target features.







回复

使用道具 举报

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

本版积分规则

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