모든 챌린지
보통 1명 해결 정답률 100.0%

Largest Adjacent-Digit Product

Take every contiguous window of span digits and multiply its digits.

배우게 될 내용

  • 고정 크기의 모든 윈도 살펴보기
  • 윈도 안의 숫자 곱하기
  • 가장 큰 곱 추적하기

문제

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.

예시

예시 1
입력:
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.

예시 2
입력:
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

윈도 안에 0이 있으면 곱은 0이 되므로, 연속된 비0 구간만 최대값에 기여할 수 있습니다.

힌트 2

슬라이딩 윈도나 숫자 구간별 곱을 추적해 매 구간을 처음부터 다시 계산하지 마세요.

힌트 3

정확한 길이를 주의하세요: 앞의 0도 자릿수로 세고, span이 비0 구간보다 길면 곱은 0입니다.

예상 복잡도

시간 O(n), 추가 공간 O(1)

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 975 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 솔루션

스포일러: 아래에 1개 전체 솔루션 있음 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.