Problem : You are a Pokémon trainer. Each Pokémon has its own power, described by a positive integer value. As you travel, you watch Pokémon and you catch each of them. After each catch, you have to display maximum and minimum powers of Pokémon caught so far. You must have linear time complexity. So sorting won’t help here. Try having minimum extra space complexity. Examples: Suppose you catch Pokémon of powers 3 8 9 7. Then the output should be 3 3 3 8 3 9 3 9
Input : The single line describing powers of N Pokémon caught. Output : N lines stating minimum power so far and maximum power so far separated by single space
Code : Python code to implement Pokemon training game
# python code to train pokemon powers = [3, 8, 9, 7] mini, maxi = 0, 0 for power in powers: if mini == 0 and maxi == 0: mini, maxi = powers[0], powers[0] print(mini, maxi) else: mini = min(mini, power) maxi = max(maxi, power) print(mini, maxi) # Time Complexity is O(N) with Space Complexity O(1)
Output :
3 3 3 8 3 9 3 9
原文链接:codingdict.net