Key With Maximum Value
Python
Easy
3 views
Problem Description
Read n pairs key value (integer). Compute the key with maximum value. If tie, pick lexicographically smallest key. Output the key.
Input Format
First line n. Next n lines: key value.
Sample Test Case
Input:
4
a 5
b 9
c 9
aa 9
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
n=int(lines[0].strip())
bestKey=None
bestVal=None
for i in range(1,1+n):
parts=(lines[i] if i<len(lines) else '').split()
if len(parts)<2: continue
k=parts[0]
v=int(parts[1])
if bestVal is None or v>bestVal or (v==bestVal and k<bestKey):
bestVal=v
bestKey=k
sys.stdout.write(bestKey if bestKey is not None else '')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!