From c37bf43bb456b6a29f18320e59547ea06301e909 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 26 Apr 2026 04:49:14 -0600 Subject: [PATCH] adding updates --- .../ex_143_best_time_to_buy_stock_iii.py | 17 +++++++++++++++++ .../ex_143_best_time_to_buy_stock_iii.ts | 15 +++++++++++++++ ...t_143_best_time_to_buy_stock_iii_round_22.py | 17 +++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_143_best_time_to_buy_stock_iii.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_143_best_time_to_buy_stock_iii.ts create mode 100644 tests/test_150_questions_round_22/test_143_best_time_to_buy_stock_iii_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_143_best_time_to_buy_stock_iii.py b/src/my_project/interviews/top_150_questions_round_22/ex_143_best_time_to_buy_stock_iii.py new file mode 100644 index 00000000..90d8f1e7 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_143_best_time_to_buy_stock_iii.py @@ -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 diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_143_best_time_to_buy_stock_iii.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_143_best_time_to_buy_stock_iii.ts new file mode 100644 index 00000000..5b2a5e4e --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_143_best_time_to_buy_stock_iii.ts @@ -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; +} diff --git a/tests/test_150_questions_round_22/test_143_best_time_to_buy_stock_iii_round_22.py b/tests/test_150_questions_round_22/test_143_best_time_to_buy_stock_iii_round_22.py new file mode 100644 index 00000000..21ab7913 --- /dev/null +++ b/tests/test_150_questions_round_22/test_143_best_time_to_buy_stock_iii_round_22.py @@ -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)