Skip to content

Commit 7c529eb

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 7d719a2 + 3b1d72f commit 7c529eb

29 files changed

+892
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
func maxProfit(_ prices: [Int]) -> Int {
3+
var lowPrice: Int = prices[0]
4+
var maxProfit: Int = 0
5+
for i in 0..<prices.endIndex {
6+
lowPrice = min(lowPrice, prices[i])
7+
maxProfit = max(maxProfit, prices[i] - lowPrice)
8+
}
9+
return maxProfit
10+
}
11+
}
12+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
time complexity : O(n)
3+
space complexity : O(2n)
4+
5+
Algorithm : dp
6+
주식 보유하는 경우:
7+
- 이전에 이미 보유 : hold[i - 1]
8+
- i번째 날 새로 사는 경우 : -prices[i]
9+
10+
주식을 팔았을 떄:
11+
- 이전에 이미 팜 : sold[i - 1]
12+
- i번째 날 파는 경우 : hold[i-1] + prices[i]
13+
'''
14+
15+
class Solution:
16+
def maxProfit(self, prices: List[int]) -> int:
17+
if not prices or len(prices) < 2:
18+
return 0
19+
n = len(prices)
20+
hold = [0] * n
21+
sold = [0] * n
22+
23+
hold[0] = -prices[0]
24+
sold[0] = 0
25+
26+
for i in range(1, n):
27+
hold[i] = max(hold[i - 1], -prices[i])
28+
sold[i] = max(sold[i - 1], hold[i - 1] + prices[i])
29+
return sold[n - 1]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
"""
3+
1. 브루트포스
4+
2중 for문이라서 O(n^2)임.
5+
prices의 길이가 10^5이므로 10^10이 되면서 시간초과가 발생
6+
7+
2. 이분탐색으로 가능할까 했지만 DP를 사용해야 하는 문제 같음
8+
시간복잡도는 O(n)이 나옴
9+
"""
10+
"""
11+
def maxProfit(self, prices: List[int]) -> int:
12+
max_profit = 0
13+
for i in range(len(prices)-1):
14+
for j in range(i, len(prices)):
15+
profit = prices[j] - prices[i]
16+
max_profit = max(max_profit, profit)
17+
return max_profit
18+
"""
19+
def maxProfit(self, prices: List[int]) -> int:
20+
max_profit = 0
21+
min_price = prices[0]
22+
for price in prices:
23+
max_profit = max(max_profit, price - min_price)
24+
min_price = min(price, min_price)
25+
return max_profit
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxProfit(prices: number[]): number {
2+
let minPrice = Infinity;
3+
let maxProfit = 0;
4+
5+
for (const price of prices) {
6+
if (price < minPrice) {
7+
minPrice = price;
8+
} else {
9+
maxProfit = Math.max(maxProfit, price - minPrice);
10+
}
11+
}
12+
13+
return maxProfit;
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time Complexity: O(n), n: prices.length
2+
// Space Complexity: O(1), because 3 int-variables are declared -> O(1)+O(1)+O(1)
3+
class Solution {
4+
public int maxProfit(int[] prices) {
5+
int buyPrice = prices[0];
6+
int sellPrice = 0;
7+
int profit = 0; // store maxProfit
8+
9+
for (int i = 1; i < prices.length; ++i) {
10+
buyPrice = Math.min(buyPrice, prices[i]);
11+
profit = Math.max(profit, prices[i] - buyPrice);
12+
}
13+
14+
return profit;
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int price = prices[0];
4+
int maxProfit = 0;
5+
6+
for(int i=1; i<prices.length; i++){
7+
int profit = prices[i]-price;
8+
maxProfit = Math.max(maxProfit, profit);
9+
price = Math.min(price, prices[i]);
10+
}
11+
return maxProfit;
12+
}
13+
}
14+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func maxProfit(prices []int) int {
2+
ans := 0
3+
bought := prices[0]
4+
for i := 1; i != len(prices); i++ {
5+
ans = max(ans, prices[i] - bought)
6+
bought = min(bought, prices[i])
7+
}
8+
return ans
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
"""
3+
@param: strs: a list of strings
4+
@return: encodes a list of strings to a single string.
5+
"""
6+
def encode(self, strs):
7+
return "secretKey!@#".join(strs)
8+
9+
"""
10+
@param: str: A string
11+
@return: decodes a single string to a list of strings
12+
"""
13+
def decode(self, str):
14+
return str.split("secretKey!@#")
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
private DELIMITER = "*";
3+
4+
/**
5+
* @param strs: a list of strings
6+
* @returns: encodes a list of strings to a single string.
7+
*/
8+
public encode(strs: string[]): string {
9+
let encoded = "";
10+
11+
for (const str of strs) {
12+
encoded += `${str.length}${this.DELIMITER}${str}`;
13+
}
14+
15+
return encoded;
16+
}
17+
18+
/**
19+
* @param str: A string
20+
* @returns: decodes a single string to a list of strings
21+
*/
22+
public decode(str: string): string[] {
23+
const decoded: string[] = [];
24+
25+
let stack: string[] = [];
26+
let pointer = 0;
27+
28+
while (pointer < str.length) {
29+
const char = str[pointer];
30+
31+
if (char === this.DELIMITER) {
32+
let strLength: number = Number(stack.join(""));
33+
stack = [];
34+
35+
const word = str.substring(pointer + 1, pointer + 1 + strLength);
36+
pointer = pointer + 1 + strLength;
37+
38+
decoded.push(word);
39+
} else {
40+
stack.push(char);
41+
pointer++;
42+
}
43+
}
44+
45+
return decoded;
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class Solution {
2+
/*
3+
* @param strs: a list of strings
4+
* @return: encodes a list of strings to a single string.
5+
*/
6+
public String encode(List<String> strs) {
7+
// write your code here
8+
return strs.parallelStream()
9+
.map(s ->
10+
s.chars()
11+
.parallel()
12+
.mapToObj(c -> new StringBuilder(String.format("%02x", c)))
13+
.collect(
14+
StringBuilder::new,
15+
StringBuilder::append,
16+
(a, b) -> a.append(b)
17+
)
18+
).collect(
19+
StringBuilder::new,
20+
(a, b) -> a.append(b)
21+
.append(' '),
22+
(a, b) -> a.append(b)
23+
).toString();
24+
}
25+
26+
/*
27+
* @param str: A string
28+
* @return: decodes a single string to a list of strings
29+
*/
30+
public List<String> decode(String str) {
31+
// write your code here
32+
final List<String> ans = new ArrayList<>();
33+
final StringTokenizer st = new StringTokenizer(str);
34+
while (st.hasMoreTokens()) {
35+
final String s = st.nextToken();
36+
final StringBuilder sb = new StringBuilder();
37+
for (int i = 0; i < s.length(); i += 2) {
38+
sb.append((char) Integer.parseInt(s.substring(i, i + 2), 16));
39+
}
40+
ans.add(sb.toString());
41+
}
42+
return ans;
43+
}
44+
}

0 commit comments

Comments
 (0)