Skip to content

Commit 21ab49b

Browse files
Merge branch 'DaleStudy:main' into main
2 parents 8365abe + 186d84c commit 21ab49b

File tree

25 files changed

+519
-0
lines changed

25 files changed

+519
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Brainstorm
3+
- Brute force: double for-loop O(N^2)
4+
- Efficient: single pass O(N)
5+
6+
Plan
7+
1. Initialize the variables
8+
2. Iterate through each price
9+
2-1. Update the min price
10+
2-2. Calculate and update max profit
11+
3. Return the max profit
12+
'''
13+
14+
15+
def maxProfit(prices):
16+
min_price = float('inf')
17+
max_profit = 0
18+
for price in prices:
19+
if price < min_price:
20+
min_price = price
21+
elif price - min_price > max_profit:
22+
max_profit = price - min_price
23+
return max_profit
24+
25+
#TC: O(N), SC: O(1)
26+
27+
# Normal Case
28+
print(maxProfit([7, 1, 5, 3, 6, 4]) == 5)
29+
print(maxProfit([7, 6, 4, 3, 1]) == 0)
30+
31+
# Edge Case
32+
print(maxProfit([]) == 0)
33+
print(maxProfit([1]) == 0)
34+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Solution
3+
4+
Algorithm:
5+
1. Iterate through the list in reverse.
6+
2. Keep track of the maximum value seen so far.
7+
3. Calculate the profit by subtracting the current value from the maximum value.
8+
4. Update the profit if it is greater than the current profit.
9+
10+
Time complexity: O(n)
11+
Space complexity: O(1)
12+
"""
13+
14+
15+
from typing import List
16+
17+
18+
class Solution:
19+
def maxProfit(self, prices: List[int]) -> int:
20+
profit = 0
21+
prev_max = 0
22+
23+
for i in reversed(range(len(prices) - 1)):
24+
prev_max = max(prev_max, prices[i + 1])
25+
profit = max(profit, prev_max - prices[i])
26+
27+
return profit
28+
29+
30+
def main():
31+
test_cases = [[[7, 1, 5, 3, 6, 4], 5], [[7, 6, 4, 3, 1], 0]]
32+
s = Solution()
33+
34+
for test_case in test_cases:
35+
prices_input, expected = test_case
36+
assert s.maxProfit(prices_input) == expected
37+
38+
39+
if __name__ == "__main__":
40+
main()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func maxProfit(prices []int) int {
2+
purchasePrice := prices[0]
3+
maxBenefit := 0
4+
5+
for _, price := range prices {
6+
purchasePrice = min(purchasePrice, price)
7+
maxBenefit = max(maxBenefit, price-purchasePrice)
8+
}
9+
return maxBenefit
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function maxProfit(prices: number[]): number {
2+
let minPrice = Number.MAX_SAFE_INTEGER;
3+
let maxProfit = 0;
4+
5+
for (let i = 0; i < prices.length; i++) {
6+
if (prices[i] < minPrice) {
7+
minPrice = prices[i];
8+
}
9+
10+
const potentialProfit = prices[i] - minPrice;
11+
12+
if (maxProfit < potentialProfit) {
13+
maxProfit = potentialProfit;
14+
}
15+
}
16+
17+
return maxProfit;
18+
}

โ€Žcontains-duplicate/JoyJaewon.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Brainstrom
3+
- brute force: double for-loop (O^2)
4+
- efficient approach: use set O(N)
5+
6+
Plan
7+
1. Initialize the set
8+
2. Iterate over the array
9+
2-1. For each element, check if it's in the set. If it is, return True
10+
2-2. If not, add the num to the set
11+
3. Return False since no duplicates are found
12+
'''
13+
14+
def containsDuplicate(nums):
15+
num_set=set()
16+
for num in nums:
17+
if num in num_set:
18+
return True
19+
num_set.add(num)
20+
return False
21+
22+
#TC: O(N), SC: O(N)
23+
24+
#Normal case
25+
print(containsDuplicate([1, 2, 3, 1]) == True)
26+
print(containsDuplicate([1, 2, 3, 4]) == False)
27+
28+
#Edge case
29+
print(containsDuplicate([1]) == False)
30+
print(containsDuplicate([]) == False)
31+

โ€Žcontains-duplicate/bhyun-kim.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Solution
3+
4+
Algorithm:
5+
1. Create a set from the list.
6+
2. If the length of the set is not equal to the length of the list, return True.
7+
3. Otherwise, return False.
8+
9+
Time complexity: O(n)
10+
Space complexity: O(n)
11+
"""
12+
13+
14+
from typing import List
15+
16+
17+
class Solution:
18+
def containsDuplicate(self, nums: List[int]) -> bool:
19+
return len(set(nums)) != len(nums)
20+
21+
22+
def main():
23+
test_cases = [
24+
[
25+
[1, 2, 3, 1],
26+
True,
27+
][[1, 2, 3, 4], False],
28+
[[1, 1, 1, 3, 3, 4, 3, 2, 4, 2], True],
29+
]
30+
s = Solution()
31+
32+
for test_case in test_cases:
33+
nums_input, expected = test_case
34+
assert s.containsDuplicate(nums_input) == expected
35+
36+
37+
if __name__ == "__main__":
38+
main()

โ€Žcontains-duplicate/invidam.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func containsDuplicate(nums []int) bool {
2+
appeared := make(map[int]bool)
3+
4+
for _, num := range nums {
5+
appeared[num] = true
6+
}
7+
8+
return len(appeared) != len(nums)
9+
}

โ€Žcontains-duplicate/sounmind.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function containsDuplicate(nums: number[]): boolean {
2+
return nums.length !== new Set(nums).size;
3+
}

โ€Žinvert-binary-tree/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/invert-binary-tree/
2+
- ํ’€์ด: https://www.algodale.com/problems/invert-binary-tree/

โ€Žlinked-list-cycle/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/linked-list-cycle/
2+
- ํ’€์ด: https://www.algodale.com/problems/linked-list-cycle/

0 commit comments

Comments
ย (0)