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

9.26新鲜热乎的狗狗昂塞

 
🔗
DylanZhang 2018-10-5 00:15:49 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
hdfaji 2018-10-5 00:56:22 | 只看该作者
全局:
DylanZhang 发表于 2018-10-5 00:15
这样好像同一个数可能被sum 两次,比如 4本来是算不出来的。但是2,可以算出来,4-2 = 2(true),结果4  ...

是的,我的想法错的,感谢指正
回复

使用道具 举报

🔗
mtrsen 2018-10-17 00:20:44 | 只看该作者
全局:
DylanZhang 发表于 2018-10-5 00:15
这样好像同一个数可能被sum 两次,比如 4本来是算不出来的。但是2,可以算出来,4-2 = 2(true),结果4  ...

所以可以反向更新dp array先到4再到2
回复

使用道具 举报

🔗
tall_cool_13 2018-10-19 23:35:09 | 只看该作者
全局:
您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 100 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限 或 查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
回复

使用道具 举报

🔗
jygan 2018-10-21 23:17:42 | 只看该作者
全局:
二叉树游戏,大家有思路吗
回复

使用道具 举报

🔗
salamanderrex1 2018-12-27 23:38:50 | 只看该作者
全局:
1.
  1. def uniqueSubsetSum(nums):
  2.     nums = sorted(nums)
  3.     L = len(nums)
  4.     sums = {0}


  5.     def helper(start, c_sum):
  6.         if start >= L:
  7.             return

  8.         prev = float('inf')
  9.         for i in range(start, L):
  10.             if nums[i] == prev:
  11.                 continue

  12.             prev = nums[i]
  13.             c_sum += nums[i]
  14.             sums.add(c_sum)
  15.             helper(i + 1, c_sum)
  16.             c_sum -= nums[i]

  17.     helper(0,0)

  18.     return list(sums)

  19. print uniqueSubsetSum( [1,2,2])
复制代码





2.

  1. # should ask boundry include or not
  2. # refer meeting room, [url]https://leetcode.com/problems/meeting-rooms-ii/solution/[/url]
  3. def kthUser(log, K):
  4.     starts = map(lambda x: [x[1], x[0]], log)
  5.     ends = map(lambda x: [x[2], x[0]], log)

  6.     starts = sorted(starts)
  7.     ends = sorted(ends)

  8.     i = j = 0
  9.     activate_meetings = 0
  10.     in_room = set()
  11.     result = set()
  12.     escaped = 0
  13.     while i < len(starts) and j < len(ends):
  14.         # print 'in the room', in_room
  15.         # print 'escaped', escaped
  16.         # print 'activate', activate_meetings
  17.         if starts[i][0] < ends[j][0]:
  18.             activate_meetings += 1
  19.             #print 'adding to room', starts[i][1]
  20.             in_room.add(starts[i][1])
  21.             if activate_meetings + escaped>= K :
  22.                 result |= in_room

  23.             i += 1
  24.         else:
  25.             activate_meetings -= 1
  26.             escaped += 1
  27.             in_room.remove(ends[j][1])
  28.             if escaped == K:
  29.                 break
  30.             j += 1

  31.     print 'i,j', i, j
  32.     print result
复制代码




3.
  1. class TreeNode:
  2.     def __init__(self, val, left, right, parent):
  3.         self.val = val
  4.         self.left = left
  5.         self.right = right
  6.         self.parent = parent

  7. def treeGame(position):
  8.     counts = [0, 0, 0] # parent, left, right

  9.     def count(node):
  10.         if node is None:
  11.             return 0
  12.         c = count(node.left) + count(node.right) + 1


  13.         if position.left and position.left == node:
  14.             counts[1] = c
  15.         elif position.right and position.right == node:
  16.             counts[2] = c

  17.         return c

  18.     counts[0] = count(root)
  19.     counts[0] = counts[0] - counts[1] - counts[2] - 1


  20.     i = max(*[(counts[idx], idx) for idx in range(3)])[1]

  21.     if i == 0:
  22.         return position.parent.val
  23.     elif i == 1:
  24.         return position.left.val
  25.     else:
  26.         return position.right.val



  27. n1 = TreeNode(1, None, None, None)
  28. n2 = TreeNode(2, None, None, None)
  29. n3 = TreeNode(3, None, None, None)
  30. n4 = TreeNode(4, None, None, None)
  31. n5 = TreeNode(5, None, None, None)
  32. n6 = TreeNode(6, None, None, None)

  33. n1.left = n2
  34. n1.right = n3

  35. n2.left = n4


  36. n1.right = n3

  37. n3.left = n5
  38. n3.right = n6

  39. n4.parent = n2
  40. n2.parent = n1
  41. n3.parent = n1
  42. n5.parent = n3
  43. n6.parent = n3

  44. root = n1
  45. position = n2


  46. print treeGame(position)
复制代码





4.
  1. # lc 694
  2. def numDistinctIslands(grid):
  3.     results = set()
  4.     seen = set()

  5.     def explore(i, j, r0, c0):
  6.         if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] and (i, j) not in seen:
  7.             seen.add((i, j))
  8.             shape.add((i-r0,j-c0))
  9.             explore(i+1, j, r0, c0)
  10.             explore(i-1, j, r0, c0)
  11.             explore(i, j + 1, r0, c0)
  12.             explore(i, j - 1, r0, c0)

  13.     for i in range(len(grid)):
  14.         for j in range(len(grid[0])):
  15.             shape = set()

  16.             explore(i, j, i, j)
  17.             if shape:
  18.                 results.add(frozenset(shape))

  19.     return len(results)

  20. grid = [[1,1,0,0,0],
  21. [1,1,0,0,0],
  22. [0,0,0,1,1],
  23. [0,0,0,1,1],]

  24. print numDistinctIslands(grid)

  25. grid  = [[1,1,0,1,1],
  26. [1,0,0,0,0],
  27. [0,0,0,0,1],
  28. [1,1,0,1,1]]
  29. print numDistinctIslands(grid)




  30. grid = [
  31. [1, 1, 1, 0, 0, 0],
  32. [1, 1, 0, 0, 0, 0],
  33. [0, 0, 0, 1, 0, 0],
  34. [1, 1, 1, 0, 0, 0],
  35. [1, 1, 0, 0, 1, 1]]

  36. print numDistinctIslands(grid)
复制代码



回复

使用道具 举报

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

本版积分规则

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