Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List


class Solution:
def maxProfit(self, prices: List[int]) -> int:
buy1 = float('-inf')
sell1 = 0
buy2 = float('-inf')
sell2 = 0

for price in prices:
buy1 = max(buy1, -price)
sell1 = max(sell1, buy1 + price)
buy2 = max(buy2, sell1 - price)
sell2 = max(sell2, buy2 + price)

return sell2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function maxProfit(prices: number[]): number {
let buy1 = -Infinity;
let sell1 = 0;
let buy2 = -Infinity;
let sell2 = 0;

for (const price of prices) {
buy1 = Math.max(buy1, -price);
sell1 = Math.max(sell1, buy1 + price);
buy2 = Math.max(buy2, sell1 - price);
sell2 = Math.max(sell2, buy2 + price);
}

return sell2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_143_best_time_to_buy_stock_iii import Solution


class BestTimeToBuyStockIIITestCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_example_1(self):
self.assertEqual(self.solution.maxProfit([3, 3, 5, 0, 0, 3, 1, 4]), 6)

def test_example_2(self):
self.assertEqual(self.solution.maxProfit([1, 2, 3, 4, 5]), 4)

def test_example_3(self):
self.assertEqual(self.solution.maxProfit([7, 6, 4, 3, 1]), 0)
Loading