आसान 0 हल किए गए 0% स्वीकृति

Array Minimum

Find the smallest value in a list.

आप क्या सीखेंगे

  • Find the smallest value in one pass
  • Compare each element with the current best
  • Avoid a wrong starting value

समस्या

अनुवाद दिखाएँ

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

उदाहरण

उदाहरण 1
इनपुट:
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.

उदाहरण 2
इनपुट:
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।