Skip to content

Commit 66d005a

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents e421bbb + 767c2f3 commit 66d005a

File tree

200 files changed

+7597
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+7597
-6
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Solution: 1) 2중 포문을 돌면서 max값을 구한다.
3+
Time: O(n^2)
4+
Space: O(1)
5+
6+
Time Limit Exceeded
7+
8+
"""
9+
10+
11+
class Solution:
12+
def maxProfit(self, prices: List[int]) -> int:
13+
result = 0
14+
for l in range(len(prices) - 1):
15+
for r in range(l + 1, len(prices)):
16+
result = max(result, prices[r] - prices[l])
17+
18+
return result
19+
20+
21+
"""
22+
Solution:
23+
1) prices를 순회하면서 max_profit 을 찾는다.
24+
2) profit 은 current price - min_profit로 구한다.
25+
Time: O(n)
26+
Space: O(1)
27+
"""
28+
29+
30+
class Solution:
31+
def maxProfit(self, prices: List[int]) -> int:
32+
n = len(prices)
33+
max_profit = 0
34+
min_price = prices[0]
35+
36+
for i in range(1, len(prices)):
37+
profit = prices[i] - min_price
38+
max_profit = max(max_profit, profit)
39+
min_price = min(prices[i], min_price)
40+
return max_profit
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
/**
4+
1. understanding
5+
- price[i]: i th day's stock price
6+
- to maximize profit, choose a single day to buy, and future day to sell.
7+
- return maximum profit
8+
- [7, 1, 5, 3, 6, 4] -> [0, 0, 4, 4, 5, 5]
9+
- [7, 6, 4, 3, 1] -> [0, 0, 0, 0, 0]
10+
2. strategy
11+
- profit = (sell price) - (buy price)
12+
3. complexity
13+
- time: O(N)
14+
- space: O(1)
15+
*/
16+
int minPrice = prices[0];
17+
for (int i = 0; i <prices.length; i++) {
18+
int tmp = prices[i];
19+
if (i == 0) {
20+
prices[i] = 0;
21+
} else {
22+
prices[i] = Math.max(prices[i-1], prices[i] - minPrice);
23+
}
24+
minPrice = Math.min(minPrice, tmp);
25+
}
26+
27+
return prices[prices.length - 1];
28+
}
29+
}
30+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode_study
2+
3+
import kotlin.math.max
4+
import kotlin.math.min
5+
fun maxProfit(prices: IntArray): Int {
6+
var minPrice = prices[0]
7+
var maxProfit = 0
8+
9+
for (i in 0 until prices.size) {
10+
maxProfit = max(maxProfit, prices[i] - minPrice)
11+
minPrice = min(minPrice, prices[i])
12+
}
13+
14+
return maxProfit
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* 최대 수익을 구하는 알고리즘
3+
* 알고리즘 복잡도
4+
* - 시간 복잡도: O(n)
5+
* - 공간 복잡도: O(1)
6+
* @param prices
7+
*/
8+
function maxProfit(prices: number[]): number {
9+
let min = prices[0]
10+
let total = 0
11+
12+
for(let i = 1 ; i < prices.length ; i++) {
13+
min = Math.min(min, prices[i])
14+
// console.log(dp[i],'===', dp[i-1], '===', prices[i])
15+
total = Math.max(total, prices[i] - min)
16+
}
17+
18+
return total
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
2+
3+
package hello
4+
5+
func maxProfit(prices []int) int {
6+
min := prices[0]
7+
maxProfit := 0
8+
9+
for i := 1; i < len(prices); i++ {
10+
if prices[i] < min {
11+
min = prices[i]
12+
}
13+
if (prices[i] - min) > maxProfit {
14+
maxProfit = prices[i] - min
15+
}
16+
}
17+
18+
return maxProfit
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int minPrice = Integer.MAX_VALUE;
4+
int maxProfit = 0;
5+
for(int i = 0; i < prices.length; i++) {
6+
minPrice = Math.min(minPrice, prices[i]);
7+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
8+
}
9+
return maxProfit;
10+
}
11+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// 풀이 방법
2+
// 1. 인덱스 x까지의 최소값을 저장하는 배열 1개와, 인덱스 x부터의 최대값을 저장하는 배열 1개를 만든다.
3+
// 2. 1번에서 만든 두 배열에 값을 채운다.
4+
// 3. 두 배열을 각각 인덱스 별로 차를 구하고, 그 중 최댓값을 구한다.
5+
6+
// 시간 복잡도
7+
// O(n) : 배열을 2번 순회하므로 O(n)이다.
8+
// 공간 복잡도
9+
// O(n) : 최소값과 최대값을 저장하는 배열을 만들었으므로 O(n)이다.
10+
11+
class Solution {
12+
public int maxProfit(int[] prices) {
13+
int len = prices.length;
14+
int[] minArr = new int[len];
15+
int[] maxArr = new int[len];
16+
17+
for(int i=0;i<len;i++){
18+
if(i==0){
19+
minArr[i] = prices[i];
20+
maxArr[len-i-1] = prices[len-i-1];
21+
}else{
22+
minArr[i] = Math.min(minArr[i-1], prices[i]);
23+
maxArr[len-i-1] = Math.max(maxArr[len-i], prices[len-i-1]);
24+
}
25+
}
26+
27+
int result = 0;
28+
for(int i=0;i<len;i++){
29+
result = Math.max(result, maxArr[i]-minArr[i]);
30+
}
31+
return result;
32+
}
33+
}
34+
35+
36+
// 2nd solution
37+
// 시간 복잡도: O(n)
38+
// 공간 복잡도: O(1)
39+
40+
class Solution{
41+
public int maxProfit(int[] prices){
42+
int len = prices.length;
43+
int buy = prices[0];
44+
int result = 0;
45+
46+
for(int i=1;i<len;i++){
47+
if(prices[i]<buy){ // 더 저렴한 주식이 있으므로
48+
buy = prices[i]; // 이 주식을 산다.
49+
}else{
50+
result = Math.max(result, prices[i]-buy); // 현재 주식을 팔았을 때 이득이 더 크다면 판다.
51+
}
52+
}
53+
return result;
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Time complexity: O(n)
3+
Space complexity: O(1)
4+
5+
i <= j 인 두 인덱스 i, j에 대해서, prices[j] - prices[i]를 최대화해야 한다.
6+
7+
1. i = 0부터 시작하여, 오른쪽으로 순회한다.
8+
2. 현재 값이 max보다 크다면, max를 갱신하고, min과의 차이를 계산한다.
9+
3. 현재 값이 min보다 작다면, min을 갱신하고, max 역시 같은 값으로 갱신한다. (과거로 돌아가서 팔 수는 없으므로)
10+
*/
11+
class Solution {
12+
public int maxProfit(int[] prices) {
13+
int min = 999999;
14+
int max = 0;
15+
int ans = 0;
16+
for (int i = 0; i < prices.length; i++) {
17+
if (prices[i] > max) {
18+
max = prices[i];
19+
if (max - min > ans) {
20+
ans = max - min;
21+
}
22+
}
23+
if (prices[i] < min) {
24+
min = max = prices[i];
25+
}
26+
}
27+
28+
return ans;
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
/**
3+
* 시간 : O(n) 공간 : O(1)
4+
* 풀이
5+
* lastIndex - 1부터 0까지 조회하면서 가장높은값(maxPrice) 에서 현재값(price[i])의 차(currentProfit)를 구한다.
6+
* profit 과 currentProfit중 더 높은값이 profit.
7+
* maxPrice와 prices[i]중 더 높은값이 maxPrice가 된다.
8+
* */
9+
fun maxProfit(prices: IntArray): Int {
10+
var profit = 0
11+
var maxPrice = prices[prices.lastIndex]
12+
for (i in prices.lastIndex - 1 downTo 0) {
13+
val currentProfit = maxPrice - prices[i]
14+
profit = max(profit, currentProfit)
15+
maxPrice = max(maxPrice, prices[i])
16+
}
17+
18+
return profit
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number[]} prices
6+
* @return {number}
7+
*/
8+
var maxProfit = function (prices) {
9+
let answer = 0;
10+
let minValue = Number.MAX_SAFE_INTEGER;
11+
12+
for (const price of prices) {
13+
minValue = Math.min(minValue, price);
14+
answer = Math.max(answer, price - minValue);
15+
}
16+
17+
return answer;
18+
};

0 commit comments

Comments
 (0)