문제
Look at every decimal digit of n and return the largest one.
Implement largest_digit(n).
Input
n: A positive integer.
Output
Return the largest digit, from 0 to 9.
제약 조건
1 ≤ n ≤ 10¹⁸
예시
예시 1
입력:
n = 352
예상 출력:
expected = 5
입력:
long long n = 352;
예상 출력:
long long expected = 5;
입력:
long n = 352;
예상 출력:
long expected = 5;
설명
The digits are 3, 5 and 2; the largest is 5.
예시 2
입력:
n = 7
예상 출력:
expected = 7
입력:
long long n = 7;
예상 출력:
long long expected = 7;
입력:
long n = 7;
예상 출력:
long expected = 7;
설명
The only digit is 7.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
Compare every decimal digit and keep the biggest one seen so far.
힌트 2
Extract digits one by one from the right, or inspect the string characters.
힌트 3
A number containing 9 anywhere immediately sets the answer to 9.
예상 복잡도
O(d) time, O(1) extra space