- 浏览: 188452 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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.
动态规划问题,实际上就是让我们在一个数组中找到两个元素的差最大,较小的元素在较大元素的左边。用一个变量profit来维护当前的最小元素,用max来维护当前最大的利润。代码如下:
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.
动态规划问题,实际上就是让我们在一个数组中找到两个元素的差最大,较小的元素在较大元素的左边。用一个变量profit来维护当前的最小元素,用max来维护当前最大的利润。代码如下:
public class Solution { public int maxProfit(int[] prices) { int profit = 0; int max = 0; for(int i = 1; i < prices.length; i++) { profit += prices[i] - prices[i - 1]; if(profit < 0) profit = 0; if(profit > max) max = profit; } return max; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 288Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 293You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 413Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 396Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 521Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 600Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 503Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 696Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 495The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 450Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 611Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 625Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 451All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 924Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 950Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 626Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 715Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 897Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 809You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 750For a undirected graph with tre ...
相关推荐
同时,我们需要一个变量`buy_price`来存储最近一次买入的价格,如果当前价格低于`buy_price`,我们就更新`buy_price`;如果当前价格高于`buy_price`,我们就计算利润并更新`max_profit`。 ### 算法流程 1. 初始化`...
在深入探讨“java-leetcode题解之Best Time to Buy and Sell Stock III.java”这一文件内容之前,我们需要了解LeetCode平台及其题解的价值。LeetCode是一个专门面向技术面试的在线编程平台,它提供了一系列的编程...
本篇Java题解关注的是“Best Time to Buy and Sell Stock IV”(买卖股票的最佳时机 IV),这是该系列问题中的一个变体,涉及动态规划这一算法思想。 首先,需要明确问题的具体要求。在这个变体中,给定一个数组,...
在解决股票交易问题时,程序员们经常会遇到“Best Time to Buy and Sell Stock with Cooldown”这类问题。该问题属于动态规划的经典案例,通常被列为中等难度。在这类问题中,给定一个整数数组表示某只股票的价格,...
- 题目理解:Best Time to Buy and Sell Stock II这道题目的目的是寻找一个最佳的股票买卖时机,使得获得的利润最大化。 - 问题分析:此题是一道典型的动态规划问题,也可以使用贪心算法解决。 - 解题思路:贪心算法...
Best Time to Buy and Sell Stock I是LeetCode网站上的一道著名的编程题目,它属于动态规划的范畴。这道题目通常被称为“买卖股票的最佳时机”,主要是考察程序员对动态规划思想的理解和应用。在这个问题中,给定一...
在JavaScript中,LeetCode题目"Best Time to Buy and Sell Stock III"是一个典型的数据结构和算法问题,通常出现在编程面试和算法训练中。该问题的目标是在给定的股票价格数组中找出最大的利润,但是与简单买卖一次...
Best Time to Buy and Sell Stock 122 买卖股票的最佳时机 Ⅱ Best Time to Buy and Sell StockⅡ 123 买卖股票的最佳时机 Ⅲ Best Time to Buy and Sell StockⅢ 188 买卖股票的最佳时机Ⅳ Best Time to Buy and ...
在探讨python实现leetcode第122题"Best Time to Buy and Sell Stock II"的解决方案前,我们首先需要明确题目要求。第122题属于动态规划和贪心算法范畴,要求编写一个程序,通过分析给定的股票价格数组来找出最大的...
121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to Buy and Sell Stock IV...
- Best Time to Buy and Sell Stock IV:与前三者不同,可能需要多次买入和卖出。 - Best Time to Buy and Sell Stock with Cooldown:买卖后需要冷却一天。 - Best Time to Buy and Sell Stock with Transaction...
Best Time to Buy and Sell Stock (Easy) 一次交易,找最大收益 for i in prices: low = min(low, i) profit = max(profit, i-low) Best Time to Buy and Sell Stock II (Easy) 可多次交易,找总最大收益 for i in ...
python python_leetcode题解之123_Best_Time_to_Buy_and_Sell_Stock_III
63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必
第一个问题是“Best Time to Buy and Sell Stock”(121题),题目要求找到一次买入和卖出股票的最好时机,使得收益最大化。解决这个问题的一种有效方法是使用线性扫描,遍历数组找到一个局部最小值作为买入点,再...
7. **122.py** - 可能是LeetCode的122题,"Best Time to Buy and Sell Stock II"(买卖股票的最佳时机II),这是一道关于动态规划或贪心策略的题目,用于找出股票交易的最佳策略。 8. **156.py** - 这可能是...
- **股票买卖问题**:如LeetCode上的题目《Best Time to Buy and Sell Stock II》,通过贪心策略,每次遇到股价低于之前最低价时买入,遇到高于当前价格时卖出,可以实现利润的最大化。 - **零钱找零问题**:...
Best Time to Buy and Sell Stock"股票买卖问题,可以利用动态规划找到最佳买卖时机。 5. 排序与查找:排序算法如快速排序、归并排序,查找算法如二分查找,都在实际面试中经常出现。例如,"148. Sort List"要求对...
leetcode 分类 LeetCode LeetCode Java Solution 2018年5月31日 更新 最近刷了一遍 ...题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客: