Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc4e9249

Browse files
committed
update the problem description
1 parent8e851c8 commitc4e9249

File tree

4 files changed

+107
-40
lines changed

4 files changed

+107
-40
lines changed

‎algorithms/cpp/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.II.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,37 @@
22
// Author : Hao Chen
33
// Date : 2014-06-18
44

5-
/**********************************************************************************
6-
*
7-
* Say you have an array for which the ith element is the price of a given stock on day i.
8-
*
9-
* Design an algorithm to find the maximum profit. You may complete as many transactions
10-
* as you like (ie, buy one and sell one share of the stock multiple times). However,
11-
* you may not engage in multiple transactions at the same time (ie, you must sell the
12-
* stock before you buy again).
13-
*
14-
**********************************************************************************/
5+
/*****************************************************************************************************
6+
*
7+
* Say you have an array for which the ith element is the price of a given stock on day i.
8+
*
9+
* Design an algorithm to find the maximum profit. You may complete as many transactions as you like
10+
* (i.e., buy one and sell one share of the stock multiple times).
11+
*
12+
* Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock
13+
* before you buy again).
14+
*
15+
* Example 1:
16+
*
17+
* Input: [7,1,5,3,6,4]
18+
* Output: 7
19+
* Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
20+
* Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
21+
*
22+
* Example 2:
23+
*
24+
* Input: [1,2,3,4,5]
25+
* Output: 4
26+
* Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
27+
* Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
28+
* engaging multiple transactions at the same time. You must sell before buying again.
29+
*
30+
* Example 3:
31+
*
32+
* Input: [7,6,4,3,1]
33+
* Output: 0
34+
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
35+
******************************************************************************************************/
1536

1637
classSolution {
1738
public:

‎algorithms/cpp/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.III.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22
// Author : Hao Chen
33
// Date : 2014-08-22
44

5-
/**********************************************************************************
6-
*
7-
* Say you have an array for which the ith element is the price of a given stock on day i.
8-
*
9-
* Design an algorithm to find the maximum profit. You may complete at most two transactions.
10-
*
11-
* Note:
12-
* You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
13-
*
14-
**********************************************************************************/
5+
/*****************************************************************************************************
6+
*
7+
* Say you have an array for which the ith element is the price of a given stock on day i.
8+
*
9+
* Design an algorithm to find the maximum profit. You may complete at most two transactions.
10+
*
11+
* Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock
12+
* before you buy again).
13+
*
14+
* Example 1:
15+
*
16+
* Input: [3,3,5,0,0,3,1,4]
17+
* Output: 6
18+
* Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
19+
* Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
20+
*
21+
* Example 2:
22+
*
23+
* Input: [1,2,3,4,5]
24+
* Output: 4
25+
* Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
26+
* Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
27+
* engaging multiple transactions at the same time. You must sell before buying again.
28+
*
29+
* Example 3:
30+
*
31+
* Input: [7,6,4,3,1]
32+
* Output: 0
33+
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
34+
******************************************************************************************************/
1535

1636

1737
classSolution {

‎algorithms/cpp/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.IV.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22
// Author : Hao Chen
33
// Date : 2015-03-31
44

5-
/**********************************************************************************
6-
*
7-
* Say you have an array for which the ith element is the price of a given stock on day i.
8-
*
9-
* Design an algorithm to find the maximum profit. You may complete at most k transactions.
10-
*
11-
* Note:
12-
* You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
13-
*
14-
* Credits:Special thanks to @Freezen for adding this problem and creating all test cases.
15-
*
16-
**********************************************************************************/
5+
/*****************************************************************************************************
6+
*
7+
* Say you have an array for which the ith element is the price of a given stock on day i.
8+
*
9+
* Design an algorithm to find the maximum profit. You may complete at most k transactions.
10+
*
11+
* Note:
12+
* You may not engage in multiple transactions at the same time (ie, you must sell the stock before
13+
* you buy again).
14+
*
15+
* Example 1:
16+
*
17+
* Input: [2,4,1], k = 2
18+
* Output: 2
19+
* Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
20+
*
21+
* Example 2:
22+
*
23+
* Input: [3,2,6,5,0,3], k = 2
24+
* Output: 7
25+
* Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4.
26+
* Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
27+
******************************************************************************************************/
1728

1829
classSolution {
1930
public:

‎algorithms/cpp/bestTimeToBuyAndSellStock/bestTimeToBuyAndSellStock.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,29 @@
22
// Author : Hao Chen
33
// Date : 2014-06-18
44

5-
/**********************************************************************************
6-
*
7-
* Say you have an array for which the ith element is the price of a given stock on day i.
8-
*
9-
* If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),
10-
* design an algorithm to find the maximum profit.
11-
*
12-
**********************************************************************************/
5+
/*****************************************************************************************************
6+
*
7+
* Say you have an array for which the ith element is the price of a given stock on day i.
8+
*
9+
* If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of
10+
* the stock), design an algorithm to find the maximum profit.
11+
*
12+
* Note that you cannot sell a stock before you buy one.
13+
*
14+
* Example 1:
15+
*
16+
* Input: [7,1,5,3,6,4]
17+
* Output: 5
18+
* Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
19+
* Not 7-1 = 6, as selling price needs to be larger than buying price.
20+
*
21+
* Example 2:
22+
*
23+
* Input: [7,6,4,3,1]
24+
* Output: 0
25+
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
26+
*
27+
******************************************************************************************************/
1328

1429
classSolution {
1530
public:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp