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; + } +} 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 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; + } +} diff --git a/valid-parentheses/dahyeong-yun.java b/valid-parentheses/dahyeong-yun.java new file mode 100644 index 0000000000..b003693529 --- /dev/null +++ b/valid-parentheses/dahyeong-yun.java @@ -0,0 +1,37 @@ +/** + * TC : O(n) + * - 문자열 s의 길이 만큼 순회 하므로 O(n) + * SC : O(n) + * - 문자열 길이 만큼의 char 배열을 생성하고, 최대 n/2 만큼의 스택 사용. 합산 1.5n 이므로 O(n) + */ +class Solution { + public boolean isValid(String s) { + Deque stack = new ArrayDeque<>(); + + for(char c : s.toCharArray()) { + //여는 괄호의 경우 stack 에 쌓기 + if(c == '{' + || c == '(' + || c == '[' + ) { + stack.push(c); + } else { + // 닫는 괄호의 경우 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 stack.size() == 0; + } +}