Skip to content

Commit fdff435

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 37934dd + bee65e5 commit fdff435

63 files changed

Lines changed: 2715 additions & 97 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
3+
4+
5+
6+
7+
//O(n)์— ์ฆ๊ฐ€ ํ•œ ๊ฒฝ์šฐ profitMax๋ฅผ ๊ฐฑ์‹ ์‹œํ‚ค๋ฉฐ ํŒ๋‹จ -> time limit
8+
public int maxProfit(int[] prices) {
9+
int profitMax = 0;
10+
/*
11+
for(int i=0; i< prices.length - 1; i++){
12+
int buy= prices[i];
13+
14+
15+
for(int d =i+1; d < prices.length; d++){
16+
int sellCandid = prices[d];
17+
if (sellCandid - buy > profitMax){
18+
profitMax = sellCandid - buy;
19+
}
20+
21+
}
22+
}//end of for
23+
24+
*/
25+
26+
int priceMin = Integer.MAX_VALUE;
27+
for(int i =0; i < prices.length; i++){
28+
if (prices[i] < priceMin){
29+
priceMin = prices[i];
30+
}
31+
else{
32+
profitMax = Math.max(profitMax, prices[i] - priceMin);
33+
}
34+
}
35+
36+
return profitMax;
37+
38+
}
39+
40+
}
41+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
// TC : O(n)
3+
// SC : O(1)
4+
public int maxProfit(int[] prices) {
5+
int maximumProfit = 0;
6+
int cursor = 0;
7+
8+
// ์ „์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ด์ต์„ ๊ฒ€์ฆ
9+
for(int i = 0; i < prices.length; i++){
10+
int profit = prices[i] - prices[cursor];
11+
12+
// ๋งŒ์•ฝ ํ˜„์žฌ ๊ฐ’์ด ์ปค์„œ์˜ ๊ฐ’๋ณด๋‹ค ๋” ์ž‘๋‹ค๋ฉด ํ˜„์žฌ ๊ฐ’๋ถ€ํ„ฐ ๋น„๊ตํ•˜๊ธฐ ์‹œ์ž‘
13+
if(profit < 0){
14+
cursor = i;
15+
continue;
16+
}
17+
18+
// ์ด์ „์— ์ปค์„œ๋กœ ์ถ”์ •ํ•œ ๊ฐ’์ด ๋†’๋”๋ผ๋„ Math.max๋กœ ๋ณด์กด๋จ
19+
maximumProfit = Math.max(profit, maximumProfit);
20+
}
21+
22+
return maximumProfit;
23+
}
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
[๊ฒฐ๊ณผ ์š”์•ฝ]
3+
# ์‹œ๋„ํ•œ ๋กœ์ง ์ˆ˜: 2
4+
1. if/else ๋ฌธ์œผ๋กœ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ(์‹œ๊ฐ„: O(n) / ๊ณต๊ฐ„: O(1))
5+
-
6+
2. if๋ฌธ ๋Œ€์‹  min / max๋ฅผ ๊ณ„์† ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์‹
7+
- None ๋Œ€์‹  float(inf)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ None ๊ฒ€์ฆ ๋ถ„๊ธฐ ์ œ๊ฑฐ
8+
- inf ์‚ฌ์šฉ์„ ์œ„ํ•ด์„œ ๋งˆ์ง€๋ง‰ return์— int() ํƒ€์ž… ๋ณ€ํ™˜ ํ•„์š”
9+
- ์„ฑ๋Šฅ์€ 1๊ณผ ํฐ ์ฐจ์ด ์—†์œผ๋ฉฐ ์ฝ”๋“œ๊ฐ€ ๊ฐ„์†Œํ™”
10+
"""
11+
12+
13+
class Solution:
14+
def maxProfit(self, prices: list[int]) -> int:
15+
max_profit = 0
16+
min_price = float("inf")
17+
18+
for p in prices:
19+
min_price = min(min_price, p)
20+
max_profit = max(max_profit, p - min_price)
21+
22+
return int(max_profit)
23+
24+
25+
if __name__ == "__main__":
26+
test_cases = [
27+
([7, 1, 5, 3, 6, 4], 5),
28+
([7, 6, 4, 3, 1], 0),
29+
([2, 4, 1], 2),
30+
([1, 2, 4, 10000, 2, 1, 3], 9999),
31+
]
32+
33+
solution = Solution()
34+
for idx, case_ in enumerate(test_cases):
35+
prices, answer = case_
36+
result = solution.maxProfit(prices)
37+
assert (
38+
answer == result
39+
), f"Test Case {idx} Failed: Expected {answer}, Got {result}"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function maxProfit(prices: number[]): number {
2+
let minPrice = Number.MAX_SAFE_INTEGER;
3+
let maxProfit = 0;
4+
5+
for (let p of prices) {
6+
if (minPrice >= p) {
7+
minPrice = p;
8+
continue;
9+
} else {
10+
let profit = p - minPrice;
11+
if (profit > maxProfit) maxProfit = profit;
12+
}
13+
}
14+
return maxProfit;
15+
}
16+
17+
function maxProfit(prices: number[]): number {
18+
let minPrice = Infinity;
19+
let maxProfit = 0;
20+
21+
for (let p of prices) {
22+
minPrice = Math.min(minPrice, p);
23+
maxProfit = Math.max(maxProfit, p - minPrice);
24+
}
25+
return maxProfit;
26+
}

โ€Žbest-time-to-buy-and-sell-stock/robinyoon-dev.jsโ€Ž

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ var maxProfit = function (prices) {
2323
return maxProfit;
2424
};
2525

26-
2726
// -----์•„๋ž˜๋Š” ์ด์ „์— ์ž‘์„ฑํ•œ ๋‹ต์•ˆ์ž…๋‹ˆ๋‹ค.
2827
// /**
2928
// * @param {number[]} prices
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# TC : O(n)
2+
# SC : O(1)
3+
4+
class Solution:
5+
def maxProfit(self, prices: List[int]) -> int:
6+
answer = 0
7+
mm = 10001
8+
9+
for price in prices:
10+
mm = min(mm, price)
11+
answer = max(answer, price - mm)
12+
13+
return answer
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
// 3rd tried
12
function maxProfit(prices: number[]): number {
2-
let minPrice = Infinity;
3-
let maxProfit = 0;
4-
for (let i = 0; i < prices.length; i++) {
5-
if (prices[i] < minPrice) {
6-
minPrice = prices[i];
7-
}
3+
let left = 0;
4+
let right = 1;
5+
let max = 0;
86

9-
if (prices[i] - minPrice > maxProfit) {
10-
maxProfit = prices[i] - minPrice;
11-
}
7+
while(left < right && right < prices.length) {
8+
if(prices[right] - prices[left] > 0) {
9+
max = Math.max(max, prices[right] - prices[left])
10+
} else {
11+
left = right;
12+
}
13+
right++;
1214
}
13-
return maxProfit;
14-
}
15+
return max;
16+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// tc: O(n);
2+
// sc: O(1);
3+
const maxArea = function (height) {
4+
let max = 0;
5+
let leftIdx = 0;
6+
let rightIdx = height.length - 1;
7+
8+
while (leftIdx < rightIdx) {
9+
const width = rightIdx - leftIdx;
10+
const minHeight = Math.min(height[leftIdx], height[rightIdx]);
11+
const area = width * minHeight;
12+
max = Math.max(max, area);
13+
14+
if (height[leftIdx] > height[rightIdx]) {
15+
rightIdx -= 1;
16+
} else {
17+
leftIdx += 1;
18+
}
19+
}
20+
21+
return max;
22+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
impl Solution {
4+
pub fn max_area(height: Vec<i32>) -> i32 {
5+
let mut max_area = 0;
6+
let (mut left, mut right) = (0, height.len() - 1);
7+
while left < right {
8+
let width = (right - left) as i32;
9+
let min_height = height[left].min(height[right]);
10+
let area = width * min_height;
11+
max_area = max_area.max(area);
12+
if height[left] < height[right] {
13+
left += 1;
14+
} else {
15+
right -= 1;
16+
}
17+
}
18+
max_area
19+
}
20+
}

โ€Žcontainer-with-most-water/gcount85.pyโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
# Approach
33
ํˆฌํฌ์ธํ„ฐ๋กœ ์–‘ ๋์—์„œ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค.
4-
๋” ๋‚ฎ์€ height์ธ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ€๋Š”๋ฐ, ๋†’์ด๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์–‘์ชฝ ๋ชจ๋‘ ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ž…๋‹ˆ๋‹ค.
4+
๋” ๋‚ฎ์€ height์ธ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ€๋Š”๋ฐ, ๋†’์ด๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์™ผ์ชฝ ํฌ์ธํ„ฐ๋งŒ ์›€์ง์ž…๋‹ˆ๋‹ค.
55
66
# Complexity
77
height์˜ ๊ธธ์ด๊ฐ€ N์ผ ๋•Œ

0 commit comments

Comments
ย (0)