Đề bài
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.
Ràng buộc
1 ≤ length ≤ 100,000
Ví dụ
Đầu vào:
numbers = [3, 9, 2]
Kết quả mong đợi:
expected = 9
Đầu vào:
std::vector<long long> numbers = {3, 9, 2};
Kết quả mong đợi:
long long expected = 9;
Đầu vào:
long[] numbers = new long[] {3, 9, 2};
Kết quả mong đợi:
long expected = 9;
Giải thích
9 is larger than 3 and 2.
Đầu vào:
numbers = [-4, -1, -7]
Kết quả mong đợi:
expected = -1
Đầu vào:
std::vector<long long> numbers = {-4, -1, -7};
Kết quả mong đợi:
long long expected = -1;
Đầu vào:
long[] numbers = new long[] {-4, -1, -7};
Kết quả mong đợi:
long expected = -1;
Giải thích
With only negative values, -1 is the largest.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Start with the first element as the best value so far.
Gợi ý 2
Scan the list once and update the best value whenever you find a larger one.
Gợi ý 3
Do not assume values are positive; negatives can still be the maximum.
Độ phức tạp mong đợi
O(n) time, O(1) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.