贪心算法---The best time to buy and sell store-ii
2024-09-01 05:30:36
Say you have an array for which the i th 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 Solution {
public int maxProfit(int[] prices) {
int sum = 0;
if(prices.length <= 1)
return 0;
for(int i=0; i < prices.length-1 ; i++){
if(prices[i+1] > prices[i]){
int y = prices[i+1] - prices[i];
sum = sum + y;
}
}
if(sum<0)
return 0;
else
return sum;
}
}
最新文章
- CentOS7下安装并简单设置PostgreSQL笔记
- 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
- PostgreSQL入门
- css渐变色DIV
- 转!mysql 查询 distinct多个字段 注意!!
- Javascript函数、构造函数、原型、类和对象
- android 读中文文本文件
- EMVTag系列4《5A 应用主账号》
- linux查看CPU性能及工作状态的指令
- Web Api Route 注册要放在 Mvc Route 注册前
- Struts工作机制图+OGNL+EL+值栈(Map,对象栈)
- js在web绘制在页上的圆
- 《CoderXiaoban团队》第一次作业:团队亮相
- Python实战:网络爬虫都能干什么?
- 【Entity Framework】Model First Approach
- python实现定时任务
- sqlserver触发器执行顺序【未经验证】
- Javascript 函数传参问题
- P4论文粗读笔记(一)
- NULL指针引起的一个linux内核漏洞