From a89c8f435559713035dbe6ce86db4b60ccabe6f1 Mon Sep 17 00:00:00 2001 From: danny Date: Wed, 29 Jul 2026 23:44:12 +0900 Subject: [PATCH 1/6] 0020-valid-parentheses --- valid-parentheses/dahyeong-yun.java | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 valid-parentheses/dahyeong-yun.java diff --git a/valid-parentheses/dahyeong-yun.java b/valid-parentheses/dahyeong-yun.java new file mode 100644 index 0000000000..f08a079830 --- /dev/null +++ b/valid-parentheses/dahyeong-yun.java @@ -0,0 +1,43 @@ +/** + * [풀이 개요] + * - 시간복잡도 : O(n) + * - 공간복잡도 : O(1) + */ +class Solution { + /** + * [문제 풀이 아이디어] + * - 주어진 문자열의 길이가 최대 10^5 이므로 10^8 까지 통과할 수 있다고 가정할 때, 시간복잡도는 O (n log n)까지 가능해 보임 + * - 문자열 s의 시작과 끝 인덱스가 각각 x, y 라고 할 때 s[x] == s[y], s[x+1] == s[y-1], s[x+2] == s[y-2], s[x+n] == s[y-n] (n은 문자열 중간 인덱스 까지) 와 같이 검증할 수 있음. + * - 즉 투포인터 형태가 될 수 있어 보임. + * - 다만 알파벳이 아닌 문자는 제외하고 판단해야 하므로 포인터가 정확히 같은 값을 x, y에서 빼는 형태가 될 수는 없고, 비교 대상이 아닌 인덱스를 넘어가서 다음에 판단해야 함. + * - 이렇게 순회할 경우 문자열 길이 n의 1/2 를 순회하므로 시간복잡도는 O(n) 이 됨. + * - 매 char 변수 이외에 추가 공간이 필요치 않으므로 공간복잡도는 O(1) 이 됨. + */ + public boolean isPalindrome(String s) { + int len = s.length(); + if(len == 1) return true; + + int left = 0; + int right = len - 1; + + while(left < right) { + char leftChar = Character.toLowerCase(s.charAt(left)); + char rightChar = Character.toLowerCase(s.charAt(right)); + + // 문자가 아닌 경우 다음 인덱스 확인 + if(!Character.isLetterOrDigit(leftChar)) { + left++; + } else if(!Character.isLetterOrDigit(rightChar)) { + right--; + + // 동일한 경우 다음 인덱스 확인 + } else if(leftChar == rightChar) { + left++; + right--; + } else { + return false; + } + } + return true; + } +} From 8042123b53f5db86f959e64dfde398882f37f70b Mon Sep 17 00:00:00 2001 From: danny Date: Wed, 29 Jul 2026 23:48:46 +0900 Subject: [PATCH 2/6] fix --- valid-parentheses/dahyeong-yun.java | 64 +++++++++++++---------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/valid-parentheses/dahyeong-yun.java b/valid-parentheses/dahyeong-yun.java index f08a079830..b003693529 100644 --- a/valid-parentheses/dahyeong-yun.java +++ b/valid-parentheses/dahyeong-yun.java @@ -1,43 +1,37 @@ /** - * [풀이 개요] - * - 시간복잡도 : O(n) - * - 공간복잡도 : O(1) - */ + * TC : O(n) + * - 문자열 s의 길이 만큼 순회 하므로 O(n) + * SC : O(n) + * - 문자열 길이 만큼의 char 배열을 생성하고, 최대 n/2 만큼의 스택 사용. 합산 1.5n 이므로 O(n) + */ class Solution { - /** - * [문제 풀이 아이디어] - * - 주어진 문자열의 길이가 최대 10^5 이므로 10^8 까지 통과할 수 있다고 가정할 때, 시간복잡도는 O (n log n)까지 가능해 보임 - * - 문자열 s의 시작과 끝 인덱스가 각각 x, y 라고 할 때 s[x] == s[y], s[x+1] == s[y-1], s[x+2] == s[y-2], s[x+n] == s[y-n] (n은 문자열 중간 인덱스 까지) 와 같이 검증할 수 있음. - * - 즉 투포인터 형태가 될 수 있어 보임. - * - 다만 알파벳이 아닌 문자는 제외하고 판단해야 하므로 포인터가 정확히 같은 값을 x, y에서 빼는 형태가 될 수는 없고, 비교 대상이 아닌 인덱스를 넘어가서 다음에 판단해야 함. - * - 이렇게 순회할 경우 문자열 길이 n의 1/2 를 순회하므로 시간복잡도는 O(n) 이 됨. - * - 매 char 변수 이외에 추가 공간이 필요치 않으므로 공간복잡도는 O(1) 이 됨. - */ - public boolean isPalindrome(String s) { - int len = s.length(); - if(len == 1) return true; - - int left = 0; - int right = len - 1; + public boolean isValid(String s) { + Deque stack = new ArrayDeque<>(); - while(left < right) { - char leftChar = Character.toLowerCase(s.charAt(left)); - char rightChar = Character.toLowerCase(s.charAt(right)); - - // 문자가 아닌 경우 다음 인덱스 확인 - if(!Character.isLetterOrDigit(leftChar)) { - left++; - } else if(!Character.isLetterOrDigit(rightChar)) { - right--; - - // 동일한 경우 다음 인덱스 확인 - } else if(leftChar == rightChar) { - left++; - right--; + for(char c : s.toCharArray()) { + //여는 괄호의 경우 stack 에 쌓기 + if(c == '{' + || c == '(' + || c == '[' + ) { + stack.push(c); } else { - return false; + // 닫는 괄호의 경우 stack이 비어있으면 안됨 + if(stack.size() < 1) return false; + else { + char peek = stack.peek(); + if((peek == '(' && c == ')') + || (peek == '{' && c == '}') + || (peek == '[' && c == ']') + ) { + stack.pop(); + } else { + return false; + } + } } } - return true; + + return stack.size() == 0; } } From c3741b7f20d868f7a60dfdef73c5c125a6ef05a6 Mon Sep 17 00:00:00 2001 From: danny Date: Fri, 31 Jul 2026 17:18:46 +0900 Subject: [PATCH 3/6] 0300-longest-increasing-subsequence --- .../dahyeong-yun.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 longest-increasing-subsequence/dahyeong-yun.java diff --git a/longest-increasing-subsequence/dahyeong-yun.java b/longest-increasing-subsequence/dahyeong-yun.java new file mode 100644 index 0000000000..3151b84080 --- /dev/null +++ b/longest-increasing-subsequence/dahyeong-yun.java @@ -0,0 +1,30 @@ +/** + * TC : O(n^2) + * - 숫자 배열의 길이가 n 일 때 바깥 쪽 루프의 인덱스가 하나씩 증가함에 따라 때 안쪽 루프는 각각 1번, 2번, n-1번 까지 순회함 + * - 따라서 총 반복 횟수는 1 + 2 + ... + n-1 + * - 등차급수 합계 공식에 따라 n(n-1) / 2 만큼 반복하므로 시간복잡도는 최고 차항만 고려하여 n^2 + * SC : O(n) + * - 숫자 배열 길이 n 만큼의 추가 배열을 공간을 생성하므로 O(n) + */ +class Solution { + public int lengthOfLIS(int[] nums) { + int len = nums.length; + int[] dp = new int[len]; // 최대 길이는 input 의 길이 만큼임 + Arrays.fill(dp, 1); // 모든 길이는 최소 1 + + for(int i=1; i Date: Fri, 31 Jul 2026 21:06:22 +0900 Subject: [PATCH 4/6] 0011-container-with-most-water --- container-with-most-water/dahyeong-yun.java | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 container-with-most-water/dahyeong-yun.java diff --git a/container-with-most-water/dahyeong-yun.java b/container-with-most-water/dahyeong-yun.java new file mode 100644 index 0000000000..c7c75821cd --- /dev/null +++ b/container-with-most-water/dahyeong-yun.java @@ -0,0 +1,26 @@ +/** + * TC : O(n) + * - 기둥 높이 배열의 길이 n에 대해 최악의 경우 전체를 순회 하므로 O(n) + * SC : O(1) + * - 별도로 유의미한 공간 생성이 없음 + */ +class Solution { + /** + * 넗이의 조합을 다 해보고 max 값을 찾으면 됨 + * 그 중에 안해도 되는 조합이 있음. 더 낮은 높이로 인해서 넓이가 결정되므로 더 높은 탑은 그냥 유지하고 다른 조합만 확인해보면 됨. + */ + public int maxArea(int[] height) { + int left = 0, right = height.length-1, max = 0; + while(left < right) { + int w = right - left; + int h = height[left] < height[right] ? height[left] : height[right]; + + int current = w * h; + max = Math.max(max, current); + + if(height[left] > height[right]) right --; + else left ++; + } + return max; + } +} From f129e9980919c249d31844683ca39707ae8ae799 Mon Sep 17 00:00:00 2001 From: danny Date: Fri, 31 Jul 2026 21:58:33 +0900 Subject: [PATCH 5/6] 0054-spiral-matirx --- spiral-matrix/dahyeong-yun.java | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spiral-matrix/dahyeong-yun.java diff --git a/spiral-matrix/dahyeong-yun.java b/spiral-matrix/dahyeong-yun.java new file mode 100644 index 0000000000..78022bbe80 --- /dev/null +++ b/spiral-matrix/dahyeong-yun.java @@ -0,0 +1,41 @@ +/** + * TC : O(m * n) + * - matrix의 원소 수 m * n 만큼 순회 하므로 O(m * n) + * SC : O(m * n) + * - matrix의 원소 수 m * n 만큼의 리스트를 생성하므로 O(m * n) + */ +class Solution { + public List spiralOrder(int[][] matrix) { + int[] dRow = {0, 1, 0, -1}; + int[] dCol = {1, 0, -1, 0}; + + int direction = 0; + List list = new ArrayList<>(); + + int rowCount = matrix.length; + int colCount = matrix[0].length; + + int row = 0, col = 0; + int totalElements = rowCount * colCount; + + for (int i = 0; i < totalElements; i++) { + list.add(matrix[row][col]); + matrix[row][col] = 101; // 방문 처리 (제약조건: -100 <= val <= 100) + + int nextRow = row + dRow[direction]; + int nextCol = col + dCol[direction]; + + // 범위를 벗어나거나 이미 방문한 칸인 경우 방향 전환 + if (nextRow < 0 || nextRow >= rowCount || + nextCol < 0 || nextCol >= colCount || + matrix[nextRow][nextCol] > 100) { + + direction = (direction + 1) % 4; + } + + row += dRow[direction]; + col += dCol[direction]; + } + return list; + } +} \ No newline at end of file From 15b3c698fe252524e5beb294fbb7b6537f5e5260 Mon Sep 17 00:00:00 2001 From: danny Date: Fri, 31 Jul 2026 22:00:35 +0900 Subject: [PATCH 6/6] add newline --- spiral-matrix/dahyeong-yun.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spiral-matrix/dahyeong-yun.java b/spiral-matrix/dahyeong-yun.java index 78022bbe80..03708026e1 100644 --- a/spiral-matrix/dahyeong-yun.java +++ b/spiral-matrix/dahyeong-yun.java @@ -38,4 +38,4 @@ public List spiralOrder(int[][] matrix) { } return list; } -} \ No newline at end of file +}