title: Why using stack container is slower?
body: <p>I 'm trying to solve problem 739, Daily Temperatures on LeetCode.
  <a href="https://leetcode.com/problems/daily-temperatures/" rel="nofollow noreferrer">https://leetcode.com/problems/daily-temperatures/</a></p>
  
  <p>My code used Stack container provided by the JAVA. It takes 60 ms to run. This is my code:</p>
  
  <pre><code>class Solution {
      public int[] dailyTemperatures(int[] T) {
          int[] ret = new int[T.length];
          Stack&lt;Integer&gt; stack = new Stack&lt;Integer&gt;();
          for(int i=0; i &lt; T.length; i++){
              while(!stack.isEmpty() &amp;&amp; T[i] &gt; T[stack.peek()]){
                  int index = stack.pop();
                  ret[index] = i - index;             
              }
              stack.push(i);
          }
          return ret;
      }
  }
  </code></pre>
  
  <p>Here is a code that only takes 6ms to run:</p>
  
  <pre><code>class Solution {
      public int[] dailyTemperatures(int[] T) {
  
          int[] temperatures = T;
          if(temperatures == null) return null;
  
          int[] result = new int[temperatures.length];
          int[] stack = new int[temperatures.length];
          int top = 0;
          stack[top] = -1;
  
          for(int i = 0; i &lt; temperatures.length; i++) {
              while(stack[top] != -1 &amp;&amp; temperatures[i] &gt; temperatures[stack[top]]) {
                  int index = stack[top--];
                  result[index] = i - index;
              }
  
              stack[++top] = i;
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>Why building the stack using an array is faster than using the stack container?</p>