Skip to content

Commit 21e3894

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 257032b + 5f080b5 commit 21e3894

File tree

10 files changed

+286
-0
lines changed

10 files changed

+286
-0
lines changed

coin-change/kimjunyoung90.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Arrays;
2+
3+
class Solution {
4+
//풀이 원리 = 특정 금액을 만들 때 최소 동전 수는 몇 개?
5+
//예) 0원은 동전 0개
6+
// 1원은 동전 1개
7+
// 2원은 동전 1
8+
public int coinChange(int[] coins, int amount) {
9+
//불가능한 값
10+
int max = amount + 1;
11+
int[] dp = new int[amount + 1];
12+
//최소 동전 수를 모두 불가능한 값으로 설정
13+
Arrays.fill(dp, max);
14+
dp[0] = 0;
15+
16+
//금액 1원 부터 계산
17+
for (int i = 1; i <= amount; i++) {
18+
//사용할 수 있는 동전을 꺼냄
19+
for(int coin: coins) {
20+
if (coin <= i) {
21+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
22+
}
23+
}
24+
}
25+
26+
return dp[amount] > amount ? -1 : dp[amount];
27+
}
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findMin = function(nums) {
6+
return Math.min(...nums)
7+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// 요구사항 : 이진 탐색 이용
2+
class Solution {
3+
public int findMin(int[] nums) {
4+
int left = 0;
5+
int right = nums.length - 1;
6+
while(left < right) {
7+
int mid = (left + right) / 2;
8+
//중앙 값이 오른쪽 값보다 크다. = 최소값이 오른쪽 구간에 있음
9+
if(nums[mid] > nums[right]) {
10+
left = mid + 1;
11+
} else {
12+
right = mid;
13+
}
14+
}
15+
16+
return nums[left];
17+
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = function(root) {
14+
if(!root) return 0
15+
16+
const dfs = (node, level) =>{
17+
if(!node) return level;
18+
19+
let left = level;
20+
let right = level
21+
22+
if(node.left){
23+
left = dfs(node.left, level+1)
24+
}
25+
26+
if(node.right){
27+
right = dfs(node.right, level+1)
28+
}
29+
30+
return Math.max(left,right);
31+
}
32+
33+
return dfs(root,1)
34+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
class Solution:
3+
def maxDepth(self, root: Optional['TreeNode']) -> int:
4+
"""
5+
문제
6+
- 이진트리의 최대 깊이를 구하라.
7+
8+
아이디어 (재귀 DFS)
9+
- 공집합(None)의 깊이는 0
10+
- 현재 노드 깊이 = max(왼쪽 서브트리 깊이, 오른쪽 서브트리 깊이) + 1
11+
12+
시간복잡도: O(n) — 각 노드를 한 번씩 방문
13+
공간복잡도: O(h) — 재귀 스택(h는 트리 높이; 최악 O(n), 평균 O(log n))
14+
"""
15+
# 1) 빈 트리이면 깊이 0
16+
if not root:
17+
return 0
18+
19+
# 2) 왼쪽/오른쪽 서브트리의 최대 깊이를 구한다
20+
left_depth = self.maxDepth(root.left)
21+
right_depth = self.maxDepth(root.right)
22+
23+
# 3) 현재 노드의 깊이 = 더 큰 서브트리 깊이 + 1(현재 노드 포함)
24+
return max(left_depth, right_depth) + 1
25+
# dfs(3) - 왼쪽으로 이동
26+
# dfs(9) - 왼쪽으로 이동
27+
# dfs(None) -> 0
28+
# dfs(9) - 오른쪽으로 이동
29+
# dfs(None) -> 0
30+
# dfs(9) 반환: max(0,0)+1 = 1
31+
# dfs(3) - 오른쪽으로 이동
32+
# dfs(20) - 왼쪽으로 이동
33+
# dfs(15) - 왼쪽으로 이동
34+
# dfs(None) -> 0
35+
# dfs(15) - 오른쪽으로 이동
36+
# dfs(None) -> 0
37+
# dfs(15) 반환: max(0,0)+1 = 1
38+
# dfs(20) - 오른쪽으로 이동
39+
# dfs(7) - 왼쪽으로 이동
40+
# dfs(None) -> 0
41+
# dfs(7) - 오른쪽으로 이동
42+
# dfs(None) -> 0
43+
# dfs(7) 반환: max(0,0)+1 = 1
44+
# dfs(20) 반환: max(1,1)+1 = 2
45+
# dfs(3) 반환: max(1,2)+1 = 3
46+
# 최종 결과: 3
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
13+
ListNode list = new ListNode(-1);
14+
ListNode result = list;
15+
while (list1 != null && list2 != null) {
16+
if (list1.val < list2.val) {
17+
result.next = list1;
18+
list1 = list1.next;
19+
} else {
20+
result.next = list2;
21+
list2 = list2.next;
22+
}
23+
result = result.next;
24+
}
25+
result.next = list1 != null ? list1 : list2;
26+
return list.next;
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
14+
/**
15+
* list는 오름 차순으로 정렬되어 있기 때문에 두 개의 리스트의 head가 더 작은 값이 listNode의 next에 순서대로 들어오게 하면 됩니다.
16+
* list 둘 중하나가 없는 경우에는 나머지 다른 하나의 리스트를 반환합니다.
17+
* 두 헤드 중에서 더 작은 값의 next에 재귀적으로 다음 링크드 리스트를 넘겨주면서 비교하는 방법으로 문제를 해결했습니다
18+
*
19+
*/
20+
21+
var mergeTwoLists = function(list1, list2) {
22+
if(!list1 || !list2) return list1 || list2
23+
24+
if(list1.val < list2.val){
25+
list1.next = mergeTwoLists(list1.next,list2)
26+
return list1
27+
}
28+
29+
list2.next = mergeTwoLists(list2.next,list1);
30+
31+
return list2;
32+
};

merge-two-sorted-lists/socow.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
문제 요약
3+
- 정렬된 두 연결 리스트를 하나의 정렬된 리스트로 병합
4+
5+
아이디어
6+
- dummy 노드로 시작점 고정
7+
- 두 리스트 비교하며 작은 값을 연결
8+
- 남은 리스트 한번에 연결
9+
10+
시간복잡도: O(n + m) - 두 리스트 길이의 합
11+
공간복잡도: O(1) - 추가 공간 없이 포인터만 변경
12+
"""
13+
14+
15+
class Solution:
16+
def mergeTwoLists(
17+
self, list1: Optional[ListNode], list2: Optional[ListNode]
18+
) -> Optional[ListNode]:
19+
dummy = ListNode() # 가짜 시작 노드
20+
node = dummy
21+
22+
# 두 리스트 비교하며 병합
23+
while list1 and list2:
24+
if list1.val < list2.val:
25+
node.next = list1
26+
list1 = list1.next
27+
else:
28+
node.next = list2
29+
list2 = list2.next
30+
node = node.next
31+
32+
# 남은 리스트 연결
33+
node.next = list1 or list2
34+
35+
return dummy.next
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[] productExceptSelf(int[] nums) {
3+
int [] result = new int [nums.length];
4+
result[0] = 1;
5+
for (int i = 1; i < nums.length; i++) {
6+
result[i] = result[i - 1] * nums[i - 1];
7+
}
8+
int suffixProduct = 1;
9+
for (int i = nums.length - 1; i >= 0; i--) {
10+
result[i] *= suffixProduct;
11+
suffixProduct *= nums[i];
12+
}
13+
return result;
14+
}
15+
}
16+

word-search/kimjunyoung90.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public boolean exist(char[][] board, String word) {
3+
4+
int rows = board.length;
5+
int columns = board[0].length;
6+
boolean[][] visited = new boolean[rows][columns];
7+
for (int i = 0; i < rows; i++) {
8+
for (int j = 0; j < columns; j++) {
9+
if (dfs(board, word, i, j, 0, visited)) {
10+
return true;
11+
}
12+
}
13+
}
14+
return false;
15+
}
16+
17+
private boolean dfs(char[][] board, String word, int row, int col, int idx, boolean[][] visited) {
18+
//예외 처리 1
19+
if (idx == word.length()) return true;
20+
21+
//예외 처리 2
22+
if (row < 0 || col < 0 || row >= board.length || col >= board[0].length) return false;
23+
24+
//예외 처리 3
25+
if (visited[row][col]) return false;
26+
27+
//1. 글자 체크
28+
if (board[row][col] != word.charAt(idx)) return false;
29+
30+
//방문 처리
31+
visited[row][col] = true;
32+
33+
//상, 하, 좌, 우 찾기
34+
boolean found = dfs(board, word, row + 1, col, idx + 1, visited)
35+
|| dfs(board, word, row - 1, col, idx + 1, visited)
36+
|| dfs(board, word, row, col + 1, idx + 1, visited)
37+
|| dfs(board, word, row, col - 1, idx + 1, visited);
38+
39+
visited[row][col] = false;
40+
return found;
41+
}
42+
}

0 commit comments

Comments
 (0)