| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- # 1560. Most Visited Sector in a Circular Track
- # @param {Integer} n
- # @param {Integer[]} rounds
- # @return {Integer[]}
- def most_visited(n, rounds)
- expanded = []
- expanded.push(rounds.shift())
- loop do
- nxt = rounds.shift()
- break if nxt.nil?()
- i = expanded.last()
- loop do
- i = i+1 > n ? 1 : i+1
- expanded.push(i)
- break if i == nxt
- end
- break if rounds.last().nil?()
- end
- uses = Hash.new(0)
- expanded.each {|x| uses[x] += 1}
- max_amount = 0
- uses.each_pair {|k, v| max_amount = v > max_amount ? v : max_amount}
- 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
|