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

刷题记录帖子

🔗
 楼主| Myron2017 2022-1-17 07:36:36 | 只看该作者
全局:
Leetcode 2140. Solving Questions With Brainpower

感觉中文力扣的解答真的不错,都是直击心灵的。



推荐这个大神,真是大神,给出的解答都是直接心灵的。

比如这道题目,逆向 DP,



正向 DP



回复

使用道具 举报

🔗
 楼主| Myron2017 2022-8-3 09:00:35 | 只看该作者
全局:
1041. Robot Bounded In Circle

这个题目其实不难,但是难度在于如何方便快速的判断和模拟。

主要点事两个,把机器人的属性拆成两个,机器人脸的朝向 和 位置(x,y)
  1. class Solution:
  2.     def isRobotBounded(self, instructions: str) -> bool:
  3.         dirs = [(0,1),(-1,0),(0,-1),(1,0)]
  4.         d = 0
  5.         x = y = 0
  6.         for op in instructions:
  7.             if op == "G":
  8.                 x += dirs[d][0]
  9.                 y += dirs[d][1]
  10.             elif op == "L":
  11.                 d = (d + 1) % 4
  12.             else:
  13.                 d = (d - 1) % 4
  14.             print(x,y,d)
  15.         return x == y == 0 or d != 0
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2022-8-3 09:48:59 | 只看该作者
全局:
2359. Find Closest Node to Given Two Nodes

周赛题目,比较坑爹,这个题目的理解问题。

such that the maximum between the distance from node1 to that node, and from node2 to that node is minimized.


这个其实是,

min of max( value1, value2)
回复

使用道具 举报

🔗
 楼主| Myron2017 2023-3-18 11:36:02 | 只看该作者
全局:
1472. Design Browser History
  1. class BrowserHistory:

  2.     def __init__(self, homepage: str):
  3.         self.history_stack = [homepage]
  4.         self.current = 0        

  5.     def visit(self, url: str) -> None:
  6.         if self.current >= 0:
  7.             self.history_stack = self.history_stack[:self.current+1]
  8.         self.history_stack.append(url)
  9.         self.current += 1

  10.     def back(self, steps: int) -> str:
  11.         if steps <= self.current:
  12.             self.current = self.current - steps
  13.         else:
  14.             self.current  = 0
  15.         #print(self.history_stack)  
  16.         #print(self.current)         
  17.         return self.history_stack[self.current]
  18.         
  19.     def forward(self, steps: int) -> str:
  20.         if steps <= (len(self.history_stack) - 1 - self.current):
  21.             self.current = self.current + steps
  22.         else:
  23.             self.current  = len(self.history_stack) - 1
  24.         return self.history_stack[self.current]
  25.         


  26. # Your BrowserHistory object will be instantiated and called as such:
  27. # obj = BrowserHistory(homepage)
  28. # obj.visit(url)
  29. # param_2 = obj.back(steps)
  30. # param_3 = obj.forward(steps)
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2025-1-18 23:26:01 | 只看该作者
全局:
本帖最后由 Myron2017 于 2025-1-18 09:32 编辑
  1. # LC 3417 Zigzag Grid Traversal With Skip<div class="blockcode"><blockquote># LC 3417 Zigzag Grid Traversal With Skip

  2. class Solution:
  3.     def zigzagTraversal(self, grid: List[List[int]]) -> List[int]:
  4.         curr_x, curr_y = 0, 0
  5.         len_y, len_x = len(grid), len(grid[0])
  6.         res = list()
  7.         direction = 1

  8.         while curr_x < len_x and curr_y < len_y:
  9.             res.append(grid[curr_y][curr_x])
  10.             curr_x = curr_x + direction * 2
  11.             while curr_x < len_x and curr_x >= 0:
  12.                 res.append(grid[curr_y][curr_x])
  13.                 curr_x = curr_x + direction * 2
  14.             curr_y += 1
  15.             direction *= -1
  16.             if curr_x == -2: curr_x = 1
  17.             if curr_x == len_x + 1: curr_x = len_x - 2
  18.             if curr_x == -1: curr_x = 0
  19.             if curr_x == len_x: curr_x = len_x -1
  20.             

  21.         return res


  22.             
  23. # A more elegent solution
  24. # using i % 2 and the iteration to control order and reversed order of list

  25. class Solution:
  26.     def zigzagTraversal(self, grid: List[List[int]]) -> List[int]:
  27.         res = []
  28.         for i, r in enumerate(grid):
  29.             res += r[::-1] if i % 2 else r
  30.         return res[::2]
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2025-1-19 08:54:33 | 只看该作者
全局:
  1. # 3263. Convert Doubly Linked List to Array I

  2. """
  3. # Definition for a Node.
  4. class Node:
  5.     def __init__(self, val, prev=None, next=None):
  6.         self.val = val
  7.         self.prev = prev
  8.         self.next = next
  9. """
  10. class Solution:
  11.     def toArray(self, root: 'Optional[Node]') -> List[int]:
  12.         node = root
  13.         res = []        
  14.         while node:
  15.             res.append(node.val)
  16.             node = node.next
  17.         return res
  18.         
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2025-1-20 09:03:13 | 只看该作者
全局:
  1. # 2879. Display the First Three Rows

  2. import pandas as pd

  3. def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame:
  4.     return employees.head(3)
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2025-1-20 12:20:21 | 只看该作者
全局:
LC 1265
  1. # """
  2. # This is the ImmutableListNode's API interface.
  3. # You should not implement it, or speculate about its implementation.
  4. # """
  5. # class ImmutableListNode:
  6. #     def printValue(self) -> None: # print the value of this node.
  7. #     def getNext(self) -> 'ImmutableListNode': # return the next node.

  8. class Solution:
  9.     def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None:
  10.         # res = []
  11.         # curr = head
  12.         # while curr:
  13.         #     res.append(curr.printValue())
  14.         #     curr = curr.getNext()
  15.         # print(res)
  16.         # return [res[x] for x in range(len(res)-1, -1, -1)]

  17.         '''
  18.         # Recursion
  19.         if head is not None:
  20.             self.printLinkedListInReverse(head.getNext())
  21.             head.printValue()
  22.         '''

  23.         # stack
  24.         stack = []

  25.         while head:
  26.             stack.append(head)
  27.             head = head.getNext()
  28.         
  29.         while stack:
  30.             node = stack.pop()
  31.             node.printValue()
复制代码
回复

使用道具 举报

🔗
 楼主| Myron2017 2025-10-31 10:11:13 | 只看该作者
全局:
  1. class Solution:
  2.     def getSneakyNumbers(self, nums: List[int]) -> List[int]:
  3.         ss = set()
  4.         res = []
  5.         for n in nums:
  6.             if n not in ss:
  7.                 ss.add(n)
  8.             else:
  9.                 res.append(n)
  10.         return res
复制代码
LC 3289
回复

使用道具 举报

🔗
 楼主| Myron2017 2025-10-31 10:30:12 | 只看该作者
全局:
本帖最后由 Myron2017 于 2025-10-30 21:31 编辑
  1. class Solution:
  2.     def countValidSelections(self, nums: List[int]) -> int:
  3.         res = 0
  4.         l, r = 0, sum(nums)
  5.         for i in range(len(nums)):
  6.             if nums[i] == 0:
  7.                 if l == r : res += 2
  8.                 if l - 1 == r or l == r-1: res += 1
  9.             else:
  10.                 l += nums[i]
  11.                 r -= nums[i]
  12.         return res
复制代码
LC 3354



sum_left --> sum of all elements in left to nums[i]
sum_right --> sum of all elements in right to nums[i]
when nums[i] ==0 and sum_right == sum_left : count+=2
when nums[i] ==0 and sum_right-1==sum_left : count +=1
when nums[i] == 0 and sum_right == sum_left-1 : count +=1
回复

使用道具 举报

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

本版积分规则

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