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

9.26新鲜热乎的狗狗昂塞

 
🔗
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 | 只看该作者
全局:
第一题:
比较简单遍历所有可能就行了
  1. def uniqueSubsetSum(A):
  2.     ans = {0}  # set initialization
  3.     N = len(A)
  4.     for i in range(N):
  5.         cur_ans = {A[i]}
  6.         for s in ans:
  7.             cur_ans.add(s+A[i])
  8.         ans = ans.union(cur_ans)
  9.     return list(ans)
复制代码


第二题的话,有点小复杂,我感觉前面一些人说的不确定对不对,可以看思路,就是找特殊点

  1. def kthUser(logs, k):
  2.     ans, must, possible = [], [], []
  3.     for _, s, e in logs:
  4.         ans.append(s)
  5.         ans.append(e)

  6.     ans = sorted(ans)
  7.     for t in ans:
  8.         cur_must, cur_possible = [], []
  9.         for user_id, s, e in logs:
  10.             if t >= e:
  11.                 cur_must.append(user_id)
  12.             if s <= t < e:
  13.                 cur_possible.append(user_id)
  14.             must.append(cur_must)
  15.             possible.append(cur_possible)

  16.     res = set()
  17.     for idx, must_values in enumerate(must):
  18.         if k - len(possible[idx]) <= len(must_values) < k:
  19.             for i in possible[idx]:
  20.                 res.add(i)

  21.     return list(res)


  22. logs = [[1,20,30], [2,0,50], [3,45,70], [4,35,55]]
  23. for k in [1, 2, 3, 4]:
  24.     print "k=%s" % k, kthUser(logs, k)
复制代码


第三题,宝宝不会,没想出来。求详细论证第三题

第四题,如果单纯计算岛屿的数量直接用dfs就可以求出来,但是要求distinct,其实就是对岛屿做一个归一化处理而已,其实全部上下左右移动好,求一个唯一key就行了
  1. def NumberOfDistinctIsland(matrix):
  2.     import sys

  3.     def dfs(i, j, group):
  4.         if i >= N or j >= M:
  5.             return None

  6.         if matrix[i][j] == 1:
  7.             group.append((i, j))
  8.             matrix[i][j] = 0
  9.             dfs(i+1, j, group)
  10.             dfs(i, j + 1, group)
  11.             dfs(i + 1, j + 1, group)

  12.     def transform(group):
  13.         group = sorted(group)
  14.         min_i, min_j = sys.maxint, sys.maxint
  15.         for i, j in group:
  16.             min_i = min(i, min_i)
  17.             min_j = min(j, min_j)
  18.         key = ""
  19.         for i, j in group:
  20.             key += "%s,%s" % (i-min_i, j-min_j)
  21.         return key

  22.     N = len(matrix)
  23.     M = len(matrix[0])
  24.     groups = []
  25.     for i in range(N):
  26.         for j in range(M):
  27.             if matrix[i][j] == 1:
  28.                 group = []
  29.                 dfs(i, j, group)
  30.                 groups.append(transform(group))
  31.     return len(set(groups))


  32. matrix = [
  33. [1, 1, 1, 0, 0, 0],
  34. [1, 1, 0, 0, 0, 0],
  35. [0, 0, 0, 1, 0, 0],
  36. [1, 1, 1, 0, 0, 0],
  37. [1, 1, 0, 0, 1, 1]]

  38. print NumberOfDistinctIsland(matrix)
复制代码

回复

使用道具 举报

🔗
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面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

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