| 123456789101112131415161718192021222324252627282930313233343536373839 |
- # 1560. Most Visited Sector in a Circular Track
- # @param {Integer} n
- # @param {Integer[]} rounds
- # @return {Integer[]}
- def most_visited(n, rounds)
- uses = Hash.new(0)
- i = 0
- while i < (rounds.size()-1)
- x = rounds[i]
- tgt = rounds[i+1]
- while x != tgt
- uses[x] += 1
- x = x+1 > n ? 1 : x+1
- end
- i += 1
- end
- uses[rounds[-1]] += 1
- max_amount = 0
- uses.each_pair {|k, v| if v > max_amount then max_amount = v end}
- res = []
- uses.each_pair {|k, v| if v == max_amount then res.push(k) end}
- res.sort()
- end
- def main()
- puts String(most_visited(4, [1, 3, 1, 2]) == [1,2])
- puts String(most_visited(2, [2,1,2,1,2,1,2,1,2]) == [2])
- puts String(most_visited(7, [1,3,5,7]) == [1,2,3,4,5,6,7])
- end
- if __FILE__ == $0
- main()
- end
|