Đề bài
Return the smallest value in the list by checking each element once.
Implement array_minimum(numbers).
Input
numbers: A non-empty list of integers.
Output
Return the minimum.
Ràng buộc
1 ≤ length ≤ 100,000
Ví dụ
Đầu vào:
numbers = [3, 9, 2]
Kết quả mong đợi:
expected = 2
Đầu vào:
std::vector<long long> numbers = {3, 9, 2};
Kết quả mong đợi:
long long expected = 2;
Đầu vào:
long[] numbers = new long[] {3, 9, 2};
Kết quả mong đợi:
long expected = 2;
Giải thích
2 is smaller than 3 and 9.
Đầu vào:
numbers = [0, -5, 5]
Kết quả mong đợi:
expected = -5
Đầu vào:
std::vector<long long> numbers = {0, -5, 5};
Kết quả mong đợi:
long long expected = -5;
Đầu vào:
long[] numbers = new long[] {0, -5, 5};
Kết quả mong đợi:
long expected = -5;
Giải thích
-5 is the smallest.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Track the smallest value seen while scanning the list once.
Gợi ý 2
Compare each element to your current best and update only when smaller.
Gợi ý 3
Remember the list is non-empty, so you can start from the first element.
Độ 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.