Keep getting runtime error


  • 0
    K

    Hello there, I kept getting runtime error and below is my code. Haven't find out where the problem is. Any idea?

    public class Solution {
    public int maxProfit(int[] prices) {
        int lowestPrice = prices[0], mp = 0;
        for (int i = 0 ; i < prices.length; i++){
            lowestPrice = prices[i] > lowestPrice ? lowestPrice : prices[i];
            mp = mp >(prices[i]-lowestPrice ) ? mp : (prices[i]-lowestPrice);
        }
        return mp;
    }}

  • 2
    S

    Never forget edge case

    if (prices.length == 0) return 0;
    

  • 0
    K

    Well, problem solved. Thanks. Corners cases like this will be addressed in an interview?


  • 0
    S

    You should always concern about it by yourself. Mention it to interviewer will leave great impression.


Log in to reply
 

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