查看: 3600| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[树/链表/图] Atlassian oa题, 做不来求帮助。。

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
帮同学问的,在地里找到的oa题。没有头绪,求大佬指点


评分

参与人数 1大米 +5 收起 理由
14417335 + 5 很有用的信息!

查看全部评分


上一篇:回馈地里, 提供mock interview.
下一篇:找工作刷题贴~Java
🔗
gamesover 2019-4-6 02:57:07 | 只看该作者
全局:
搜一下吧,有人贴过答案,比较难理解的是u和v两个数据,意思是一个路径从u走到v,同一个index
比如
u = [2,4]
v = [1,0]
表示2条路径,从2<=>1和4<=>0,注意路径是双向的

  1. require 'set'

  2. def find_best_path(n, m, max_t, beauty, u, v, t)
  3.   $max_beauty = 0

  4.   paths = {}
  5.   times = {}
  6.   u.each_with_index do |from, i|
  7.     current = v[i]
  8.     times[i+1] = t[i]

  9.     if paths.key? from
  10.       paths[from] << current
  11.     else
  12.       paths[from] = [current]
  13.     end

  14.     if paths.key? current
  15.       paths[current] << from
  16.     else
  17.       paths[current] = [from]
  18.     end
  19.   end

  20.   dfs(paths, {0 => 1}, [].to_set, 0, beauty[0], max_t, times, beauty)
  21.   $max_beauty
  22. end

  23. def dfs(paths, visited_cities, visited_paths, city, current_beauties, time_left, times, beauties)
  24.   if time_left < 0
  25.     return
  26.   end

  27.   if city == 0
  28.     $max_beauty = [$max_beauty, current_beauties].max
  29.   end

  30.   paths[city].each do |next_city|
  31.     path = "#{city}|#{next_city}"
  32.    
  33.     if visited_paths.include?(path)
  34.       next
  35.     else
  36.       visited_paths << path
  37.     end

  38.     city_beauties = current_beauties
  39.     if visited_cities.key?(next_city)
  40.       visited_cities[next_city] += 1
  41.     else
  42.       visited_cities[next_city] = 1
  43.       city_beauties += beauties[next_city]  
  44.     end

  45.     spend_time = times[[city, next_city].max]

  46.     dfs(paths, visited_cities, visited_paths, next_city, city_beauties, time_left - spend_time, times, beauties)
  47.     visited_paths.delete(path)
  48.     visited_cities[next_city] -= 1
  49.     if visited_cities[next_city] == 0
  50.       visited_cities.delete(next_city)
  51.     end
  52.   end
  53. end

  54. u = [0,1,0]
  55. v = [1,2,3]
  56. t = [6,7,10]
  57. beauty = [5,10,15,20]

  58. p find_best_path(4, 3, 30, beauty, u, v, t) #30


  59. u = [0,2,0]
  60. v = [1,0,3]
  61. t = [10,13,17]
  62. beauty = [0,32,10,43]

  63. p find_best_path(4, 3, 49, beauty, u, v, t) #43
复制代码
回复

使用道具 举报

🔗
14417335 2019-4-6 03:52:21 | 只看该作者
全局:
这题有说是树,DAG或者是图吗?如果是树(猜的),那么还有个combinational sum的问题。比如时间充裕,我回到旅馆后还可以再选择一条线路。
回复

使用道具 举报

🔗
gamesover 2019-4-6 05:34:50 | 只看该作者
全局:
14417335 发表于 2019-4-6 03:52
这题有说是树,DAG或者是图吗?如果是树(猜的),那么还有个combinational sum的问题。比如时间充裕,我回 ...

更像无向图吧,你说的办法的确可以
回复

使用道具 举报

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

本版积分规则

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