题解 | #买卖股票的最好时机(一)#

买卖股票的最好时机(一)

https://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec

import java.util.*;


public class Solution {
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        // write code here
        int min = prices[0];
        int profit = 0;
        int n = prices.length;
        if(n <= 1){
            return 0;
        }
        for(int i = 1; i < n; i++){
            profit = Math.max(profit, prices[i] - min);
            if(prices[i] < min){
                min = prices[i];
            }
        }
        return profit;
    }
}

算法题收录 文章被收录于专栏

收录所有算法题目解法

全部评论

相关推荐

点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务