0

when I create List - program executed for 2.5 ms, if I changed List to ArrayList - program executed for 1.1ms.... who knows why?

class StockSpanner {

    ArrayList<Integer> list; //1.1 ms
    //List<Integer> list; 2.5 ms

    public StockSpanner(){
       list = new ArrayList<>();
    }

    public int next(int price) {
        int less = 0;
        int counter = 0;        

        list.add(price);
        counter = list.size() - 1;

        while(counter >= 0){
            if(list.get(counter) <= price) {
                less++;
                counter--;
            } else {break;}
        }
        return less;
    }
}
gervais.b
  • 2,294
  • 2
  • 22
  • 46
  • 3
    How many times have you run this. Based upon the simplistically of your questions, maybe you have not done proper performance testing – Scary Wombat May 20 '20 at 05:37
  • @ScaryWombat I did it many times on leetcode engine. You can try my code here https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3334/ – RUSLAN SIMAKOV May 20 '20 at 05:58

0 Answers0