Skip to content

Commit 5c74ff7

Browse files
committed
finish Buy and Sell Stock questions
1 parent 0c07b06 commit 5c74ff7

17 files changed

+494
-168
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-08-23 09:54:56
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
If you sum the ith bit of all numbers and mod 3,
10+
it must be either 0 or 1 due to the constraint of this problem
11+
where each number must appear either three times or once.
12+
This will be the ith bit of that "single number".
13+
*/
14+
int singleNumber(vector<int>& nums) {
15+
int bits[32]={0};
16+
int ret = 0;
17+
for(int i=0; i<32; i++){
18+
for(auto n: nums){
19+
bits[i] += (n >> i) & 0x1;
20+
}
21+
bits[i] %= 3;
22+
ret |= bits[i] << i;
23+
}
24+
return ret;
25+
}
26+
};
27+
28+
class Solution_2 {
29+
public:
30+
/*
31+
Use two-bits represents the sum(should be 0/3, 1, 2) of all num's i-th bit.
32+
Twice-Once(the two bits): 00(0, 3)-->01(1)-->10(2)-->00(0, 3)
33+
Then we need to set rules for 'once' and 'twice' so that they act as we hopes.
34+
once = once ^ n & (~twice)
35+
twice = twice ^ n & (~once)
36+
37+
Since each of the 32 bits follow the same rules,
38+
we can calculate them all at once.
39+
*/
40+
int singleNumber(vector<int>& nums) {
41+
int once=0, twice=0;
42+
for(auto n: nums){
43+
once = (once ^ n) & (~twice);
44+
twice = (twice ^ n) & (~once);
45+
}
46+
return once;
47+
}
48+
};
49+
50+
/*
51+
[1]
52+
[1,1,3,1]
53+
[1,1,1,2,2,2,3,4,4,4]
54+
[-2,-2,1,1,-3,1,-3,-3,-4,-2]
55+
*/

BitManipulation/137_SingleNumberII.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
#! /usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
# Refer to https://leetcode.com/discuss/857/constant-space-solution
3+
4+
# @Last Modified time: 2016-08-23 09:44:33
45

56

67
class Solution(object):
8+
"""
9+
If you sum the ith bit of all numbers and mod 3,
10+
it must be either 0 or 1 due to the constraint of this problem
11+
where each number must appear either three times or once.
12+
This will be the ith bit of that "single number".
13+
14+
Refer to:
15+
https://discuss.leetcode.com/topic/455/constant-space-solution
16+
"""
717
def singleNumber(self, nums):
8-
once = 0
9-
twice = 0
10-
third = 0
11-
for num in nums:
12-
twice |= once & num
13-
once ^= num
14-
third = once & twice
15-
once &= ~third
16-
twice &= ~third
18+
bit_record = [0] * 32
19+
result = 0
20+
for i in range(32):
21+
for n in nums:
22+
bit_record[i] += (n >> i) & 0x1
23+
bit_val = bit_record[i] % 3
24+
result |= bit_val << i
25+
26+
# Int in python is an object and has no upper limit,
27+
# If you do 1<<31, you get 2147483648 other than -2147483648
28+
return result - 2**32 if result >= 2**31 else result
29+
1730

31+
class Solution_2(object):
32+
"""
33+
Use two-bits represents the sum(should be 0/3, 1, 2) of all num's i-th bit.
34+
Twice-Once(the two bits): 00(0, 3)-->01(1)-->10(2)-->00(0, 3)
35+
Then we need to set rules for 'once' and 'twice' so that they act as we hopes.
36+
once = once ^ n & (~twice)
37+
twice = twice ^ n & (~once)
38+
39+
Since each of the 32 bits follow the same rules,
40+
we can calculate them all at once. Refer to:
41+
https://discuss.leetcode.com/topic/2031/challenge-me-thx/17
42+
"""
43+
def singleNumber(self, nums):
44+
once, twice = 0, 0
45+
for n in nums:
46+
once = once ^ n & (~twice)
47+
twice = twice ^ n & (~once)
1848
return once
1949

50+
2051
"""
2152
[1]
53+
[1,1,3,1]
2254
[1,1,1,2,2,2,3,4,4,4]
55+
[-2,-2,1,1,-3,1,-3,-3,-4,-2]
2356
"""
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-08-23 11:57:19
4+
*/
5+
6+
class Solution {
7+
public:
8+
/*
9+
Same as "max subarray problem" using Kadane's Algorithm
10+
11+
Just need one scan through the array values,
12+
computing at each position the max profit ending at that position.
13+
14+
https://en.wikipedia.org/wiki/Maximum_subarray_problem
15+
*/
16+
int maxProfit(vector<int>& prices) {
17+
if(prices.size() == 0) return 0;
18+
int min_buy = prices[0], max_pro = 0;
19+
for(int i=1; i<prices.size(); i++){
20+
int cur_price = prices[i];
21+
min_buy = (min_buy <= cur_price ? min_buy : cur_price);
22+
max_pro = (max_pro >= cur_price - min_buy ? max_pro :cur_price-min_buy);
23+
}
24+
return max_pro;
25+
}
26+
};
27+
28+
class Solution_2{
29+
public:
30+
/*
31+
sell_1: The maximum if we've just sold 1nd stock so far.
32+
buy_1: The maximum if we've just buy 1st stock so far.
33+
34+
Then we can update sell_1 if p + buy_1(sell now) get more than pre-sell_1.
35+
And update buy_1 if remain more money when we buy now than pre-buy_1.
36+
Here update sell_1 before buy_1 because we need to use pre_buy_1 to get sell_1.
37+
*/
38+
int maxProfit(vector<int>& prices) {
39+
int sell_1 = 0, buy_1 = INT_MIN;
40+
for(int p: prices){
41+
sell_1 = max(sell_1, p+buy_1);
42+
buy_1 = max(buy_1, -p);
43+
}
44+
return sell_1;
45+
}
46+
};
47+
48+
/*
49+
[]
50+
[3,4,5,6,2,4]
51+
[6,5,4,3,2,1]
52+
[1,2,3,4,3,2,1,9,11,2,20]
53+
*/

DynamicProgramming/121_BestTimeToBuyAndSellStock.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,40 @@
11
#! /usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
# According to: http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html
43

54

65
class Solution(object):
6+
""" Same as "max subarray problem" using Kadane's Algorithm.
7+
8+
Just need one scan through the array values,
9+
computing at each position the max profit ending at that position.
10+
https://en.wikipedia.org/wiki/Maximum_subarray_problem
11+
"""
712
def maxProfit(self, prices):
813
if not prices:
914
return 0
1015
max_profit = 0
1116
min_buy = prices[0]
1217
for price in prices:
1318
min_buy = min(price, min_buy)
14-
max_profit = max(price-min_buy, max_profit)
19+
max_profit = max(price - min_buy, max_profit)
1520
return max_profit
16-
"""
17-
# Not readable.
18-
class Solution(object):
19-
def maxProfit(self, prices):
20-
if not prices:
21-
return 0
22-
buy_price = prices[0]
23-
sell_price = buy_price
24-
max_profit = 0
25-
days = len(prices)
26-
ith_day = 1
27-
while ith_day < days:
28-
# The current price is higher than the cost we buy,
29-
# so just keep the stock, until the price is lower.
30-
if prices[ith_day] >= buy_price:
31-
# make the price that we sell the stock be biggest
32-
if prices[ith_day] > sell_price:
33-
sell_price = prices[ith_day]
34-
else:
35-
pass
36-
# Meet a new lower price, so buy the stock
37-
else:
38-
max_profit = max(max_profit, sell_price - buy_price)
39-
buy_price = prices[ith_day]
40-
sell_price = buy_price
4121

42-
ith_day += 1
43-
# The price may never be lower than the first day.
44-
max_profit = max(max_profit, sell_price - buy_price)
45-
return max_profit
46-
"""
22+
23+
class Solution_2(object):
24+
"""
25+
sell_1: The maximum if we've just sold 1nd stock so far.
26+
buy_1: The maximum if we've just buy 1st stock so far.
27+
28+
Then we can update sell_1 if p + buy_1(sell now) get more than pre-sell_1.
29+
And update buy_1 if remain more money when we buy now than pre-buy_1.
30+
Here update sell_1 before buy_1 because we need to use pre_buy_1 to get sell_1.
31+
"""
32+
def maxProfit(self, prices):
33+
sell_1, buy_1 = 0, -2**31
34+
for p in prices:
35+
sell_1 = max(sell_1, p + buy_1)
36+
buy_1 = max(buy_1, -p)
37+
return sell_1
4738

4839
"""
4940
[]

DynamicProgramming/123_BestTimeToBuyAndSellStockIII.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
2-
* @Author: xuelangZF
3-
* @Date: 2016-03-09 14:10:44
4-
* @Last Modified by: xuelangZF
5-
* @Last Modified time: 2016-03-09 14:10:44
6-
*/
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-08-23 10:53:01
4+
*/
5+
76
class Solution {
87
public:
98
int maxProfit(vector<int>& prices) {
@@ -33,3 +32,37 @@ class Solution {
3332
return max_profit;
3433
}
3534
};
35+
36+
class Solution_2 {
37+
public:
38+
/*
39+
Assume we only have 0 money at first, Then
40+
sell_2: The maximum if we've just sold 2nd stock so far.
41+
buy_2: The maximum if we've just buy 2nd stock so far.
42+
sell_1: The maximum if we've just sold 1nd stock so far.
43+
buy_1: The maximum if we've just buy 1st stock so far.
44+
Refer to:
45+
https://discuss.leetcode.com/topic/5934/is-it-best-solution-with-o-n-o-1
46+
*/
47+
int maxProfit(vector<int>& prices) {
48+
int buy_1 = INT_MIN, buy_2 = INT_MIN;
49+
int sell_1 = 0, sell_2 = 0;
50+
for(int p: prices){
51+
sell_2 = max(sell_2, p+buy_2);
52+
buy_2 = max(buy_2, sell_1-p);
53+
sell_1 = max(sell_1, p+buy_1);
54+
buy_1 = max(buy_1, -p);
55+
}
56+
return sell_2;
57+
}
58+
};
59+
60+
/*
61+
[]
62+
[1,2]
63+
[1,3,5]
64+
[2,8,3,9]
65+
[2,8,3,9,1,2]
66+
[2,8,3,9,1,9]
67+
[6,5,4,3,2,1]
68+
*/

DynamicProgramming/123_BestTimeToBuyAndSellStockIII.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
#! /usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
# According to:
4-
# http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html
53

64

75
class Solution(object):
86
def maxProfit(self, prices):
9-
"""
10-
:type prices: List[int]
11-
:rtype: int
12-
"""
137
if not prices:
148
return 0
159
days_count = len(prices)
@@ -20,13 +14,13 @@ def maxProfit(self, prices):
2014
min_buy = prices[0]
2115
for i in range(1, days_count):
2216
min_buy = min(min_buy, prices[i])
23-
pre_profit[i] = max(pre_profit[i-1], prices[i]-min_buy)
17+
pre_profit[i] = max(pre_profit[i - 1], prices[i] - min_buy)
2418

2519
# Get max profit when buy and sell Stock only once in post (n-i) days.
2620
max_sell = prices[-1]
27-
for j in range(days_count-2, -1, -1):
21+
for j in range(days_count - 2, -1, -1):
2822
max_sell = max(max_sell, prices[j])
29-
post_profit[j] = max(post_profit[j+1], max_sell-prices[j])
23+
post_profit[j] = max(post_profit[j + 1], max_sell - prices[j])
3024

3125
# Find the max profit when buy and sell Stock only twice.
3226
# First in the pre kth day, and second in the post (n-k) days
@@ -35,6 +29,28 @@ def maxProfit(self, prices):
3529
max_profit = max(max_profit, pre_profit[i] + post_profit[i])
3630
return max_profit
3731

32+
33+
class Solution_2(object):
34+
"""
35+
Assume we only have 0 money at first, Then
36+
sell_2: The maximum if we've just sold 2nd stock so far.
37+
buy_2: The maximum if we've just buy 2nd stock so far.
38+
sell_1: The maximum if we've just sold 1nd stock so far.
39+
buy_1: The maximum if we've just buy 1st stock so far.
40+
41+
Refer to:
42+
https://discuss.leetcode.com/topic/5934/is-it-best-solution-with-o-n-o-1
43+
"""
44+
def maxProfit(self, prices):
45+
buy_1, buy_2 = -2**31, -2**31
46+
sell_1, sell_2 = 0, 0
47+
for p in prices:
48+
sell_2 = max(sell_2, p + buy_2)
49+
buy_2 = max(buy_2, sell_1 - p)
50+
sell_1 = max(sell_1, p + buy_1)
51+
buy_1 = max(buy_1, -p)
52+
return sell_2
53+
3854
"""
3955
[]
4056
[1,2]

0 commit comments

Comments
 (0)