Todos los desafíos
Fácil 0 resueltos 0% de aceptación

Coin Toss Streaks

Find the longest streak of heads, the longest streak of tails and the number of streaks in a coin toss record.

Lo que aprenderás

  • Split a sequence into maximal runs
  • Track the best run for each value
  • Count runs

Problema

Ver traducción

A probability lab flips a coin many times and writes the results as a string of H (heads) and T (tails). A streak is a maximal group of consecutive equal results; HHTHHH has the streaks HH, T and HHH. Students are often surprised how long the longest streak in a random record is, so the lab measures it.

Implement coin_toss_streaks(tosses).

Input

tosses: The results in order, using only H and T.

Output

Return [longest_heads, longest_tails, streaks]: the length of the longest heads streak, the length of the longest tails streak (0 if that side never comes up) and the total number of streaks.

Restricciones

0 ≤ length of tosses ≤ 100,000

Ejemplos

Ejemplo 1
Entrada:
tosses = "HHTHHH"

Salida esperada:
expected = [3, 1, 3]
Entrada:
std::string tosses = "HHTHHH";

Salida esperada:
std::vector<long long> expected = {3, 1, 3};
Entrada:
string tosses = "HHTHHH";

Salida esperada:
long[] expected = new long[] {3, 1, 3};

Explicación

The streaks are HH, T and HHH, so the longest heads streak is 3, the longest tails streak is 1, and there are 3 streaks.

Ejemplo 2
Entrada:
tosses = "TTTT"

Salida esperada:
expected = [0, 4, 1]
Entrada:
std::string tosses = "TTTT";

Salida esperada:
std::vector<long long> expected = {0, 4, 1};
Entrada:
string tosses = "TTTT";

Salida esperada:
long[] expected = new long[] {0, 4, 1};

Explicación

Heads never comes up, so its longest streak is 0; there is one tails streak of length 4: [0, 4, 1].

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

A new streak starts at index 0 and wherever a toss differs from the one before.

Pista 2

Keep one best length for heads and one for tails.

Pista 3

An empty string has no streaks at all.

Complejidad esperada

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

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Clasificación

Clasificación completa

La mejor solución aprobada de cada programador. Las más rápidas, ligeras y mejor valoradas van primero.

Aún no hay soluciones aprobadas. Resuélvelo y quédate el primer puesto.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.