Test cases are bad. O(n^2) solution ends up being the fastest


  • 0
    Q

    Take a simple example of strictly increasing stock prices. The code below should get TLE but end's up being faster than other Java solutions.

    public class Solution {
            public int maxProfit(int[] prices) {
                int maxProfit = 0;
                for (int i=0;i<prices.length;i++){
                    for (int j=i+1;j<prices.length;j++){
                        int profit = prices[j]-prices[i];
                        if (profit<=0){
                            break;
                        }
                        if (profit > maxProfit){
                            maxProfit = profit;
                        }
                    }
                }
                return maxProfit;
            }
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.