모든 챌린지
보통 0명 해결 정답률 0%

Longest Run of Rolls

Find where the longest run of equal consecutive dice rolls starts and how long it is.

배우게 될 내용

  • Detect where a run of equal values ends
  • Keep the start and length of the best run
  • Break ties toward the first run

문제

번역본 보기

A dice simulator records every roll of a six-sided die in a list. A run is a group of consecutive rolls with the same value; a single roll is a run of length 1. Find the longest run. If several runs share the longest length, report the one that starts first.

Implement longest_run_of_rolls(rolls).

Input

rolls: The rolls in the order they happened, each from 1 to 6.

Output

Return [start, length]: the 0-based index where the longest run starts and its length. For an empty list return [-1, 0].

제약 조건

0 ≤ number of rolls ≤ 10,000

1 ≤ roll ≤ 6

예시

예시 1
입력:
rolls = [1, 4, 4, 2, 6, 6, 6, 3]

예상 출력:
expected = [4, 3]
입력:
std::vector<long long> rolls = {1, 4, 4, 2, 6, 6, 6, 3};

예상 출력:
std::vector<long long> expected = {4, 3};
입력:
long[] rolls = new long[] {1, 4, 4, 2, 6, 6, 6, 3};

예상 출력:
long[] expected = new long[] {4, 3};

설명

The three 6s starting at index 4 beat the two 4s: [4, 3].

예시 2
입력:
rolls = [5, 5, 2, 2, 3]

예상 출력:
expected = [0, 2]
입력:
std::vector<long long> rolls = {5, 5, 2, 2, 3};

예상 출력:
std::vector<long long> expected = {0, 2};
입력:
long[] rolls = new long[] {5, 5, 2, 2, 3};

예상 출력:
long[] expected = new long[] {0, 2};

설명

Both pairs have length 2; the 5s start first, at index 0: [0, 2].

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Remember where the current run started.

힌트 2

When the value changes (or the list ends), compare the finished run with the best one.

힌트 3

Replace the best only when the new run is strictly longer.

예상 복잡도

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

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.