Tất cả thử thách
Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Largest Adjacent-Digit Product

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

Đề bài

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.

Ràng buộc

1 ≤ digit-string length ≤ 100,000. Characters are 0…9. 1 ≤ span ≤ min(18, length). All products fit signed 64-bit.

Ví dụ

Ví dụ 1
Đầu vào:
span = 2
digits = "23405"

Kết quả mong đợi:
expected = 12
Đầu vào:
long long span = 2;
std::string digits = "23405";

Kết quả mong đợi:
long long expected = 12;
Đầu vào:
long span = 2;
string digits = "23405";

Kết quả mong đợi:
long expected = 12;

Giải thích

The windows 23, 34, 40, and 05 have products 6, 12, 0, and 0. The largest is 12.

Ví dụ 2
Đầu vào:
span = 3
digits = "102"

Kết quả mong đợi:
expected = 0
Đầu vào:
long long span = 3;
std::string digits = "102";

Kết quả mong đợi:
long long expected = 0;
Đầu vào:
long span = 3;
string digits = "102";

Kết quả mong đợi:
long expected = 0;

Giải thích

There is only one width-three window. It contains zero, so 1 × 0 × 2 = 0.

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.