Java bit 94.59%


  • 1
    /**
     * we can easy deal this problem with using dynamic programming.<br/>
     * Without any difficulty<br/>
     * <p>
     * <strong>result of test:</strong><br/>
     * 200 / 200 test cases passed<br/>
     * Status: Accepted<br/>
     * Runtime: 1 ms, 94.59%<br/>
     *
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        int profit = 0;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < min) {
                min = prices[i];
            }
            profit = (prices[i] - min > profit) ? prices[i] - min : profit;
        }
        return profit;
    }

Log in to reply
 

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