leetcode:Best Time to Buy and Sell Stock

来源:互联网 发布:赛亚人遮脸的软件 编辑:程序博客网 时间:2024/04/27 13:41


给出一个一维的数组prices[],prices[i]表示第i个点上股票的价格,

现在给出一个事务交易,买进后卖出,请问可最大获利多少


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


0 0
原创粉丝点击