中級 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 つずつ開いてみましょう。

ヒント 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 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。