Tutte le sfide
Media 1 risolta 100.0% di accettazione

Largest Adjacent-Digit Product

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

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

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

Esempi

Esempio 1
Input:
digits = "23405"
span = 2

Output atteso:
expected = 12
Input:
std::string digits = "23405";
long long span = 2;

Output atteso:
long long expected = 12;
Input:
string digits = "23405";
long span = 2;

Output atteso:
long expected = 12;

Spiegazione

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

Esempio 2
Input:
digits = "102"
span = 3

Output atteso:
expected = 0
Input:
std::string digits = "102";
long long span = 3;

Output atteso:
long long expected = 0;
Input:
string digits = "102";
long span = 3;

Output atteso:
long expected = 0;

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 Н"Никита "Programmer" Python 975 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 1 soluzione completa sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.