Skip to content

Commit 931ace2

Browse files
authored
Merge pull request #833 from ekgns33/main
[byteho0n] Week4
2 parents 1061ab6 + ceb26c5 commit 931ace2

File tree

5 files changed

+222
-0
lines changed

5 files changed

+222
-0
lines changed

coin-change/ekgns33.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
input : array of integers each element represents coins, single integer amount
3+
output : fewest number of coin to make up the amount
4+
constraint
5+
1) elements are positive?
6+
yes
7+
2) coins are in integer range?
8+
yes
9+
3) range of amoutn
10+
integer range?
11+
[0, 10^4]
12+
4) at least one valid answer exists?
13+
nope. if there no exist than return -1
14+
15+
edge. case
16+
1) if amount == 0 return 0;
17+
18+
solution 1) top-down approach
19+
20+
amount - each coin
21+
until amount == 0
22+
return min
23+
tc : O(n * k) when n is amount, k is unique coin numbers
24+
sc : O(n) call stack
25+
26+
solution 2) bottom-up
27+
tc : O(n*k)
28+
sc : O(n)
29+
let dp[i] the minimum number of coins to make up amount i
30+
31+
*/
32+
class Solution {
33+
public int coinChange(int[] coins, int amount) {
34+
//edge
35+
if(amount == 0) return 0;
36+
int[] dp = new int[amount+1];
37+
dp[0] = 0;
38+
for(int i = 1; i<= amount; i++) {
39+
dp[i] = amount+1;
40+
for(int coin: coins) {
41+
if(i - coin >= 0) {
42+
dp[i] = Math.min(dp[i], dp[i-coin] + 1);
43+
}
44+
}
45+
}
46+
return dp[amount] == amount+1 ? -1 : dp[amount];
47+
}
48+
}

merge-two-sorted-lists/ekgns33.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
/**
12+
input : two sorted integer linked list
13+
output : merged single linked list with sorted order
14+
constraints:
15+
1) both linked list can be empty?
16+
yes
17+
2) both given lists are sorted?
18+
yes
19+
edge :
20+
1) if both list are empty return null
21+
2) if one of input lists is empty return the other one
22+
23+
solution 1)
24+
using two pointer
25+
26+
compare the current pointer's value.
27+
get the smaller one, set to the next node
28+
29+
until both pointer reach the end
30+
31+
tc : O(n) sc : O(1)
32+
*/
33+
class Solution {
34+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
35+
ListNode dummy = new ListNode();
36+
ListNode p1 = list1;
37+
ListNode p2 = list2;
38+
ListNode currNode = dummy;
39+
while(p1 != null && p2 != null) {
40+
if(p1.val < p2.val){
41+
currNode.next = p1;
42+
p1 = p1.next;
43+
} else {
44+
currNode.next = p2;
45+
p2 = p2.next;
46+
}
47+
currNode = currNode.next;
48+
}
49+
50+
if(p1 == null) {
51+
currNode.next = p2;
52+
} else {
53+
currNode.next = p1;
54+
}
55+
56+
return dummy.next;
57+
}
58+
}

missing-number/ekgns33.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
input : integer of array
3+
output : return the only missing number
4+
constraints:
5+
1) is there any duplicate?
6+
no.
7+
2) range of element
8+
[0, n]
9+
10+
solution 1)
11+
iterate through the array
12+
save to hash set
13+
iterate i from 0 to n
14+
check if exists
15+
16+
tc : O(n) sc : O(n)
17+
18+
solution 2)
19+
iterate throught the array
20+
add all the elements
21+
get sum of integer sequence 0 .. n with. n(n+1)/2
22+
get subtraction
23+
tc : O(n) sc : O(1)
24+
*/
25+
class Solution {
26+
public int missingNumber(int[] nums) {
27+
int sum = 0;
28+
int n = nums.length;
29+
for(int num : nums) {
30+
sum += num;
31+
}
32+
int totalSum = (n+1) * n / 2;
33+
return totalSum - sum;
34+
}
35+
}

palindromic-substrings/ekgns33.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
input : string s
3+
output: the number of palindromic substrings of given string
4+
tc : O(n^2) sc : o(n^2) when n is the length of string s
5+
6+
optimize?
7+
maybe nope.. atleast n^2 to select i and j
8+
9+
*/
10+
class Solution {
11+
public int countSubstrings(String s) {
12+
int n = s.length();
13+
int cnt = 0;
14+
boolean[][] dp = new boolean[n][n];
15+
for(int i = n-1; i >= 0; i--) {
16+
for(int j = i; j < n; j++) {
17+
dp[i][j] = s.charAt(i) == s.charAt(j) && (j-i +1 < 3 || dp[i+1][j-1]);
18+
if(dp[i][j]) cnt++;
19+
}
20+
}
21+
return cnt;
22+
}
23+
}

word-search/ekgns33.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
input : m x n matrix and string word
3+
output : return true if given word can be constructed
4+
5+
solution 1) brute force
6+
tc : O(n * 4^k) when n is the number of cells, k is the length of word
7+
sc : O(k) call stack
8+
*/
9+
class Solution {
10+
private int[][] directions = new int[][] {{-1,0}, {1,0}, {0,1}, {0,-1}};
11+
public boolean exist(char[][] board, String word) {
12+
//edge case
13+
int m = board.length;
14+
int n = board[0].length;
15+
16+
if(m * n < word.length()) return false;
17+
18+
19+
//look for the starting letter and do dfs
20+
for(int i = 0; i < m; i++) {
21+
22+
for(int j = 0; j < n; j++) {
23+
24+
if(board[i][j] == word.charAt(0)) {
25+
//do dfs and get answer
26+
board[i][j] = '0';
27+
boolean res = dfsHelper(board, word, i, j, 1);
28+
board[i][j] = word.charAt(0);
29+
if(res) return true;
30+
}
31+
}
32+
}
33+
34+
return false;
35+
}
36+
37+
public boolean dfsHelper(char[][] board, String word, int curR, int curC, int curP) {
38+
39+
//endclause
40+
if(curP == word.length()) return true;
41+
42+
boolean ret = false;
43+
44+
for(int[] direction : directions) {
45+
int nextR = curR + direction[0];
46+
int nextC = curC + direction[1];
47+
48+
if(nextR < 0 || nextR >= board.length || nextC < 0 || nextC >= board[0].length) continue;
49+
50+
if(board[nextR][nextC] == word.charAt(curP)) {
51+
board[nextR][nextC] = '0';
52+
ret = ret || dfsHelper(board, word, nextR, nextC, curP + 1);
53+
board[nextR][nextC] = word.charAt(curP);
54+
}
55+
}
56+
return ret;
57+
}
58+
}

0 commit comments

Comments
 (0)