Skip to content

[ackku] Week 4 #822

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 3 commits into from
Jan 4, 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
21 changes: 21 additions & 0 deletions coin-change/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 비슷한 문제를 푼 적이 있어서 쉽게 해결
// https://www.acmicpc.net/problem/2294
// O(N * amount) 시간복잡도가 배열 크기와 amount에 종속된다.
// dp[N]만 사용하므로 공간복잡도는 O(N)
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];
}
}
16 changes: 16 additions & 0 deletions merge-two-sorted-lists/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 처음에 여러 조건을 붙였으나 기본적인 조건만 필요하다.
// 가장 기본적인 개념은 다음에 이동할곳이 어딘지만 알려주면 됨
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;

if (list1.val <= list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
}
}
34 changes: 34 additions & 0 deletions missing-number/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// GPT의 도움을 받은 결과, 수학적으로 접근하면 된다.
// 모든 값은 유니크하고 nums 배열 사이즈인 n을 지켜주기 때문에 가능한 결과
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int expected = n * (n + 1) / 2;
int actual = 0;
for (int num : nums) {
actual += num;
}
return expected - actual;
}
}

// 시간복잡도는 O(N)으로 떨어진다.
// 공간복잡도가 nums 배열 사이즈에 종속되서 O(N)이다.
// Accepted가 되지만, 다른 방법을 찾아봐야함
class Solution {
public int missingNumber(int[] nums) {
boolean[] existCheck = new boolean[nums.length + 1];

for (int num : nums) {
existCheck[num] = true;
}

for (int i = 0; i < existCheck.length; i++) {
if (!existCheck[i]) {
return i;
}
}

return 0;
}
}
31 changes: 31 additions & 0 deletions palindromic-substrings/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// O(N^2) 이 나올 수밖에 없는 문제. 이런 문제의 특징은 N의 크기가 작다.
// 이번문제도 N의 크기가 1000으로 주어졌을때, 이차원 for문이 허용된다는걸 간접적으로 알아챌 수 있다.
// 이차원 배열 이므로 공간복잡도도 O(N^2)
class Solution {
public int countSubstrings(String s) {
int n = s.length();
boolean[][] dp = new boolean[n][n];
int count = 0;

for (int i = 0; i < n; i++) {
dp[i][i] = true;
count++;
}

for (int len = 2; len <= n; len++) {
for (int i = 0; i <= n - len; i++) { // 시작 위치
int j = i + len - 1; // 끝 위치

// 양 끝 문자가 같고, 내부가 회문이거나 길이가 2인 경우
if (s.charAt(i) == s.charAt(j)) {
if (len == 2 || dp[i + 1][j - 1]) {
dp[i][j] = true;
count++;
}
}
}
}

return count;
}
}
44 changes: 44 additions & 0 deletions word-search/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 189ms가 나와서 다시 시도해볼 예정
class Solution {
private int[] rowMove = {1, -1, 0, 0};
private int[] colMove = {0, 0, 1, -1};

public boolean exist(char[][] board, String word) {
int rows = board.length;
int cols = board[0].length;

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (dfs(board, word, i, j, 0)) {
return true;
}
}
}
return false;
}

private boolean dfs(char[][] board, String word, int row, int col, int index) {
if (index == word.length()) {
return true;
}

if (row < 0 || col < 0 || row >= board.length || col >= board[0].length || board[row][col] != word.charAt(index)) {
return false;
}

char temp = board[row][col];
board[row][col] = '#';

for (int i = 0; i < 4; i++) {
int newRow = row + rowMove[i];
int newCol = col + colMove[i];
if (dfs(board, word, newRow, newCol, index + 1)) {
return true;
}
}

board[row][col] = temp;

return false;
}
}
Loading