Skip to content
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 8 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions coin-change/minji-go.java
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(NM), Runtime 15ms - M is the amount
Space Complexity: O(M), 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];
}
}
6 changes: 2 additions & 4 deletions combination-sum/minji-go.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
Problem: https://leetcode.com/problems/combination-sum/
Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
Concept: Array, Backtracking
Time Complexity: O(Nⁿ), Runtime 2ms
Space Complexity: O(N), Memory 44.88MB

- Time Complexity, Space Complexity를 어떻게 계산해야할지 어렵네요 :(
Time Complexity: O(Nᵀ), Runtime 2ms
Space Complexity: O(T), Memory 44.88MB
*/
class Solution {
public List<List<Integer>> answer = new ArrayList<>();
Expand Down
36 changes: 36 additions & 0 deletions merge-two-sorted-lists/minji-go.java
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(N+M), 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;
}
}
19 changes: 19 additions & 0 deletions missing-number/minji-go.java
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;
}
}
27 changes: 27 additions & 0 deletions palindromic-substrings/minji-go.java
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--;
}
Comment on lines +20 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subString으로 문자열을 자르지 않고 앞, 뒤 index를 가리키는 방법으로 회문을 판단하는 로직을 사용하셨네요!
하나 배워갑니다 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EcoFriendlyAppleSu 리뷰 감사합니다 :)

return count;
}
}
47 changes: 47 additions & 0 deletions word-search/minji-go.java
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;
}
}
Loading