-
-
Notifications
You must be signed in to change notification settings - Fork 195
[forest000014] Week 04 #805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd7cc59
Merge Two Sorted Lists
forest000014 c955ca4
Missing Number
forest000014 300d1c1
Word Search
forest000014 848dc5d
Coin Change
forest000014 138e4c5
Palindromic Substrings
forest000014 336cce3
Palindromic Substrings - PR 피드백 반영
forest000014 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Time Complexity: O(coins.length * amount) | ||
Space Complexity: O(amount) | ||
|
||
1 ~ i-1원까지의 최적해를 알고 있다면, i원의 최적해를 구할 때 coins 배열 iteration으로 구할 수 있음 | ||
*/ | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
int c = coins.length; | ||
|
||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, 99999); | ||
dp[0] = 0; | ||
|
||
for (int i = 1; i <= amount; i++) { | ||
for (int j = 0; j < c; j++) { | ||
if (i - coins[j] < 0) { | ||
continue; | ||
} | ||
if (dp[i - coins[j]] >= 0 && dp[i - coins[j]] + 1 < dp[i]) { | ||
dp[i] = dp[i - coins[j]] + 1; | ||
} | ||
} | ||
} | ||
|
||
return dp[amount] == 99999 ? -1 : dp[amount]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
|
||
(n = list1.length + list2.length) | ||
시간 복잡도: O(n) | ||
- 최악의 경우 전체 노드 순회 | ||
공간 복잡도: O(n) | ||
- 최악의 경우 전체 노드만큼 새 노드 생성 | ||
|
||
*/ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
ListNode head = new ListNode(); | ||
ListNode curr = head; | ||
|
||
while (list1 != null && list2 != null) { | ||
if (list1.val <= list2.val) { | ||
curr.next = new ListNode(list1.val); | ||
curr = curr.next; | ||
list1 = list1.next; | ||
} else { | ||
curr.next = new ListNode(list2.val); | ||
curr = curr.next; | ||
list2 = list2.next; | ||
} | ||
} | ||
|
||
if (list1 == null) { | ||
curr.next = list2; | ||
} else if (list2 == null) { | ||
curr.next = list1; | ||
} | ||
|
||
return head.next; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
시간 복잡도: O(n) | ||
공간 복잡도: O(1) | ||
|
||
1 ~ n의 합이 n * (n + 1) / 2 라는 점을 활용 | ||
*/ | ||
class Solution { | ||
public int missingNumber(int[] nums) { | ||
int n = nums.length; | ||
int ans = n * (n + 1) / 2; | ||
for (int i = 0; i < n; i++) { | ||
ans -= nums[i]; | ||
} | ||
return ans; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
time complexity: O(n^2) | ||
space complexity: O(1) | ||
|
||
모든 가능한 조합(O(n^2))에 대해 palindrome 여부를 검사(O(n))하는 brute force는 O(n^3). | ||
i번째 문자에서 시작하여, 앞/뒤로 한 글자씩 늘려가면서 탐색하되, 한번이라도 앞/뒤 글자가 서로 다르다면 그 이후는 탐색하지 않을 수 있음. 이 경우는 O(n^2). | ||
*/ | ||
class Solution { | ||
public int countSubstrings(String s) { | ||
int ans = 0; | ||
for (int i = 0; i < s.length(); i++) { | ||
int head = i, tail = i; | ||
while (head >= 0 && tail < s.length()) { | ||
if (s.charAt(head) == s.charAt(tail)) { | ||
ans++; | ||
} else { | ||
break; | ||
} | ||
head--; | ||
tail++; | ||
} | ||
|
||
head = i; | ||
tail = i + 1; | ||
while (head >= 0 && tail < s.length()) { | ||
if (s.charAt(head) == s.charAt(tail)) { | ||
ans++; | ||
} else { | ||
break; | ||
} | ||
head--; | ||
tail++; | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Time Complexity: O(m * n * 4^(word.length)) | ||
Space Complexity: O(m * n) | ||
*/ | ||
|
||
class Solution { | ||
boolean[][] visited; | ||
int m, n; | ||
int len; | ||
int[] dr = {-1, 0, 1, 0}; // clockwise traversal | ||
int[] dc = {0, 1, 0, -1}; | ||
char board2[][]; | ||
String word2; | ||
|
||
public boolean exist(char[][] board, String word) { | ||
int[] cnt = new int[52]; | ||
board2 = board; | ||
word2 = word; | ||
m = board.length; | ||
n = board[0].length; | ||
visited = new boolean[m][n]; | ||
len = word.length(); | ||
|
||
// 1. for pruning, count characters in board and word respectively | ||
for (int i = 0; i < m; i++) { | ||
for (int j = 0; j < n; j++) { | ||
cnt[charToInt(board[i][j])]++; | ||
} | ||
} | ||
for (int i = 0; i < len; i++) { | ||
int idx = charToInt(word.charAt(i)); | ||
if (--cnt[idx] < 0) { | ||
return false; | ||
} | ||
} | ||
|
||
// 2. DFS | ||
for (int i = 0; i < m; i++) { | ||
for (int j = 0; j < n; j++) { | ||
if (board2[i][j] != word.charAt(0)) { | ||
continue; | ||
} | ||
if (dfs(i, j, 1)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean dfs(int row, int col, int idx) { | ||
if (idx == len) { // end of word | ||
return true; | ||
} | ||
visited[row][col] = true; | ||
|
||
for (int i = 0; i < 4; i++) { | ||
int nr = row + dr[i]; | ||
int nc = col + dc[i]; | ||
if (nr < 0 || nr >= m || nc < 0 || nc >= n) { // check boundary of the board | ||
continue; | ||
} | ||
if (visited[nr][nc]) { // check visited | ||
continue; | ||
} | ||
if (board2[nr][nc] == word2.charAt(idx)) { | ||
if (dfs(nr, nc, idx + 1)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
visited[row][col] = false; | ||
return false; | ||
} | ||
|
||
private int charToInt(char ch) { | ||
if (ch <= 'Z') { | ||
return ch - 'A'; | ||
} else { | ||
return ch - 'a' + 26; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드 리뷰 반영해주셔서 감사합니다!