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;
}
}

  

最新文章

  1. CentOS7下安装并简单设置PostgreSQL笔记
  2. 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
  3. PostgreSQL入门
  4. css渐变色DIV
  5. 转!mysql 查询 distinct多个字段 注意!!
  6. Javascript函数、构造函数、原型、类和对象
  7. android 读中文文本文件
  8. EMVTag系列4《5A 应用主账号》
  9. linux查看CPU性能及工作状态的指令
  10. Web Api Route 注册要放在 Mvc Route 注册前
  11. Struts工作机制图+OGNL+EL+值栈(Map,对象栈)
  12. js在web绘制在页上的圆
  13. 《CoderXiaoban团队》第一次作业:团队亮相
  14. Python实战:网络爬虫都能干什么?
  15. 【Entity Framework】Model First Approach
  16. python实现定时任务
  17. sqlserver触发器执行顺序【未经验证】
  18. Javascript 函数传参问题
  19. P4论文粗读笔记(一)
  20. NULL指针引起的一个linux内核漏洞

热门文章

  1. 文件识别浅谈(含office文件区分)
  2. SQL经典练习题50--mysql
  3. 开源SQL审核平台——Archery 安装、部署心得
  4. POJ 2778:DNA Sequence(AC自动机构造矩阵)
  5. tomcat 启动窗口 名称 中文乱码
  6. django基础知识之GET属性:
  7. SpringMvc整合hibernate
  8. Linux命令学习-mv命令
  9. Bzoj 3165 [Heoi2013]Segment题解
  10. MyBatis从入门到精通:第一章实体类与Mapper.xml文件