Alle Herausforderungen
Mittel 1 gelöst 100.0% Erfolgsquote

Largest Adjacent-Digit Product

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

Das lernst du

  • Examine every window of a fixed size
  • Multiply digits inside a window
  • Track the best product

Problem

Übersetzung anzeigen

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.

Einschränkungen

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

Beispiele

Beispiel 1
Eingabe:
digits = "23405"
span = 2

Erwartete Ausgabe:
expected = 12
Eingabe:
std::string digits = "23405";
long long span = 2;

Erwartete Ausgabe:
long long expected = 12;
Eingabe:
string digits = "23405";
long span = 2;

Erwartete Ausgabe:
long expected = 12;

Erläuterung

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

Beispiel 2
Eingabe:
digits = "102"
span = 3

Erwartete Ausgabe:
expected = 0
Eingabe:
std::string digits = "102";
long long span = 3;

Erwartete Ausgabe:
long long expected = 0;
Eingabe:
string digits = "102";
long span = 3;

Erwartete Ausgabe:
long expected = 0;

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

A zero inside a window forces that product to zero, so only contiguous nonzero stretches can contribute to the maximum.

Hinweis 2

Use a sliding window or track products across runs of digits to avoid recomputing every span from scratch.

Hinweis 3

Watch the exact span requirement: leading zeros count as digits, and if span exceeds a nonzero block, the product is zero.

Erwartete Komplexität

O(n) time, O(1) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

# Coder Sprache Zeit Speicher KI-Bewertung Lösung
1 ННикита Python 975 ms - 4/5 Code ansehen

Von anderen lernen

Community-Lösungen

Achtung Spoiler: 1 vollständige Lösung unten Versuche es zuerst selbst zu lösen. Du kannst trotzdem einen Blick auf den Code und die KI-Reviews anderer Entwickler werfen. Melde dich an und löse die Aufgabe, um an der Diskussion teilzunehmen.