Skip to content
Merged

May06 #1629

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,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
Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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)
Loading