समस्या
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.
सीमाएँ
1 ≤ length ≤ 100,000
उदाहरण
इनपुट:
numbers = [3, 9, 2]
अपेक्षित आउटपुट:
expected = 2
इनपुट:
std::vector<long long> numbers = {3, 9, 2};
अपेक्षित आउटपुट:
long long expected = 2;
इनपुट:
long[] numbers = new long[] {3, 9, 2};
अपेक्षित आउटपुट:
long expected = 2;
व्याख्या
2 is smaller than 3 and 9.
इनपुट:
numbers = [0, -5, 5]
अपेक्षित आउटपुट:
expected = -5
इनपुट:
std::vector<long long> numbers = {0, -5, 5};
अपेक्षित आउटपुट:
long long expected = -5;
इनपुट:
long[] numbers = new long[] {0, -5, 5};
अपेक्षित आउटपुट:
long expected = -5;
व्याख्या
-5 is the smallest.
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Track the smallest value seen while scanning the list once.
संकेत 2
Compare each element to your current best and update only when smaller.
संकेत 3
Remember the list is non-empty, so you can start from the first element.
अपेक्षित जटिलता
O(n) time, O(1) extra space
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।