-
Notifications
You must be signed in to change notification settings - Fork 126
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
[minji-go] Week 4 #817
Merged
Merged
[minji-go] Week 4 #817
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ab7f5f
refactor: complexity of combination sum
minji-go 402d2bd
feat: merge two sorted lists
minji-go 75025a3
feat: missing number
minji-go d6d11fe
feat: word search
minji-go f625aac
feat: palindromic substrings
minji-go 69b7a79
feat: coin change
minji-go ab5674b
refactor: complexity of merge two sorted lists
minji-go d55d0f7
refactor: complexity of coin change
minji-go 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 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,23 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/coin-change/ | ||
Description: return the fewest number of coins that you need to make up that amount | ||
Concept: Array, Dynamic Programming, Breadth-First Search | ||
Time Complexity: O(N²), Runtime 15ms - N is the amount | ||
Space Complexity: O(N), Memory 44.28MB | ||
*/ | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
int[] dp = new int[amount+1]; | ||
Arrays.fill(dp, amount+1); | ||
dp[0]=0; | ||
|
||
for(int i=1; i<=amount; i++){ | ||
for(int coin : coins){ | ||
if(i >= coin) { | ||
dp[i] = Math.min(dp[i], dp[i-coin] +1); | ||
} | ||
} | ||
} | ||
return dp[amount]>amount? -1: dp[amount]; | ||
} | ||
} |
This file contains 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
This file contains 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,36 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/merge-two-sorted-lists/ | ||
Description: return the head of the merged linked list of two sorted linked lists | ||
Concept: Linked List, Recursion | ||
Time Complexity: O(N+M), Runtime 0ms | ||
Space Complexity: O(1), Memory 42.74MB | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode() {} | ||
* ListNode(int val) { this.val = val; } | ||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
* } | ||
*/ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
|
||
ListNode head = new ListNode(0); | ||
ListNode tail = head; | ||
|
||
while(list1 != null || list2 != null) { | ||
if (list2 == null || (list1 != null && list1.val <= list2.val)) { | ||
tail = tail.next = new ListNode(list1.val); | ||
list1 = list1.next; | ||
} else { | ||
tail = tail.next = new ListNode(list2.val); | ||
list2 = list2.next; | ||
} | ||
} | ||
return head.next; | ||
} | ||
} |
This file contains 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,19 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/missing-number/ | ||
Description: return the only number in the range that is missing from the array. | ||
Concept: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting | ||
Time Complexity: O(N), Runtime 0ms | ||
Space Complexity: O(1), Memory 45.71MB | ||
*/ | ||
class Solution { | ||
public int missingNumber(int[] nums) { | ||
int n = nums.length; | ||
int missing = n; | ||
|
||
for(int i=0; i<n; i++){ | ||
missing ^= i; | ||
missing ^= nums[i]; | ||
} | ||
return missing; | ||
} | ||
} |
This file contains 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,27 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/palindromic-substrings/ | ||
Description: return the number of palindromic substrings in it. | ||
Concept: Two Pointers, String, Dynamic Programming | ||
Time Complexity: O(N²), Runtime 6ms | ||
Space Complexity: O(1), Memory 41.62MB | ||
*/ | ||
class Solution { | ||
public int countSubstrings(String s) { | ||
int totalCount = 0; | ||
for(int i=0; i<s.length(); i++){ | ||
totalCount+= countPalindromes(s, i, i); | ||
totalCount+= countPalindromes(s, i, i+1); | ||
} | ||
return totalCount; | ||
} | ||
|
||
public int countPalindromes(String s, int left, int right){ | ||
int count = 0; | ||
while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)) { | ||
count++; | ||
right++; | ||
left--; | ||
} | ||
return count; | ||
} | ||
} |
This file contains 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,47 @@ | ||
/* | ||
Problem: https://leetcode.com/problems/word-search/ | ||
Description: return true if word exists in the grid | ||
Concept: Array, String, Backtracking, Matrix | ||
Time Complexity: O(MN4ᵀ), Runtime 147ms | ||
Space Complexity: O(MN), Memory 42.11MB | ||
*/ | ||
class Solution { | ||
public char[][] board; | ||
public String word; | ||
public boolean[][] visited; | ||
public int n, m; | ||
|
||
public boolean exist(char[][] board, String word) { | ||
this.board = board; | ||
this.word = word; | ||
this.m = board.length; | ||
this.n = board[0].length; | ||
this.visited = new boolean[m][n]; | ||
|
||
for(int i=0; i<m; i++){ | ||
for(int j=0; j<n; j++){ | ||
if(word.charAt(0) != board[i][j]) continue; | ||
if(wordExists(i, j, 1)) return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public int[] dr = new int[]{-1,1,0,0}; | ||
public int[] dc = new int[]{0,0,-1,1}; | ||
public boolean wordExists(int cr, int cc, int i){ | ||
if(i==word.length()) return true; | ||
|
||
visited[cr][cc] = true; | ||
for(int k=0; k<4; k++){ | ||
int nr = cr+dr[k]; | ||
int nc = cc+dc[k]; | ||
if(nr<0||nc<0||nr>m-1||nc>n-1||visited[nr][nc]) continue; | ||
if(board[nr][nc]!=word.charAt(i)) continue; | ||
if(wordExists(nr, nc, i+1)) return true; | ||
} | ||
visited[cr][cc] = false; | ||
|
||
return false; | ||
} | ||
} |
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.
subString으로 문자열을 자르지 않고 앞, 뒤 index를 가리키는 방법으로 회문을 판단하는 로직을 사용하셨네요!
하나 배워갑니다 😀
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.
@EcoFriendlyAppleSu 리뷰 감사합니다 :)