Skip to content

Commit 74e04c9

Browse files
Merge pull request #174 from dev-jonghoonpark/main
[๋ฐ•์ข…ํ›ˆ] 11์ฃผ์ฐจ ๋‹ต์•ˆ ์ œ์ถœ
2 parents a42e777 + 7bffe74 commit 74e04c9

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/coin-change/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/02/26/leetcode-322
3+
4+
## dfs๋กœ ํ’€๊ธฐ
5+
6+
```java
7+
class Solution {
8+
public int coinChange(int[] coins, int amount) {
9+
if(amount == 0) {
10+
return 0;
11+
}
12+
13+
int[] dp = new int[amount + 1];
14+
15+
List<Integer> sortedCoins = Arrays.stream(coins).boxed()
16+
.sorted(Collections.reverseOrder())
17+
.toList();
18+
19+
sortedCoins.forEach(coin -> dfs(dp, sortedCoins, amount, coin));
20+
21+
return dp[0] == 0 ? -1 : dp[0];
22+
}
23+
24+
void dfs(int[] dp, List<Integer> coins, int amount, int selectedCoin) {
25+
int currentPointer = amount - selectedCoin;
26+
if (currentPointer < 0) {
27+
return;
28+
}
29+
30+
if (dp[currentPointer] == 0 || dp[currentPointer] > dp[amount] + 1) {
31+
dp[currentPointer] = dp[amount] + 1;
32+
coins.forEach(coin -> dfs(dp, coins, currentPointer, coin));
33+
}
34+
}
35+
}
36+
```
37+
38+
### TS, SC
39+
40+
์ฝ”์ธ์˜ ์ˆ˜๋ฅผ n ์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, `O(n * amount ^ 2)` ์˜ ์‹œ๊ฐ„๋ณต์žก๋„์™€ `O(amount)` ์˜ ๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.
41+
42+
## dp๋กœ ํ’€๊ธฐ
43+
44+
```java
45+
public class Solution {
46+
public int coinChange(int[] coins, int amount) {
47+
int max = amount + 1;
48+
int[] dp = new int[amount + 1];
49+
Arrays.fill(dp, max);
50+
dp[0] = 0;
51+
52+
for (int i = 1; i <= amount; i++) {
53+
for (int j = 0; j < coins.length; j++) {
54+
if (coins[j] <= i) {
55+
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
56+
}
57+
}
58+
}
59+
60+
return dp[amount] > amount ? -1 : dp[amount];
61+
}
62+
}
63+
```
64+
65+
### TS, SC
66+
67+
์ฝ”์ธ์˜ ์ˆ˜๋ฅผ n ์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, `O(n * amount)` ์˜ ์‹œ๊ฐ„๋ณต์žก๋„์™€ `O(amount)` ์˜ ๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/decode-ways/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/07/08/leetcode-91
3+
4+
```java
5+
class Solution {
6+
public int numDecodings(String s) {
7+
int[] dp = new int[s.length()];
8+
9+
if (s.charAt(0) == '0') {
10+
return 0;
11+
}
12+
dp[0] = 1;
13+
14+
for (int i = 1; i < s.length(); i++) {
15+
int oneDigit = Integer.parseInt(String.valueOf(s.charAt(i)));
16+
if (oneDigit > 0) {
17+
dp[i] = dp[i - 1];
18+
}
19+
20+
int prevDigit = Integer.parseInt(String.valueOf(s.charAt(i - 1)));
21+
if (prevDigit == 0) {
22+
continue;
23+
}
24+
25+
int twoDigit = prevDigit * 10 + oneDigit;
26+
if (twoDigit <= 26) {
27+
if (i > 2) {
28+
dp[i] = dp[i] + dp[i - 2];
29+
} else {
30+
dp[i] = dp[i] + 1;
31+
}
32+
}
33+
}
34+
35+
return dp[s.length() - 1];
36+
}
37+
}
38+
```
39+
40+
### TC, SC
41+
42+
์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ด๋‹ค.
43+
์ด๋Ÿฐ์‹์œผ๋กœ ์ตœ๊ทผ ๋ฐ์ดํ„ฐ๋งŒ ์žฌ์‚ฌ์šฉ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ O(1) ์œผ๋กœ๋„ ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.
44+
์ตœ๊ทผ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹Œ ์ด์ „ ๋ฐ์ดํ„ฐ๋“ค์€ ๋” ์ด์ƒ ์ฐธ์กฐ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•œ ๊ณต๊ฐ„๋งŒ ๋งŒ๋“ค์–ด์„œ ๋ณด๊ด€ํ•˜๋ฉด ๋œ๋‹ค.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/maximum-product-subarray/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/07/09/leetcode-152
3+
4+
```java
5+
class Solution {
6+
public int maxProduct(int[] nums) {
7+
int max = Integer.MIN_VALUE;
8+
int temp = 0;
9+
int lastZeroIndex = 0;
10+
for (int i = 0; i < nums.length; i++) {
11+
int current = nums[i];
12+
if (temp == 0) {
13+
temp = current;
14+
lastZeroIndex = i;
15+
} else {
16+
if (current == 0) {
17+
temp = 0;
18+
} else if (temp > 0 && current < 0) {
19+
if (hasNextMinus(nums, i + 1)) {
20+
temp = temp * current;
21+
} else {
22+
temp = temp * current;
23+
for (int j = lastZeroIndex; j < i + 1; j++) {
24+
temp = temp / nums[j];
25+
if (temp > 0) {
26+
break;
27+
}
28+
}
29+
}
30+
} else {
31+
temp = temp * current;
32+
}
33+
}
34+
max = Math.max(max, temp);
35+
if (temp < 0 && !hasNextMinus(nums, i + 1)) {
36+
temp = 0;
37+
}
38+
}
39+
return max;
40+
}
41+
42+
private boolean hasNextMinus(int[] nums, int i) {
43+
for (; i < nums.length; i++) {
44+
if (nums[i] < 0) {
45+
return true;
46+
} else if (nums[i] == 0) {
47+
return false;
48+
}
49+
}
50+
return false;
51+
}
52+
}
53+
```
54+
55+
### TC, SC
56+
57+
์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2)์ด๋‹ค. ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(1)์ด๋‹ค. hasNextMinus ์ด ์ ๊ฒŒ ํ˜ธ์ถœ๋œ๋‹ค๋ฉด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์— ๊ฐ€๊น๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/palindromic-substrings/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/07/08/leetcode-647
3+
4+
```java
5+
class Solution {
6+
public int countSubstrings(String s) {
7+
int count = 0;
8+
9+
char[] charArray = s.toCharArray();
10+
for (int i = 0; i < s.length(); i++) {
11+
char currentChar = charArray[i];
12+
count++;
13+
14+
int left = i;
15+
int right = i;
16+
17+
while (right < s.length() - 1 && currentChar == charArray[right + 1]) {
18+
right++;
19+
count++;
20+
}
21+
22+
while (left > 0 && right < s.length() - 1 && charArray[left - 1] == charArray[right + 1]) {
23+
left--;
24+
right++;
25+
count++;
26+
}
27+
}
28+
29+
return count;
30+
}
31+
}
32+
```
33+
34+
### TC, SC
35+
36+
์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ํ‰๊ท ์ ์œผ๋กœ O(n)์ด๋‹ค. palindrome ์˜ ๊ธธ์ด๊ฐ€ n ์— ๊ฐ€๊นŒ์›Œ์งˆ์ˆ˜๋ก ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2) ์— ๊ฐ€๊นŒ์›Œ ์ง„๋‹ค.
37+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ด๋‹ค.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/word-break/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/02/28/leetcode-139
3+
4+
```java
5+
class Solution {
6+
public boolean wordBreak(String s, List<String> wordDict) {
7+
boolean[] dp = new boolean[s.length() + 1];
8+
dp[0] = true;
9+
10+
for (int i = 1; i <= s.length(); i++) {
11+
for (String word : wordDict) {
12+
if (i >= word.length()) {
13+
int start = i - word.length();
14+
if (dp[start] && s.startsWith(word, start)) {
15+
dp[i] = true;
16+
}
17+
}
18+
}
19+
}
20+
21+
return dp[s.length()];
22+
}
23+
}
24+
```
25+
26+
### TC, SC
27+
28+
s์˜ ๊ธธ์ด๋ฅผ n ์ด๋ผ ํ•˜๊ณ , wordDict์˜ ํฌ๊ธฐ๋ฅผ m ์ด๋ผ๊ณ  ํ•  ๋•Œ, ์‹œ๊ฐ„๋ณต์žก๋„๋Š” `O(n * m)` ๊ณต๊ฐ„๋ณต์žก๋„๋Š” `O(n)` ์ด๋‹ค.

0 commit comments

Comments
ย (0)