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

刷题记录帖子

🔗
 楼主| Myron2017 2020-4-6 00:50:55 | 只看该作者
全局:
122. Best Time to Buy and Sell Stock II 之前刷过,其实很简单,最重要的就是知道这个股票可以买卖多次一天内,当然要先买再卖。但是这样就只要考虑前后的价格差了。
可以说每一次价格的上涨你都可以赚到。
回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-6 02:19:41 | 只看该作者
全局:
807. Max Increase to Keep City Skyline 题目不难,就是理解下题意,然后直接简单 N方的方案就行 Greedy 解决,练习下 Python 如何在二维 list 找最小和最大值。


  1. class Solution:
  2.     def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int:
  3.         # get the skyline of each row and column
  4.         row_max = [ max(row) for row in grid]
  5.         col_max = [ max(col) for col in zip(*grid)]
  6.         
  7.         maxSum = 0
  8.         for i in range(len(grid)): # Row
  9.             for j in range(len(grid[0])): # Col
  10.                 #print(grid[i][j])
  11.                 #print(( max(row_max[i], col_max[j]) - grid[i][j] ))
  12.                 maxSum += ( min(row_max[i], col_max[j]) - grid[i][j] )
  13.         return maxSum
复制代码
[/i][/i][/i][/i][/i]
回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-6 02:28:59 | 只看该作者
全局:
1389. Create Target Array in the Given Order 太简单了,其实就是基本的 textbook 问题,如何在 array 中实现插入,用 Python 的内置函数实现。
基本的操作,需要考虑,扩容,在中间插入,在尾部插入,在头部插入等情况比较复杂。
直接 Python list insert 解决,list.insert(index, element)


  1. class Solution:
  2.     def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
  3.         res = []
  4.         # list.insert(index, element)
  5.         for num,ind in zip(nums,index):
  6.             res.insert(ind, num)
  7.         return res
复制代码

回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-7 05:20:27 | 只看该作者
全局:
49. Group Anagrams 简单题目,需要注意如何设置 dict 的key ,因为 list 不是hashable的所以不能作为 key。

# Python list is not hashable type so using tuple
# How to iterate dictory in Python

Iterating Through Keys Directly
Iterating Through .items()
Iterating Through .keys()
Iterating Through .values()


  1. class Solution:
  2.     def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
  3.         res = dict()
  4.         for item in strs:
  5.             items = [ ch for ch in item]
  6.             items_tuple = tuple(sorted(items))
  7.             if items_tuple in res.keys():
  8.                 res[items_tuple].append(item)
  9.             else:
  10.                 res[items_tuple] = [item]
  11.         ress = list()
  12.         #print(res)
  13.         for key,val in res.items():
  14.             ress.append(val)
  15.         return ress
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-7 08:53:38 | 只看该作者
全局:
1221. Split a String in Balanced Strings 这道题目其实就是括号匹配问题,只是改变了题目的叙述变得比较陌生。当然括号匹配,课堂上学的是用「栈」这是因为还有别的比如设计计算器的时候有数字等等,所以这道题目简化了。

核心是保持一个变量记录下当前是不是已经匹配了。


  1. class Solution:
  2.     def balancedStringSplit(self, s: str) -> int:
  3.         balanced = 0
  4.         res = 0
  5.         for ch in s:
  6.             if ch == 'R':
  7.                 balanced += 1
  8.             else:
  9.                 balanced -= 1
  10.             if balanced == 0:
  11.                 res += 1
  12.         return res
复制代码


回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-7 09:10:56 | 只看该作者
全局:
938. Range Sum of BST 写了一个 MidTravese 并没有用到 BST 的特色,自习研究答案的解法,其实可以发现如果改进下搜索策略可以大大加速,BST 本身就是一个好的提示。

We traverse the tree using a depth first search. If node.val falls outside the range [L, R], (for example node.val < L), then we know that only the right branch could have nodes with value inside [L, R].

https://leetcode.com/articles/range-sum-of-bst/


  1. class Solution(object):
  2.     def rangeSumBST(self, root, L, R):
  3.         def dfs(node):
  4.             if node:
  5.                 if L <= node.val <= R:
  6.                     self.ans += node.val
  7.                 if L < node.val:
  8.                     dfs(node.left)
  9.                 if node.val < R:
  10.                     dfs(node.right)

  11.         self.ans = 0
  12.         dfs(root)
  13.         return self.ans
复制代码


  1. class Solution(object):
  2.     def rangeSumBST(self, root, L, R):
  3.         ans = 0
  4.         stack = [root]
  5.         while stack:
  6.             node = stack.pop()
  7.             if node:
  8.                 if L <= node.val <= R:
  9.                     ans += node.val
  10.                 if L < node.val:
  11.                     stack.append(node.left)
  12.                 if node.val < R:
  13.                     stack.append(node.right)
  14.         return ans
复制代码



回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-7 09:36:44 | 只看该作者
全局:
1290. Convert Binary Number in a Linked List to Integer 需要熟悉 Python 的二进制操作,因为我其实用的是最简单的基本公式做了二进制和十进制的转换。


  1. class Solution(object):
  2.     def getDecimalValue(self, head):
  3.         """
  4.         :type head: ListNode
  5.         :rtype: int
  6.         """
  7.         res = 0
  8.         node = head
  9.         bString = ""
  10.         while (node != None):
  11.             bValue = node.val
  12.             bString += str(bValue)
  13.             node = node.next
  14.         
  15.         res = int(bString,2)
  16.         
  17.         return res
复制代码



  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, x):
  4. #         self.val = x
  5. #         self.next = None

  6. class Solution:
  7.     def getDecimalValue(self, head: ListNode) -> int:
  8.         num = []
  9.         while head:
  10.             num.append(head.val)
  11.             head = head.next
  12.         numLen = len(num)
  13.         res = 0
  14.         for i in range(numLen):
  15.             res += num[i] * 2 **(numLen - i - 1)
  16.         return res
复制代码
[/i]
回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-7 10:40:01 | 只看该作者
全局:
1038. Binary Search Tree to Greater Sum Tree 相当精彩的一道题目,对于树的考察特别有意思。
首先是要先去右边找子树,然后进行优化,特别是如何在递归调用的时候巧妙的引入右子树的结果。


  1. class Solution:
  2.     def bstToGst(self, root: TreeNode) -> TreeNode:
  3.         self.ans = 0
  4.         def helper(node: TreeNode) -> None:
  5.             if node is None:
  6.                 return
  7.             helper(node.right)
  8.             self.ans += node.val
  9.             node.val = self.ans
  10.             helper(node.left)
  11.         helper(root)
  12.         return root
复制代码


第二种解法,迭代写法,



  1. class Solution:
  2.     def bstToGst(self, root: TreeNode) -> TreeNode:
  3.         if root is None:
  4.             return
  5.         # If we use iterative methods
  6.         ans = 0
  7.         stack = [root]
  8.         node = root.right
  9.         while node or stack:
  10.             while node:
  11.                 stack.append(node)
  12.                 node = node.right
  13.             node = stack.pop()
  14.             ans += node.val
  15.             node.val = ans
  16.             node = node.left
  17.         return root
复制代码

回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-7 10:56:09 | 只看该作者
全局:
1266. Minimum Time Visiting All Points 很简单的题目,只需要把题意理解即可,其实就是平面移动的题目,理解题意即可。


  1. class Solution:
  2.     def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
  3.         if len(points) == 0 or len(points) == 1:
  4.             return 0

  5.         # line is the shortest
  6.         res = 0
  7.         i = 0
  8.         while i < len(points) - 1:
  9.             xMoves = abs(points[i][0] - points[i+1][0])
  10.             yMoves = abs(points[i][1] - points[i+1][1])
  11.             res += max(xMoves, yMoves)
  12.             i += 1
  13.         return res[/i][/i]

  14. [i][i]
复制代码
[/i][/i]
回复

使用道具 举报

🔗
 楼主| Myron2017 2020-4-8 01:10:20 | 只看该作者
全局:
本帖最后由 Myron2017 于 2020-4-8 01:11 编辑

应该是新出的题目,在 30 天竞赛中的 day 7。
这道题目需要理解下题意,说的不太明确其实是说,key 和 key+1 比较如果有重复,其实 是按照 key 的数目统计。
我在这儿理解了半天才明白啥意思。



  1. class Solution:
  2.     def countElements(self, arr: List[int]) -> int:
  3.         freq_arr = dict()
  4.         for num in arr:
  5.             if num in freq_arr.keys():
  6.                 freq_arr[num] += 1
  7.             else:
  8.                 freq_arr[num] = 1
  9.         res = 0
  10.         for key in freq_arr.keys():
  11.             if (key+1) in freq_arr.keys():
  12.                 res += freq_arr[key]
  13.         return res
复制代码
回复

使用道具 举报

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

本版积分规则

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