diff --git a/container-with-most-water/Tessa1217.java b/container-with-most-water/Tessa1217.java new file mode 100644 index 000000000..519b5b42a --- /dev/null +++ b/container-with-most-water/Tessa1217.java @@ -0,0 +1,29 @@ +/** + * 정수 배열 height가 주어질 때 물을 최대한 담을 수 있는 두 개의 라인을 찾으세요. + 힌트1. 투 포인터를 활용해서 찾으세요. + */ +class Solution { + + // 시간 복잡도: O(n) + public int maxArea(int[] height) { + + int max = 0; + int start = 0; + int end = height.length - 1; + + while (start < end) { + + int area = Math.min(height[start], height[end]) * (end - start); + max = Math.max(area, max); + + if (height[start] < height[end]) { + start++; + } else { + end--; + } + } + + return max; + } +} + diff --git a/design-add-and-search-words-data-structure/Tessa1217.java b/design-add-and-search-words-data-structure/Tessa1217.java new file mode 100644 index 000000000..94f2c2805 --- /dev/null +++ b/design-add-and-search-words-data-structure/Tessa1217.java @@ -0,0 +1,79 @@ +import java.util.HashMap; +import java.util.Map; + +/** + * WordDictionary + * WordDictionary() - 객체 초기화 + * void addWord(word) - 자료 구조에 word 추가 + * boolean search(word) - 자료 구조에 word 있으면 true 아니면 false 반환 + - '.'는 와일드카드로 활용 가능 + */ +class WordDictionary { + + private Node root; + + public WordDictionary() { + root = new Node(); + } + + public void addWord(String word) { + Node current = root; + for (char c : word.toCharArray()) { + if (!current.nodeCharacters.containsKey(c)) { + current.nodeCharacters.put(c, new Node()); + } + current = current.nodeCharacters.get(c); + } + current.isEnd = true; + } + + public boolean search(String word) { + return searchInNode(word, 0, root); + } + + private boolean searchInNode(String word, int idx, Node node) { + + if (node == null) { + return false; + } + + if (idx == word.length()) { + return node.isEnd; + } + + char c = word.charAt(idx); + // 와일드카드는 Node에 있는 전체 값 다 탐색 + if (c == '.') { + for (Node child : node.nodeCharacters.values()) { + if (searchInNode(word, idx + 1, child)) { + return true; + } + } + return false; + } else { + Node current = node.nodeCharacters.get(c); + return searchInNode(word, idx + 1, current); + } + } + + static class Node { + + boolean isEnd; + + Map nodeCharacters; + + Node() { + nodeCharacters = new HashMap<>(); + isEnd = false; + } + + } +} + +/** + * Your WordDictionary object will be instantiated and called as such: + * WordDictionary obj = new WordDictionary(); + * obj.addWord(word); + * boolean param_2 = obj.search(word); + */ + diff --git a/longest-increasing-subsequence/Tessa1217.java b/longest-increasing-subsequence/Tessa1217.java new file mode 100644 index 000000000..ea36bfce5 --- /dev/null +++ b/longest-increasing-subsequence/Tessa1217.java @@ -0,0 +1,27 @@ +/** + * 정수 배열 nums가 주어질 때 가장 길게 증가하는 부분 수열의 길이를 찾으세요. (LIS) + */ +class Solution { + + // 시간복잡도: O(n^2) + public int lengthOfLIS(int[] nums) { + + int[] dp = new int[nums.length]; + int n = nums.length; + int max = 0; + + // 원소의 위치에서 가질 수 있는 LIS의 값 계산 + for (int i = 0; i < n; i++) { + dp[i] = 1; + for (int j = 0; j < i; j++) { + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + max = Math.max(dp[i], max); + } + + return max; + } +} + diff --git a/spiral-matrix/Tessa1217.java b/spiral-matrix/Tessa1217.java new file mode 100644 index 000000000..235111ce3 --- /dev/null +++ b/spiral-matrix/Tessa1217.java @@ -0,0 +1,53 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * m * n 행렬을 시계 방향 순대로 요소를 정렬하여 반환하세요. + */ +class Solution { + + public List spiralOrder(int[][] matrix) { + + List spirals = new ArrayList<>(); + + int top = 0; + int bottom = matrix.length - 1; + int start = 0; + int end = matrix[0].length - 1; + + while (top <= bottom && start <= end) { + // 우측 + for (int i = start; i <= end; i++) { + spirals.add(matrix[top][i]); + } + top++; + + // 아래 + for (int i = top; i <= bottom; i++) { + spirals.add(matrix[i][end]); + } + end--; + + // 좌측 + if (top <= bottom) { + for (int i = end; i >= start; i--) { + spirals.add(matrix[bottom][i]); + } + bottom--; + } + + // 위 + if (start <= end) { + for (int i = bottom; i >= top; i--) { + spirals.add(matrix[i][start]); + } + start++; + } + + } + + return spirals; + } + +} + diff --git a/valid-parentheses/Tessa1217.java b/valid-parentheses/Tessa1217.java new file mode 100644 index 000000000..e99cd78b4 --- /dev/null +++ b/valid-parentheses/Tessa1217.java @@ -0,0 +1,73 @@ +/** + * 유효한 괄호: '(', ')', '{', '}', '[', ']'로 이루어진 문자열 s가 주어질 때 문자열 s가 유효한 괄호로 이루어진 문자열인지 판별하세요. + 유요한 문자열인지의 판별 방법 + 1. 여는 괄호는 반드시 같은 종류의 닫는 괄호에 의해 닫혀야 한다. + 2. 여는 괄호는 반드시 정확한 순서로 닫혀야 한다. + 3. 모든 닫는 괄호는 같은 종류의 여는 괄호에 의해서 열려야 한다. + */ +class Solution { + + // 시간복잡도: O(n) + public boolean isValid(String s) { + + Stack parentheses = new Stack<>(); + // 맵으로 조건문 간소화 + Map parenthesisMap = Map.of(')', '(', '}', '{', ']', '['); + + for (char c : s.toCharArray()) { + if (c == '(' || c == '{' || c == '[') { + parentheses.push(c); + } else if (parentheses.isEmpty() || parentheses.pop() != parenthesisMap.get(c)) { // 전체 다 돌지 않고 유효한 괄호 아니면 사전에 불린 반환 + return false; + } + } + + return parentheses.size() == 0; + + } + + // public boolean isValid(String s) { + + // Stack parentheses = new Stack<>(); + // // 맵으로 조건문 간소화 + // Map parenthesisMap = Map.of(')', '(', '}', '{', ']', '['); + + // for (char c : s.toCharArray()) { + // if (parentheses.isEmpty()) { + // parentheses.push(c); + // continue; + // } + // if (parentheses.peek() == parenthesisMap.get(c)) { + // parentheses.pop(); + // } else { + // parentheses.push(c); + // } + // } + + // return parentheses.size() == 0; + + // } + + // public boolean isValid(String s) { + + // Stack parentheses = new Stack<>(); + + // for (char c : s.toCharArray()) { + // if (parentheses.isEmpty()) { + // parentheses.push(c); + // continue; + // } + // if ((parentheses.peek() == '(' && c == ')') || + // (parentheses.peek() == '{' && c == '}') || + // (parentheses.peek() == '[' && c == ']')) { + // parentheses.pop(); + // } else { + // parentheses.push(c); + // } + // } + + // return parentheses.size() == 0; + + // } +} +