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

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

   
🔗
 楼主| 李浩泉 2020-10-18 00:51:30 | 只看该作者
全局:
350. Intersection of Two Arrays II

  1. '''
  2. Given two arrays, write a function to compute their intersection.

  3. Example 1:

  4. Input: nums1 = [1,2,2,1], nums2 = [2,2]
  5. Output: [2,2]
  6. Example 2:

  7. Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
  8. Output: [4,9]
  9. '''

  10. def intersect(self, nums1, nums2):
  11.     return list((Counter(nums1) & Counter(nums2)).elements())
复制代码

回复

使用道具 举报

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

使用道具 举报

🔗
leafpop 2020-10-18 23:32:09 | 只看该作者
全局:
楼主思路不错
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-19 00:56:20 | 只看该作者
全局:
1624. Largest Substring Between Two Equal Characters

  1. '''
  2. Given a string s, return the length of the longest substring between two equal characters, excluding the two characters. If there is no such substring return -1.

  3. A substring is a contiguous sequence of characters within a string.

  4. Example 1:

  5. Input: s = "aa"
  6. Output: 0
  7. Explanation: The optimal substring here is an empty substring between the two 'a's.
  8. Example 2:

  9. Input: s = "abca"
  10. Output: 2
  11. Explanation: The optimal substring here is "bc".
  12. Example 3:

  13. Input: s = "cbzxy"
  14. Output: -1
  15. Explanation: There are no characters that appear twice in s.
  16. Example 4:

  17. Input: s = "cabbac"
  18. Output: 4
  19. Explanation: The optimal substring here is "abba". Other non-optimal substrings include "bb" and "".
  20. '''

  21. 字典法:

  22. class Solution(object):
  23.     def maxLengthBetweenEqualCharacters(self, s):
  24.         dic = {}
  25.         result = -1
  26.         for i, c in enumerate(s):
  27.             if c in dic:
  28.                 result = max(result, i - dic[c] - 1)
  29.             else:
  30.                 dic[c] = i
  31.         return result

  32. 构建一个列表实现字典法的功能,性质是一样的
  33.    
  34. class Solution:
  35.     def maxLengthBetweenEqualCharacters(self, s: str) -> int:
  36.         d = [-1] * 26
  37.         ans = -1
  38.         for i, c in enumerate(s):
  39.             o = ord(c) - ord('a')
  40.             if d[o] != -1:
  41.                 ans = max(ans, i - d[o] - 1)
  42.             if d[o] == -1:
  43.                 d[o] = i
  44.         return ans
复制代码


回复

使用道具 举报

🔗
jason9263 2020-10-19 06:45:54 | 只看该作者
全局:
Followup + LC python 党.
回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-21 01:13:19 | 只看该作者
全局:
1566. Detect Pattern of Length M Repeated K or More Times


  1. '''
  2. Given an array of positive integers arr,  find a pattern of length m that is repeated k or more times.

  3. A pattern is a subarray (consecutive sub-sequence) that consists of one or more values, repeated multiple times consecutively without overlapping. A pattern is defined by its length and the number of repetitions.

  4. Return true if there exists a pattern of length m that is repeated k or more times, otherwise return false.



  5. Example 1:

  6. Input: arr = [1,2,4,4,4,4], m = 1, k = 3
  7. Output: true
  8. Explanation: The pattern (4) of length 1 is repeated 4 consecutive times. Notice that pattern can be repeated k or more times but not less.
  9. Example 2:

  10. Input: arr = [1,2,1,2,1,1,1,3], m = 2, k = 2
  11. Output: true
  12. Explanation: The pattern (1,2) of length 2 is repeated 2 consecutive times. Another valid pattern (2,1) is also repeated 2 times.
  13. Example 3:

  14. Input: arr = [1,2,1,2,1,3], m = 2, k = 3
  15. Output: false
  16. Explanation: The pattern (1,2) is of length 2 but is repeated only 2 times. There is no pattern of length 2 that is repeated 3 or more times.
  17. Example 4:

  18. Input: arr = [1,2,3,1,2], m = 2, k = 2
  19. Output: false
  20. Explanation: Notice that the pattern (1,2) exists twice but not consecutively, so it doesn't count.
  21. Example 5:

  22. Input: arr = [2,2,2,2], m = 2, k = 3
  23. Output: false
  24. Explanation: The only pattern of length 2 is (2,2) however it's repeated only twice. Notice that we do not count overlapping repetitions.
  25. '''

  26. def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
  27.         for i in range(len(arr)-m):
  28.             if arr[i:i+m]*k == arr[i:i+m*k]:
  29.                 return True
  30.         return False
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-21 01:30:55 | 只看该作者
全局:

690. Employee Importance

  1. '''
  2. You are given a data structure of employee information, which includes the employee's unique id, their importance value and their direct subordinates' id.

  3. For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.

  4. Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all their subordinates.

  5. Example 1:

  6. Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
  7. Output: 11
  8. Explanation:
  9. Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
  10. '''

  11. def getImportance(self, employees: List['Employee'], id: int) -> int:
  12.         d = {e.id: e for e in employees}        
  13.         target = d[id]        
  14.         dq = []
  15.         dq.append(id)
  16.         res = 0
  17.         while dq:
  18.             manager = dq.pop()
  19.             res += d[manager].importance
  20.             if d[manager].subordinates:
  21.                 dq = dq+d[manager].subordinates
  22.         return res
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-21 01:32:11 | 只看该作者
全局:

690. Employee Importance

  1. '''
  2. You are given a data structure of employee information, which includes the employee's unique id, their importance value and their direct subordinates' id.

  3. For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.

  4. Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all their subordinates.

  5. Example 1:

  6. Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
  7. Output: 11
  8. Explanation:
  9. Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
  10. '''

  11. def getImportance(self, employees: List['Employee'], id: int) -> int:
  12.         d = {e.id: e for e in employees}        
  13.         target = d[id]        
  14.         dq = []
  15.         dq.append(id)
  16.         res = 0
  17.         while dq:
  18.             manager = dq.pop()
  19.             res += d[manager].importance
  20.             if d[manager].subordinates:
  21.                 dq = dq+d[manager].subordinates
  22.         return res
复制代码


回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-21 01:32:49 | 只看该作者
全局:
  1. '''
  2. You are given a data structure of employee information, which includes the employee's unique id, their importance value and their direct subordinates' id.

  3. For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.

  4. Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all their subordinates.

  5. Example 1:

  6. Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
  7. Output: 11
  8. Explanation:
  9. Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
  10. '''

  11. def getImportance(self, employees: List['Employee'], id: int) -> int:
  12.         d = {e.id: e for e in employees}        
  13.         target = d[id]        
  14.         dq = []
  15.         dq.append(id)
  16.         res = 0
  17.         while dq:
  18.             manager = dq.pop()
  19.             res += d[manager].importance
  20.             if d[manager].subordinates:
  21.                 dq = dq+d[manager].subordinates
  22.         return res
复制代码

回复

使用道具 举报

🔗
 楼主| 李浩泉 2020-10-21 01:34:30 | 只看该作者
全局:
690. Employee Importance

这道题很好,用PYTHON实现SQL里面的JOIN,非常有意思。以前刷题是为了面试,现在刷题just for fun。
回复

使用道具 举报

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

本版积分规则

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