簡単 1件解決済み 正答率100.0%

Array Maximum

Find the largest value in a list.

このチャレンジで学べること

  • Find the largest value in one pass
  • Initialize the maximum with the first element
  • Work with negative numbers

問題

翻訳を表示

Return the largest value in the list by checking each element once.

Implement array_maximum(numbers).

Input

numbers: A non-empty list of integers.

Output

Return the maximum.

制約

1 ≤ length ≤ 100,000

例

例 1
入力:
numbers = [3, 9, 2]

期待される出力:
expected = 9
入力:
std::vector<long long> numbers = {3, 9, 2};

期待される出力:
long long expected = 9;
入力:
long[] numbers = new long[] {3, 9, 2};

期待される出力:
long expected = 9;

解説

9 is larger than 3 and 2.

例 2
入力:
numbers = [-4, -1, -7]

期待される出力:
expected = -1
入力:
std::vector<long long> numbers = {-4, -1, -7};

期待される出力:
long long expected = -1;
入力:
long[] numbers = new long[] {-4, -1, -7};

期待される出力:
long expected = -1;

解説

With only negative values, -1 is the largest.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Start with the first element as the best value so far.

ヒント 2

Scan the list once and update the best value whenever you find a larger one.

ヒント 3

Do not assume values are positive; negatives can still be the maximum.

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 RCRiccardo Corti Python 840 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が1件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。