From 477705924e3f87bd3661e20130662040774d65aa Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 6 May 2026 04:49:41 -0600 Subject: [PATCH 1/2] adding algo --- .../ex_08_best_time_to_sell_stocks_ii.py | 25 +++++++++++++++++++ .../ex_08_best_time_to_sell_stocks_ii.ts | 23 +++++++++++++++++ ...08_best_time_to_sell_stocks_ii_round_22.py | 13 ++++++++++ 3 files changed, 61 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_23/ex_08_best_time_to_sell_stocks_ii.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_23/ex_08_best_time_to_sell_stocks_ii.ts create mode 100644 tests/test_150_questions_round_23/test_08_best_time_to_sell_stocks_ii_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_23/ex_08_best_time_to_sell_stocks_ii.py b/src/my_project/interviews/top_150_questions_round_23/ex_08_best_time_to_sell_stocks_ii.py new file mode 100644 index 00000000..0de5446b --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_23/ex_08_best_time_to_sell_stocks_ii.py @@ -0,0 +1,25 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + + i = 0 + peak = None + valley = None + len_prices = len(prices) + result = 0 + + while i < len(prices) - 1: + + while i < len_prices - 1 and prices[i] >= prices[i+1]: + i += 1 + valley = prices[i] + + while i < len_prices - 1 and prices[i] <= prices[i+1]: + i += 1 + peak = prices[i] + + result += peak - valley + + return result diff --git a/src/my_project/interviews_typescript/top_150_questions_round_23/ex_08_best_time_to_sell_stocks_ii.ts b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_08_best_time_to_sell_stocks_ii.ts new file mode 100644 index 00000000..60e507e5 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_23/ex_08_best_time_to_sell_stocks_ii.ts @@ -0,0 +1,23 @@ +function maxProfit(prices: number[]): number { + let i = 0; + let peak = 0; + let valley = 0; + const lenPrices = prices.length; + let result = 0; + + while (i < lenPrices - 1) { + while (i < lenPrices - 1 && prices[i] >= prices[i + 1]) { + i++; + } + valley = prices[i]; + + while (i < lenPrices - 1 && prices[i] <= prices[i + 1]) { + i++; + } + peak = prices[i]; + + result += peak - valley; + } + + return result; +}; diff --git a/tests/test_150_questions_round_23/test_08_best_time_to_sell_stocks_ii_round_22.py b/tests/test_150_questions_round_23/test_08_best_time_to_sell_stocks_ii_round_22.py new file mode 100644 index 00000000..dbd3a8dc --- /dev/null +++ b/tests/test_150_questions_round_23/test_08_best_time_to_sell_stocks_ii_round_22.py @@ -0,0 +1,13 @@ +import unittest +import unittest +from src.my_project.interviews.top_150_questions_round_23\ +.ex_08_best_time_to_sell_stocks_ii import Solution + + +class BestTimeToSellStockTestCase(unittest.TestCase): + + def test_best_time_to_sell_stock(self): + solution = Solution() + output = solution.maxProfit(prices=[7,1,5,3,6,4]) + target = 7 + self.assertEqual(output, target) \ No newline at end of file From 46a759bab4b6e2b103b53069b61409e8c452d246 Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 6 May 2026 04:51:09 -0600 Subject: [PATCH 2/2] adding algo --- ...ound_22.py => test_08_best_time_to_sell_stocks_ii_round_23.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/test_150_questions_round_23/{test_08_best_time_to_sell_stocks_ii_round_22.py => test_08_best_time_to_sell_stocks_ii_round_23.py} (100%) diff --git a/tests/test_150_questions_round_23/test_08_best_time_to_sell_stocks_ii_round_22.py b/tests/test_150_questions_round_23/test_08_best_time_to_sell_stocks_ii_round_23.py similarity index 100% rename from tests/test_150_questions_round_23/test_08_best_time_to_sell_stocks_ii_round_22.py rename to tests/test_150_questions_round_23/test_08_best_time_to_sell_stocks_ii_round_23.py