1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_122.java 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
Steve Sun 提交于 7年前 . refactor 122
package com.fishercoder.solutions;
/**
* 122. Best Time to Buy and Sell Stock II
*
* Say you have an array for which the ith element is the price of a given stock on day i.
* Design an algorithm to find the maximum profit. You may complete as many transactions as you like
* (ie, buy one and sell one share of the stock multiple times).
* However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
* */
public class _122 {
public static class Solution1 {
//peak and valley approach
public int maxProfit(int[] prices) {
int pro = 0;
int i = 0;
while (i < prices.length - 1) {
while (i < prices.length - 1 && prices[i] >= prices[i + 1]) {
i++;
}
int valley = prices[i];
while (i < prices.length - 1 && prices[i] <= prices[i + 1]) {
i++;
}
int peak = prices[i];
pro += peak - valley;
}
return pro;
}
}
public static class Solution2 {
//simple one pass approach: the above solution could be simplied as below
public int maxProfit(int[] prices) {
int pro = 0;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i + 1] > prices[i]) {
pro += prices[i + 1] - prices[i];
}
}
return pro;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助