Skip to content

[byteho0n] Week4 #833

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 6 commits into from
Jan 5, 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
48 changes: 48 additions & 0 deletions coin-change/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
input : array of integers each element represents coins, single integer amount
output : fewest number of coin to make up the amount
constraint
1) elements are positive?
yes
2) coins are in integer range?
yes
3) range of amoutn
integer range?
[0, 10^4]
4) at least one valid answer exists?
nope. if there no exist than return -1

edge. case
1) if amount == 0 return 0;

solution 1) top-down approach

amount - each coin
until amount == 0
return min
tc : O(n * k) when n is amount, k is unique coin numbers
sc : O(n) call stack

solution 2) bottom-up
tc : O(n*k)
sc : O(n)
let dp[i] the minimum number of coins to make up amount i

*/
class Solution {
public int coinChange(int[] coins, int amount) {
//edge
if(amount == 0) return 0;
int[] dp = new int[amount+1];
dp[0] = 0;
for(int i = 1; i<= amount; i++) {
dp[i] = amount+1;
for(int coin: coins) {
if(i - coin >= 0) {
dp[i] = Math.min(dp[i], dp[i-coin] + 1);
}
}
}
return dp[amount] == amount+1 ? -1 : dp[amount];
}
}
58 changes: 58 additions & 0 deletions merge-two-sorted-lists/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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; }
* }
*/
/**
input : two sorted integer linked list
output : merged single linked list with sorted order
constraints:
1) both linked list can be empty?
yes
2) both given lists are sorted?
yes
edge :
1) if both list are empty return null
2) if one of input lists is empty return the other one

solution 1)
using two pointer

compare the current pointer's value.
get the smaller one, set to the next node

until both pointer reach the end

tc : O(n) sc : O(1)
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode dummy = new ListNode();
ListNode p1 = list1;
ListNode p2 = list2;
ListNode currNode = dummy;
while(p1 != null && p2 != null) {
if(p1.val < p2.val){
currNode.next = p1;
p1 = p1.next;
} else {
currNode.next = p2;
p2 = p2.next;
}
currNode = currNode.next;
}

if(p1 == null) {
currNode.next = p2;
} else {
currNode.next = p1;
}

return dummy.next;
}
}
35 changes: 35 additions & 0 deletions missing-number/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
input : integer of array
output : return the only missing number
constraints:
1) is there any duplicate?
no.
2) range of element
[0, n]

solution 1)
iterate through the array
save to hash set
iterate i from 0 to n
check if exists

tc : O(n) sc : O(n)

solution 2)
iterate throught the array
add all the elements
get sum of integer sequence 0 .. n with. n(n+1)/2
get subtraction
tc : O(n) sc : O(1)
*/
class Solution {
public int missingNumber(int[] nums) {
int sum = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

totalSum sum의 차이를 이용하여 nums를 정렬하지 않고 최소한의 시간 복잡도로 누락된 숫자를 구하는 효율적인 구조가 인상깊습니다.

만약 n이 매우 큰 숫자인 경우를 고려한다면, n(n+1)/2 계산에서 오버플로우가 발생할 수 있으니 long 타입으로 안전하게 구현해볼 수도 있습니다.

int n = nums.length;
for(int num : nums) {
sum += num;
}
int totalSum = (n+1) * n / 2;
return totalSum - sum;
}
}
23 changes: 23 additions & 0 deletions palindromic-substrings/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
input : string s
output: the number of palindromic substrings of given string
tc : O(n^2) sc : o(n^2) when n is the length of string s

optimize?
maybe nope.. atleast n^2 to select i and j

*/
class Solution {
public int countSubstrings(String s) {
int n = s.length();
int cnt = 0;
boolean[][] dp = new boolean[n][n];
for(int i = n-1; i >= 0; i--) {
for(int j = i; j < n; j++) {
dp[i][j] = s.charAt(i) == s.charAt(j) && (j-i +1 < 3 || dp[i+1][j-1]);
if(dp[i][j]) cnt++;
}
}
return cnt;
}
}
58 changes: 58 additions & 0 deletions word-search/ekgns33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
input : m x n matrix and string word
output : return true if given word can be constructed

solution 1) brute force
tc : O(n * 4^k) when n is the number of cells, k is the length of word
sc : O(k) call stack
*/
class Solution {
private int[][] directions = new int[][] {{-1,0}, {1,0}, {0,1}, {0,-1}};
public boolean exist(char[][] board, String word) {
//edge case
int m = board.length;
int n = board[0].length;

if(m * n < word.length()) return false;


//look for the starting letter and do dfs
for(int i = 0; i < m; i++) {

for(int j = 0; j < n; j++) {

if(board[i][j] == word.charAt(0)) {
//do dfs and get answer
board[i][j] = '0';
boolean res = dfsHelper(board, word, i, j, 1);
board[i][j] = word.charAt(0);
if(res) return true;
}
}
}

return false;
}

public boolean dfsHelper(char[][] board, String word, int curR, int curC, int curP) {

//endclause
if(curP == word.length()) return true;

boolean ret = false;

for(int[] direction : directions) {
int nextR = curR + direction[0];
int nextC = curC + direction[1];

if(nextR < 0 || nextR >= board.length || nextC < 0 || nextC >= board[0].length) continue;

if(board[nextR][nextC] == word.charAt(curP)) {
board[nextR][nextC] = '0';
ret = ret || dfsHelper(board, word, nextR, nextC, curP + 1);
board[nextR][nextC] = word.charAt(curP);
}
}
return ret;
}
}
Loading