LeetCode 122 Best Time to Buy and Sell Stock II
2015-11-30 09:08
441 查看
题目描述
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).
分析
只要有利润就卖,就是最优解。代码
[code] public static int maxProfit(int[] prices) { if (prices.length <= 1) { return 0; } int hold = prices[0]; int profit = 0; for (int i = 1; i < prices.length; i++) { if (hold < prices[i]) { profit += prices[i] - hold; } hold = prices[i]; } return profit; }
相关文章推荐
- 软件开发过程自动化原理及技术(完整示例)
- html5 css3 入门教程
- CSS基础知识
- 【Java并发编程实战】-----“J.U.C”:Exchanger
- 易错点:循环临界条件的设置(素数版)
- POJ2681——求字符串长度
- Spring MVC+ ztree动态实现
- XSuperMES移动端运用FragmentActivity适配大屏幕(二)
- Android studio 提示org.gradle.jvmargs=-Xmx1024m类似错误
- 为什么估值总是错的——对金融学的哲学思考
- tablewidget 选中去虚框
- 互联网产品跨部门沟通的10个原则(转)
- ubuntu语言支持找不到执行下面命令
- 用户友好界面
- ubuntu下配置tftp服务器
- sql herniate
- 单例模式
- 字典的基本用法
- Bootstrap每天必学之进度条
- XSuperMES移动端运用FragmentActivity适配大屏幕(一)