Guest User

Untitled

a guest
Apr 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. def max_time(input)
  2. result = []
  3. input.each_key do |id|
  4. sum = 0
  5. result.push(total_duration(input, id, sum))
  6. end
  7. result.max
  8. end
  9.  
  10. def total_duration(input, start_id, sum)
  11. current_task = input[start_id]
  12. sum += current_task[:duration]
  13. return sum if current_task[:dependency] == -1
  14. dep_id = current_task[:dependency]
  15. total_duration(input, dep_id, sum)
  16. end
  17.  
  18. input1 = { 1 => { duration: 3, dependency: -1 },
  19. 2 => { duration: 5, dependency: -1 },
  20. 3 => { duration: 4, dependency: 1 } }
  21. input2 = { 1 => { duration: 3, dependency: 2 },
  22. 2 => { duration: 5, dependency: -1 },
  23. 3 => { duration: 4, dependency: 1 } }
  24. input3 = { 1 => { duration: 3, dependency: 2 },
  25. 2 => { duration: 5, dependency: -1 },
  26. 3 => { duration: 4, dependency: 1 },
  27. 4 => { duration: 6, dependency: 3 } }
  28. p max_time(input1)
  29. p max_time(input2)
  30. p max_time(input3)
Add Comment
Please, Sign In to add comment