title: How does ruby's .min and .max method work, and what is the time complexity when using those methods to find min or max value in an array?
body: <p>Context: </p>
  
  <p>I was looking at an algorithms problem to determine max_stock_profit when given an array of stock prices. I was trying to determine the time complexity of said method, and encountered a <code>.min</code> used on an array within the <code>each</code> method. This led me to asking the question titled in this post.</p>
  
  <p>Generally speaking, when simply looking at the <code>.min</code> or <code>.max</code> method on an array of length 1,000,000 for example, wouldn't running <code>.min</code> or <code>.max</code> on the array require linear time O(n) to determine a min or max value, where n is the length of the array?..</p>
  
  <p>If so, then based on the example code shown below, by running <code>.min</code> or <code>.max</code> method within an <code>each</code> method, wouldn't the time complexity be O(n^2) since it needs to run another iteration within the <code>each</code> method to determine min value? I think the code snippet should be running at O(n) time but my lack of understanding of how <code>.min</code> and <code>.max</code> works is what's causing great confusion.</p>
  
  <p>Is it because since <code>.min</code> is invoked on an array containing only 2 values, it is okay assume that line of code operates in constant time O(1)? Any help to clear my misunderstanding of what's going on would be appreciated. Thanks.</p>
  
  <p>Example code snippet: </p>
  
  <pre><code>stock_prices = [5, 7, 2 ,4, 9, 1, 8]
  min_price = stock_prices[0]
  max_profit = 0
  
  stock_prices.each do |current_price|
      min_price = [min_price, current_price].min
      potential_profit = current_price - min_price  
      max_profit = [max_profit, potential_profit].max
  end
  </code></pre>