diff --git a/container-with-most-water/eunhwa99.java b/container-with-most-water/eunhwa99.java new file mode 100644 index 000000000..2557f0c44 --- /dev/null +++ b/container-with-most-water/eunhwa99.java @@ -0,0 +1,23 @@ +// two pointer + +// 시간 복잡도: O(n) - 투 포인터 +// 공간 복잡도: O(n) - height 배열 크기 + +class Solution { + public int maxArea(int[] height) { + int left=0; + int right = height.length-1; + + int maxContainer = 0; + while(left O(N*M) +// 공간 복잡도: O(M) +class WordDictionary { + + Set wordSet; + public WordDictionary() { + wordSet = new HashSet<>(); + } + + public void addWord(String word) { + wordSet.add(word); + } + + public boolean search(String word) { + if(word.contains(".")){ + char[] wordArr = word.toCharArray(); + boolean found = false; + for(String w: wordSet){ + if(word.length()!=w.length()) continue; + + char[] charArr = w.toCharArray(); + for(int i=0;i O(26^N * N) +// 공간 복잡도: O(N * M) - M(단어 개수) +class TrieNode { + TrieNode[] child; + boolean isEnd; // flag if the node is end. + + public TrieNode() { + child = new TrieNode[26]; // 알파벳 개수 + isEnd = false; + } +} + +class WordDictionary { + TrieNode root; + + public WordDictionary() { + root = new TrieNode(); // trie 루트 생성 + } + + public void addWord(String word) { + TrieNode cur = root; + for (char w : word.toCharArray()) { + if (cur.child[w - 'a'] == null) { + cur.child[w - 'a'] = new TrieNode(); // 자식 생성 + } + cur = cur.child[w - 'a']; + } + cur.isEnd = true; + } + + public boolean search(String word) { + return dfs(root, word, 0); // dfs 호출 시, index도 전달 + } + + // dfs를 수행하며, 현재 인덱스까지 탐색한 상태에서 단어를 검색 + private boolean dfs(TrieNode cur, String word, int index) { + // 단어 끝에 도달했으면, 현재 노드가 끝을 나타내는지 확인 + if (index == word.length()) { + return cur.isEnd; + } + + char c = word.charAt(index); + + // '.' 이면 모든 자식 노드에 대해 재귀적으로 탐색 + if (c == '.') { + for (TrieNode child : cur.child) { + if (child != null && dfs(child, word, index + 1)) { + return true; + } + } + return false; + } else { + // '.'이 아닌 경우, 해당 문자에 해당하는 자식으로 계속 내려감 + if (cur.child[c - 'a'] == null) { + return false; + } + return dfs(cur.child[c - 'a'], word, index + 1); + } + } +} + + diff --git a/longest-increasing-subsequence/eunhwa99.java b/longest-increasing-subsequence/eunhwa99.java new file mode 100644 index 000000000..d8cddfab3 --- /dev/null +++ b/longest-increasing-subsequence/eunhwa99.java @@ -0,0 +1,56 @@ +class Solution { + + // lenArr[i] : i를 마지막 원소로 하는 최장 부분 수열 길이 + // 시간 복잡도: O(N*N) -> 이중 반복문 + // 공간 복잡도: O(N) -> 배열 크기 + public int lengthOfLIS(int[] nums) { + int[] lenArr = new int[nums.length]; + for(int i=0;inums[j]){ + lenArr[i] = Math.max(lenArr[j] +1, lenArr[i]); + // j번째 숫자를 수열에 포함시키거나 포함시키지 않음 -> 두 개 비교해서 Max 값 찾는다. + } + } + } + + int result = 0; + for(int i=0;i BinarySearch 로도 가능하다...! +// 시간 복잡도 : O(NlogN) +// 공간 복잡도: O(N) +class Solution { + public int lengthOfLIS(int[] nums) { + int[] list = new int[nums.length]; + int j = -1; + for(int i=0;i 따라서 left = mid + 1로 범위를 좁힌다. + else right = mid; + } + list[left] = nums[i]; + + } + } + + return j + 1; + } + +} + diff --git a/spiral-matrix/eunhwa99.java b/spiral-matrix/eunhwa99.java new file mode 100644 index 000000000..fe7eb8284 --- /dev/null +++ b/spiral-matrix/eunhwa99.java @@ -0,0 +1,48 @@ +// 시간 복잡도: O(N*N) +// 공간 복잡도: O(N*N) + +// 이동하는 방향을 바꾸는 조건 (아래 중 하나 만족) +// 1. 다음에 갈 곳이 이미 방문한 곳 +// 2. 다음에 갈 곳이 배열 범위를 벗어난 곳 +class Solution { + public List spiralOrder(int[][] matrix) { + int row = matrix.length; + int col = matrix[0].length; + boolean[][] visited = new boolean[row][col]; + + // right, down, left, up + int[] dirY = new int[]{1,0,-1,0}; // 열 방향 + int[] dirX = new int[]{0,1,0,-1}; // 행 방향 + int dirPointer = 0; + + List result = new ArrayList<>(); + + int x = 0, y = 0; + int cnt = 0; int total = row*col; + while(cnt < total){ + + result.add(matrix[x][y]); + visited[x][y]=true; + ++cnt; + // 다음 좌표로 이동 + int nextX = x + dirX[dirPointer]; + int nextY = y + dirY[dirPointer]; + + // 경계 조건 체크 및 방향 전환 + if (nextX < 0 || nextX >= row || nextY < 0 || nextY >= col || visited[nextX][nextY]) { + // 현재 방향에서 벗어나면 방향을 변경 + dirPointer = (dirPointer + 1) % 4; + nextX = x + dirX[dirPointer]; + nextY = y + dirY[dirPointer]; + } + + // 좌표 업데이트 + x = nextX; + y = nextY; + } + + + return result; + } +} + diff --git a/valid-parentheses/eunhwa99.java b/valid-parentheses/eunhwa99.java new file mode 100644 index 000000000..91197d431 --- /dev/null +++ b/valid-parentheses/eunhwa99.java @@ -0,0 +1,37 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.Stack; + +// 열린 괄호일 경우. 스택에 푸쉬 +// 닫힌 괄호일 경우, 스택의 top 부분이 같은 종류의 열린 괄호이면 pop, 다른 종류 열린 괄호이면 invalid한 문자열 + +// 시간 복잡도: 스택의 크기 = 문자열의 크기 O(N) +// 공간 복잡도: 스택의 크기 = O(N) +class Solution { + public boolean isValid(String s) { + char[] array = s.toCharArray(); + Map pMap = new HashMap<>(); + pMap.put(')','('); + pMap.put('}','{'); + pMap.put(']','['); + + Stack stack = new Stack<>(); + for(char parenthes: array){ + if((parenthes == ')') || (parenthes=='}') || (parenthes==']')){ + if(stack.isEmpty()) return false; // invalid + + char myPair = pMap.get(parenthes); + if(stack.peek()==myPair){ + stack.pop(); + } + else return false; + } + else{ + stack.push(parenthes); + } + } + + return stack.empty(); + } +} +