Skip to content

Commit 5fa3676

Browse files
authored
Merge pull request #817 from minji-go/main
[minji-go] Week 4
2 parents df3d66f + d55d0f7 commit 5fa3676

File tree

6 files changed

+154
-4
lines changed

6 files changed

+154
-4
lines changed

coin-change/minji-go.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Problem: https://leetcode.com/problems/coin-change/
3+
Description: return the fewest number of coins that you need to make up that amount
4+
Concept: Array, Dynamic Programming, Breadth-First Search
5+
Time Complexity: O(NM), Runtime 15ms - M is the amount
6+
Space Complexity: O(M), Memory 44.28MB
7+
*/
8+
class Solution {
9+
public int coinChange(int[] coins, int amount) {
10+
int[] dp = new int[amount+1];
11+
Arrays.fill(dp, amount+1);
12+
dp[0]=0;
13+
14+
for(int i=1; i<=amount; i++){
15+
for(int coin : coins){
16+
if(i >= coin) {
17+
dp[i] = Math.min(dp[i], dp[i-coin] +1);
18+
}
19+
}
20+
}
21+
return dp[amount]>amount? -1: dp[amount];
22+
}
23+
}

combination-sum/minji-go.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
Problem: https://leetcode.com/problems/combination-sum/
33
Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
44
Concept: Array, Backtracking
5-
Time Complexity: O(Nⁿ), Runtime 2ms
6-
Space Complexity: O(N), Memory 44.88MB
7-
8-
- Time Complexity, Space Complexity를 어떻게 계산해야할지 어렵네요 :(
5+
Time Complexity: O(Nᵀ), Runtime 2ms
6+
Space Complexity: O(T), Memory 44.88MB
97
*/
108
class Solution {
119
public List<List<Integer>> answer = new ArrayList<>();

merge-two-sorted-lists/minji-go.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Problem: https://leetcode.com/problems/merge-two-sorted-lists/
3+
Description: return the head of the merged linked list of two sorted linked lists
4+
Concept: Linked List, Recursion
5+
Time Complexity: O(N+M), Runtime 0ms
6+
Space Complexity: O(N+M), Memory 42.74MB
7+
*/
8+
9+
/**
10+
* Definition for singly-linked list.
11+
* public class ListNode {
12+
* int val;
13+
* ListNode next;
14+
* ListNode() {}
15+
* ListNode(int val) { this.val = val; }
16+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
17+
* }
18+
*/
19+
class Solution {
20+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
21+
22+
ListNode head = new ListNode(0);
23+
ListNode tail = head;
24+
25+
while(list1 != null || list2 != null) {
26+
if (list2 == null || (list1 != null && list1.val <= list2.val)) {
27+
tail = tail.next = new ListNode(list1.val);
28+
list1 = list1.next;
29+
} else {
30+
tail = tail.next = new ListNode(list2.val);
31+
list2 = list2.next;
32+
}
33+
}
34+
return head.next;
35+
}
36+
}

missing-number/minji-go.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Problem: https://leetcode.com/problems/missing-number/
3+
Description: return the only number in the range that is missing from the array.
4+
Concept: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting
5+
Time Complexity: O(N), Runtime 0ms
6+
Space Complexity: O(1), Memory 45.71MB
7+
*/
8+
class Solution {
9+
public int missingNumber(int[] nums) {
10+
int n = nums.length;
11+
int missing = n;
12+
13+
for(int i=0; i<n; i++){
14+
missing ^= i;
15+
missing ^= nums[i];
16+
}
17+
return missing;
18+
}
19+
}

palindromic-substrings/minji-go.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Problem: https://leetcode.com/problems/palindromic-substrings/
3+
Description: return the number of palindromic substrings in it.
4+
Concept: Two Pointers, String, Dynamic Programming
5+
Time Complexity: O(N²), Runtime 6ms
6+
Space Complexity: O(1), Memory 41.62MB
7+
*/
8+
class Solution {
9+
public int countSubstrings(String s) {
10+
int totalCount = 0;
11+
for(int i=0; i<s.length(); i++){
12+
totalCount+= countPalindromes(s, i, i);
13+
totalCount+= countPalindromes(s, i, i+1);
14+
}
15+
return totalCount;
16+
}
17+
18+
public int countPalindromes(String s, int left, int right){
19+
int count = 0;
20+
while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)) {
21+
count++;
22+
right++;
23+
left--;
24+
}
25+
return count;
26+
}
27+
}

word-search/minji-go.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Problem: https://leetcode.com/problems/word-search/
3+
Description: return true if word exists in the grid
4+
Concept: Array, String, Backtracking, Matrix
5+
Time Complexity: O(MN4ᵀ), Runtime 147ms
6+
Space Complexity: O(MN), Memory 42.11MB
7+
*/
8+
class Solution {
9+
public char[][] board;
10+
public String word;
11+
public boolean[][] visited;
12+
public int n, m;
13+
14+
public boolean exist(char[][] board, String word) {
15+
this.board = board;
16+
this.word = word;
17+
this.m = board.length;
18+
this.n = board[0].length;
19+
this.visited = new boolean[m][n];
20+
21+
for(int i=0; i<m; i++){
22+
for(int j=0; j<n; j++){
23+
if(word.charAt(0) != board[i][j]) continue;
24+
if(wordExists(i, j, 1)) return true;
25+
}
26+
}
27+
return false;
28+
}
29+
30+
public int[] dr = new int[]{-1,1,0,0};
31+
public int[] dc = new int[]{0,0,-1,1};
32+
public boolean wordExists(int cr, int cc, int i){
33+
if(i==word.length()) return true;
34+
35+
visited[cr][cc] = true;
36+
for(int k=0; k<4; k++){
37+
int nr = cr+dr[k];
38+
int nc = cc+dc[k];
39+
if(nr<0||nc<0||nr>m-1||nc>n-1||visited[nr][nc]) continue;
40+
if(board[nr][nc]!=word.charAt(i)) continue;
41+
if(wordExists(nr, nc, i+1)) return true;
42+
}
43+
visited[cr][cc] = false;
44+
45+
return false;
46+
}
47+
}

0 commit comments

Comments
 (0)