Today, I studied the best time to buy and sell stock algorithms problem.
Description:
Solution:
This code is not nested loop, so it is not gonna be O(N^2). Just O(N) time complexity. What is the difference from previous brute force algorithms? I would like to say we store two array while process in loop. We stored two value called profit and minimum price.
ex) case 1:
price: [7, 1, 5, 3, 6, 4]
profit: [0, -6, 4, 2, 5, 3]
min_price:[7, 1, 1, 1, 1, 1]
Time complexity is O(N)
Space complexity is O(1)
'LeetCode ๐๏ธ > Array' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
152. Maximum Product Subarray (0) | 2023.05.28 |
---|---|
53. Maximum Subarray (0) | 2023.05.27 |
238. Product of Array Except Self (0) | 2023.01.29 |
217. Contains Duplicate (0) | 2023.01.28 |
1. Two Sum (0) | 2023.01.24 |