Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

找到最大顺序差值

int maxProfit(int* prices, int pricesSize) {
int min = 0;
int max = 0;
int result_max = 0;
// go over array
if(pricesSize == 0)
return 0;
min = max = prices[0];
for(int i = 1; i < pricesSize; i++){ // find the first min
if(min > prices[i]){
min = prices[i];
max = prices[i];
}
// then find the first max
if(max < prices[i]){
max = prices[i];
}
// get the max result
if(result_max < max - min)
result_max = max - min;
}
return result_max;
}

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).

得到可以赚到的最大值

int maxProfit(int* prices, int pricesSize) {
int min = 0;
int max = 0;
int sum = 0;
if(pricesSize == 0)
return 0;
min = max = prices[0];
// go over the array
for(int i = 1; i < pricesSize; i++){
// pass the prices less than front price
if(min > prices[i]){
min = prices[i];
max = prices[i];
}
// if we will have owner sell it and buy it again
if(max < prices[i]){
max = prices[i];
sum += max - min;
min = prices[i];
}
}
return sum;
}
  • 这里min和max的名字不太符合,追赶法更合适
  • 如果后面的值比前面的大就把差值加到sum上,然后重置min

Best Time to Buy and Sell Stock I && II的更多相关文章

  1. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  2. LeetCode:Best Time to Buy and Sell Stock I II III

    LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...

  3. [leetcode]_Best Time to Buy and Sell Stock I && II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  4. Best Time to Buy and Sell Stock I II III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  5. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  6. Best Time to Buy and Sell Stock I,II,III [leetcode]

    Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...

  7. 解题思路:best time to buy and sell stock i && ii && iii

    这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...

  8. LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV

    Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...

  9. 【数组】Best Time to Buy and Sell Stock I/II

    Best Time to Buy and Sell Stock I 题目: Say you have an array for which the ith element is the price o ...

  10. [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法

    题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...

随机推荐

  1. 不同SQL Server数据库之间的跨数据库查询

    --不同SQL Server数据库之间的跨数据库查询 EXEC sp_addlinkedserver @server=N'OldDatabase', --自己定义别名 @srvproduct=N'', ...

  2. IOS创建单例的两种方法

    1.0  苹果官方写法:  static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if ( ...

  3. OC——网络解析获取图片的应用

    headimageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DEVW, DEVW/2)]; headimageView.cont ...

  4. java 短信发送例子 2

    package com.google; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStr ...

  5. [Math]Divide Two Integers

    otal Accepted: 54356 Total Submissions: 357733 Difficulty: Medium Divide two integers without using ...

  6. ckeditor 使用手册

    CKEditor使用手册 在使用CKEditor过程中遇到了一些问题,现把它整理成手册,以便随时翻阅. 在页面<head>中引入ckeditor核心文件ckeditor.js <sc ...

  7. 关于采用MVC开发默认路由导致首页部分文件访问失效的临时解决方案

    最近开发中涉及了Mvc4的开发,其中的默认路由功能是很不错的东西,但是在实际应用中就出现了不少的问题.比如我们访问某网站http://www.abc.com,虽然路由会帮助自动转向Home/Index ...

  8. SQL Server 创建索引的 5 种方法

    前期准备: create table Employee (            ID int not null primary key,            Name nvarchar(4),  ...

  9. MYSQL <=>运算符

    <=> 与 =

  10. 类加载器子系统——JVM之四

    一.类加载器基本概念 顾名思义,类加载器(class loader)用来加载 Java 类到 Java 虚拟机中.一般来说,Java 虚拟机使用 Java 类的方式如下:Java 源程序(.java ...