Best Time to Buy and Sell Stock
来源:互联网 发布:方正数据库免费入口 编辑:程序博客网 时间:2024/04/29 21:20
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.
#include<stdio.h>int maxProfit(int *prices, int n) { int i = 0; int profit = 0, min = prices[0]; if(n == 0) return 0; for(i = 1; i < n; i++){ if(prices[i] - min > profit) profit = prices[i] - min; if(prices[i] < min) min = prices[i]; } return profit;}void main() { int prices[] = {7,6,5,4,3,2,1,0}; int n = 8; printf("%d\n", maxProfit(prices, n));}
0 0
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- MEF:SDN/NFV支撑下的第三类网络愿景
- 第五章:创建和转换场景
- Android Dialog用法(转载)
- 一款新闻类iOS APP的诞生过程(初学)
- UNIX环境高级编程-第11章- 线程 - 一
- Best Time to Buy and Sell Stock
- Tiled
- Android(java方法)上实现mp4的分割和拼接 (全)
- poj2485
- C++多字节字符与宽字节字符相互转换
- 不该那么任性,还是用用data,分分GET和POST
- 两年的心路历程
- EJB到底是什么,
- 为什么静态成员不能访问非静态成员