diff --git a/container-with-most-water/se6816.java b/container-with-most-water/se6816.java new file mode 100644 index 0000000000..a226f29aa4 --- /dev/null +++ b/container-with-most-water/se6816.java @@ -0,0 +1,47 @@ +/** + 브루트포스를 통해 모든 경우의 수를 연산하여 찾는 방식 + 배열 height의 길이 -> N + 시간 복잡도 : O(N^2) -> 시간 초과 + 공간 복잡도 : O(1) +*/ +class FailSolution { + public int maxArea(int[] height) { + int max=0; + int area=0; + for(int i=0;imax){ + max=area; + } + } + } + return max; + } +} + +/** + 투포인터를 활용하여, 낮은 높이의 포인터를 갱신하면서 최대 넓이를 구하는 방식 + 배열 height의 길이 -> N + 시간 복잡도 : O(N) + 공간 복잡도 : O(1) +*/ +class Solution { + public int maxArea(int[] height) { + int left = 0; + int right = height.length - 1; + int max = 0; + while(left < right) { + int h = Math.min(height[right], height[left]); + int w = right - left; + max = Math.max(max, w * h); + + if(h == height[right]) { + right--; + } else { + left++; + } + } + return max; + } +} diff --git a/design-add-and-search-words-data-structure/se6816.java b/design-add-and-search-words-data-structure/se6816.java new file mode 100644 index 0000000000..c498fe9982 --- /dev/null +++ b/design-add-and-search-words-data-structure/se6816.java @@ -0,0 +1,116 @@ +/** + Tries + BFS를 활용한 방식 + addWord() + 문자열 word의 길이 -> N + 시간 복잡도 : O(N) + 공간 복잡도 : O(N) + search() + Tries 내부의 노드 개수 -> M + 시간 복잡도 : O(M) + 공간 복잡도 : O(M) +*/ +class WordDictionary { + public Map wordMap; + + public WordDictionary() { + wordMap = new HashMap<>(); + } + + public void addWord(String word) { + WordNode wordNode = null; + char ch = word.charAt(0); + wordNode = wordMap.get(ch); + + if(wordNode == null) { + boolean isFirstWord = word.length() == 1; + wordNode = new WordNode(ch, isFirstWord); + wordMap.put(ch, wordNode); + } + + for(int idx = 1; idx < word.length(); idx++) { + char target = word.charAt(idx); + boolean isLeaf = word.length() - 1 == idx; + wordNode = wordNode.next.computeIfAbsent(target, key -> new WordNode(key, isLeaf)); + } + wordNode.isLeaf = true; + + + } + + public boolean search(String word) { + Queue que = new ArrayDeque<>(); + boolean result = false; + char ch = word.charAt(0); + WordNode wordNode = wordMap.get(ch); + int len = word.length(); + + if(ch == '.' && wordMap.size() != 0) { + for(Map.Entry entry : wordMap.entrySet()) { + que.add(new Node(entry.getValue(), 1)); + } + } + + if (wordNode != null) { + que.add(new Node(wordNode, 1)); + } + + + + while(!que.isEmpty()) { + Node node = que.poll(); + if(node.idx == len && node.wordNode.isLeaf) { + result = true; + break; + } + + if(node.idx == len) { + continue; + } + + char target = word.charAt(node.idx); + + if(target == '.' && node.wordNode.next.size() != 0) { + for(Map.Entry entry : node.wordNode.next.entrySet()) { + que.add(new Node(entry.getValue(), node.idx + 1)); + } + continue; + } + + + if (!node.wordNode.next.containsKey(target)) { + continue; + } + + que.add(new Node(node.wordNode.next.get(target), node.idx + 1)); + } + + return result; + + } +} + +class Node { + WordNode wordNode; + int idx; + public Node(WordNode wordNode, int idx) { + this.wordNode = wordNode; + this.idx = idx; + } +} + +class WordNode { + char ch; + Map next; + boolean isLeaf; + + public WordNode(char ch) { + this(ch, false); + } + + public WordNode(char ch, boolean isLeaf) { + next = new HashMap<>(); + this.ch = ch; + this.isLeaf = isLeaf; + } + +} diff --git a/longest-increasing-subsequence/se6816.java b/longest-increasing-subsequence/se6816.java new file mode 100644 index 0000000000..eae7619b2b --- /dev/null +++ b/longest-increasing-subsequence/se6816.java @@ -0,0 +1,59 @@ +/** + dp를 활용한 방식 + nums의 길이 -> N + 시간 복잡도 : O(N) + 공간 복잡도 : O(N) +*/ +class Solution { + public int lengthOfLIS(int[] nums) { + int[] dp =new int[nums.length]; + for(int i = 0; i < dp.length; i++) { + for(int j = 0; j < i; j++) { + if(nums[i] > nums[j]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + } + return Arrays.stream(dp) + .max() + .getAsInt() + 1; + } +} + +/** + 이분탐색 활용한 방식 + nums의 길이 -> N + 시간 복잡도 : O(NlogN) + 공간 복잡도 : O(N) +*/ +class Solution { + public int[] list; + public int lengthOfLIS(int[] nums) { + int result = 0; + list =new int[nums.length]; + Arrays.fill(list, Integer.MAX_VALUE); + for(int i = 0; i < list.length; i++) { + int idx = binarySearch(list, nums[i]); + list[idx] = nums[i]; + result = Math.max(result, idx + 1); + + } + return result; + } + + public int binarySearch(int[] list, int target) { + int start = 0; + int end = list.length - 1; + + while(start <= end) { + int mid = (start + end) / 2; + if(list[mid] >= target) { + end = mid - 1; + } else { + start = mid + 1; + } + } + + return start; + } +} diff --git a/spiral-matrix/se6816.java b/spiral-matrix/se6816.java new file mode 100644 index 0000000000..7d3a98d139 --- /dev/null +++ b/spiral-matrix/se6816.java @@ -0,0 +1,55 @@ +/** + 구현을 통해 배열을 읽어들이는 방식 + 시간 복잡도 : O(N*M) + 공간 복잡도 : O(N*M) +*/ +class Solution { + int[] moveY = {1, 0, -1, 0}; + int[] moveX = {0, -1, 0, 1}; + int N; + int M; + public List spiralOrder(int[][] matrix) { + N = matrix.length; + M = matrix[0].length; + boolean[][] visited = new boolean[N][M]; + List result = new ArrayList<>(); + int curX = 0; + int curY = 0; + int direction = 0; + int count = 0; + visited[curX][curY] = true; + result.add(matrix[curX][curY]); + + while(true) { + if(count == 4) { + break; + } + int tempX = curX + moveX[direction]; + int tempY = curY + moveY[direction]; + if(outOfIndex(tempX, tempY)) { + direction = (direction + 1) % 4; + count++; + continue; + } + + if(visited[tempX][tempY]) { + direction = (direction + 1) % 4; + count++; + continue; + } + + curX = tempX; + curY = tempY; + result.add(matrix[curX][curY]); + visited[curX][curY] = true; + + count = 0; + } + + return result; + } + + public boolean outOfIndex(int x, int y) { + return x < 0 || x >= N || y < 0 || y >= M; + } +} diff --git a/valid-parentheses/se6816.java b/valid-parentheses/se6816.java new file mode 100644 index 0000000000..788d7f473e --- /dev/null +++ b/valid-parentheses/se6816.java @@ -0,0 +1,37 @@ +/** + Stack을 활용한 방식 + 문자열 s의 길이 -> N + 시간 복잡도 : O(N) + 공간 복잡도 : O(N) +*/ +class Solution { + public boolean isValid(String s) { + boolean result= true; + Stack sta=new Stack<>(); + Map map=new HashMap<>(); + map.put('}','{'); + map.put(']','['); + map.put(')','('); + for(int i=0; i