diff --git a/container-with-most-water/jinhyungrhee.java b/container-with-most-water/jinhyungrhee.java new file mode 100644 index 000000000..ab87840c3 --- /dev/null +++ b/container-with-most-water/jinhyungrhee.java @@ -0,0 +1,32 @@ +class Solution { + + /** + * time-complexity : O(n) + * space-complexity : O(1) + */ + + public int maxArea(int[] height) { + + int left = 0; + int right = height.length - 1; + + int w = 0, h = 0, currSize = 0, maxSize = 0; + + while (left < right) { + + w = right - left; + h = Math.min(height[left], height[right]); + + currSize = w * h; + maxSize = Math.max(currSize, maxSize); + + if (height[left] < height[right]) { + left++; + } else { + right--; + } + + } + return maxSize; + } +} diff --git a/design-add-and-search-words-data-structure/jinhyungrhee.java b/design-add-and-search-words-data-structure/jinhyungrhee.java new file mode 100644 index 000000000..1cf377293 --- /dev/null +++ b/design-add-and-search-words-data-structure/jinhyungrhee.java @@ -0,0 +1,75 @@ +class TrieNode { + boolean word; + TrieNode[] children; + + TrieNode() { + this.word = false; + this.children = new TrieNode[27]; + } +} + +class WordDictionary { + /** + * space-complexity : O(N * L) (w.c) + * - N : 단어 수, L : 평균 길이 + * - 단어 하나 추가 시 최대 L개의 TrieNode 생성 + * - (w.c) 모든 단어가 중복 없이 추가되면 (N*L)개의 노드 필요 + */ + TrieNode root; + public WordDictionary() { + this.root = new TrieNode(); + } + + /** + * addWord() Time-complexity : O(L) + * - 각 문자마다 TrieNode를 따라 내려가며 필요한 경우 새 노드 생성 + * - 한 단어당 최대 L개의 노드 생성 및 접근 + */ + public void addWord(String word) { + + TrieNode curr = root; + for (char c : word.toCharArray()) { + if (curr.children[c - 'a'] == null) { + curr.children[c - 'a'] = new TrieNode(); + } + curr = curr.children[c - 'a']; + } + curr.word = true; + } + + /** + * search() Time-complexity : + * - '.'가 없는 경우 : O(L) + * -> 일반적인 문자는 한 경로만 탐색 + * - '.'가 있는 경우 : O(26^L) (w.c) + * -> 현재 노드의 모든 자식에 대해 DFS 수행 + * + */ + public boolean search(String word) { + return dfs(word, 0, root); + } + + public boolean dfs(String word, int index, TrieNode node) { + + if (index == word.length()) { + return node.word; + } + + char c = word.charAt(index); + + if (c == '.') { + for (TrieNode child : node.children) { + if (child != null && dfs(word, index + 1, child)) { + return true; + } + } + return false; + + } else { + + TrieNode next = node.children[c - 'a']; + return next != null && dfs(word, index + 1, next); + + } + } +} diff --git a/longest-increasing-subsequence/jinhyungrhee.java b/longest-increasing-subsequence/jinhyungrhee.java new file mode 100644 index 000000000..e5d5a6b64 --- /dev/null +++ b/longest-increasing-subsequence/jinhyungrhee.java @@ -0,0 +1,27 @@ +import java.util.*; +class Solution { + /** + * time-complexity : O(n^2) + * space-complexity : O(n) + */ + public int lengthOfLIS(int[] nums) { + + int[] dp = new int[nums.length]; + Arrays.fill(dp, 1); + + for (int i = 1 ; i < nums.length; i++) { + for (int j = i; j >= 0; j--) { + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + + int maxVal = 0; + for (int num : dp) { + if (num > maxVal) maxVal = num; + } + + return maxVal; + } +} diff --git a/spiral-matrix/jinhyungrhee.java b/spiral-matrix/jinhyungrhee.java new file mode 100644 index 000000000..b119725af --- /dev/null +++ b/spiral-matrix/jinhyungrhee.java @@ -0,0 +1,53 @@ +import java.util.*; +class Solution { + /** + * time-complexity : O(n * m) + * space-complexity : O(1) (excluding the output List) + */ + public List spiralOrder(int[][] matrix) { + + List result = new ArrayList<>(); + + int left = 0; + int right = matrix[0].length - 1; + int top = 0; + int bottom = matrix.length - 1; + + while (left <= right && top <= bottom) { + + // 위쪽 행을 순회한 후, 상단 경계를 1 증가 + for (int y = left; y <= right; y++) { + result.add(matrix[top][y]); + } + top++; + + // 상단 인덱스가 하단 인덱스 보다 커지면 순회 중단 + if (top > bottom) break; + + // 우측 열을 순회한 후, 우측 경계를 1 감소 + for (int x = top; x <= bottom; x++) { + result.add(matrix[x][right]); + } + right--; + + // 우측 인덱스가 좌측 인덱스보다 작아지면 순회 중단 + if (right < left) break; + + // 아래쪽 행을 순회한 후, 하단 경계를 1 감소 + for (int y = right; y >= left; y--) { + result.add(matrix[bottom][y]); + } + bottom--; + + // 왼쪽 열을 순회한 후, 좌측 경계를 1 증가 + for (int x = bottom; x >= top; x--) { + result.add(matrix[x][left]); + } + left++; + + } + return result; + + } + +} diff --git a/valid-parentheses/jinhyungrhee.java b/valid-parentheses/jinhyungrhee.java new file mode 100644 index 000000000..a60a05a74 --- /dev/null +++ b/valid-parentheses/jinhyungrhee.java @@ -0,0 +1,31 @@ +import java.util.*; +class Solution { + /** + * time-complexity : O(n) + * space-complexity : O(n) + */ + public boolean isValid(String s) { + + Deque stack = new ArrayDeque<>(); + Map table = new HashMap<>(); + table.put(')', '('); + table.put(']', '['); + table.put('}', '{'); + + for (int i = 0; i < s.length(); i++) { + if (table.containsKey(s.charAt(i))) { + + if ((table.get(s.charAt(i))).equals(stack.peek())) { + stack.pop(); + } else { + stack.push(s.charAt(i)); + } + + } else { + stack.push(s.charAt(i)); + } + } + + return stack.isEmpty(); + } +}