Tous les défis
Moyen 1 résolu 100.0 % de réussite

Largest Adjacent-Digit Product

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

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

Exemples

Exemple 1
Entrée :
digits = "23405"
span = 2

Sortie attendue :
expected = 12
Entrée :
std::string digits = "23405";
long long span = 2;

Sortie attendue :
long long expected = 12;
Entrée :
string digits = "23405";
long span = 2;

Sortie attendue :
long expected = 12;

Explication

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

Exemple 2
Entrée :
digits = "102"
span = 3

Sortie attendue :
expected = 0
Entrée :
std::string digits = "102";
long long span = 3;

Sortie attendue :
long long expected = 0;
Entrée :
string digits = "102";
long span = 3;

Sortie attendue :
long expected = 0;

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

# Codeur Langage Temps Mémoire Note de l'IA Solution
1 ННикита Python 975 ms - 4/5 Voir le code

Apprenez des autres

Solutions de la communauté

Attention spoiler : 1 solution complète ci-dessous Essayez d'abord de le résoudre vous-même. Vous pouvez toujours jeter un œil au code des autres développeurs et aux avis IA. Connectez-vous et résolvez le défi pour rejoindre la discussion.