문제
Take every contiguous window of span digits and multiply its digits. Return the largest product. Leading zeros are ordinary digits; a window containing zero has product zero. Do not reorder or skip digits.
Implement largest_adjacent_digit_product(digits, span).
Input
digits: The digit sequence, including any leading zeros.
span: The exact number of adjacent digits in a window.
Output
Return the greatest product over all windows of this width.
제약 조건
1 ≤ digit-string length ≤ 100,000. Characters are 0…9. 1 ≤ span ≤ min(18, length). All products fit signed 64-bit.
예시
입력:
digits = "23405"
span = 2
예상 출력:
expected = 12
입력:
std::string digits = "23405";
long long span = 2;
예상 출력:
long long expected = 12;
입력:
string digits = "23405";
long span = 2;
예상 출력:
long expected = 12;
설명
The windows 23, 34, 40, and 05 have products 6, 12, 0, and 0. The largest is 12.
입력:
digits = "102"
span = 3
예상 출력:
expected = 0
입력:
std::string digits = "102";
long long span = 3;
예상 출력:
long long expected = 0;
입력:
string digits = "102";
long span = 3;
예상 출력:
long expected = 0;
설명
There is only one width-three window. It contains zero, so 1 × 0 × 2 = 0.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
A zero inside a window forces that product to zero, so only contiguous nonzero stretches can contribute to the maximum.
힌트 2
Use a sliding window or track products across runs of digits to avoid recomputing every span from scratch.
힌트 3
Watch the exact span requirement: leading zeros count as digits, and if span exceeds a nonzero block, the product is zero.
예상 복잡도
O(n) time, O(1) extra space
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.