diff --git a/climbing-stairs/donghyeon95.java b/climbing-stairs/donghyeon95.java new file mode 100644 index 000000000..ce74ba608 --- /dev/null +++ b/climbing-stairs/donghyeon95.java @@ -0,0 +1,64 @@ + +/* +* 처음 풀이 +* n개 중 2를 선택하는 조합 문제로 해결 +* 21부터 overflow 발생 => 너무 큰 숫자 +* 이대로 푸려면 1과 2의 갯수가 정확하게 같아지는 경우 * 2를 하면 f(1갯수, 2갯수) 할 때, f(x, y) = f(y,x)는 같다는 점을 착안하면 42까지는 풀 수 있을 듯 하다. +* 하지만 45라서 안되는 거 같다. +* */ + +// public int climbStairs(int n) { +// // N개의 1중에서 묶이는 경우의 수가 몇개인지 +// // 5 => 1 1 1 1 1 +// // 2의 갯수가 0개 2/n 까지 있는 경우의 수를 구하면 될 듯 +// long result = 0; +// int max2Cnt = n/2; +// for (int i=0; i inList; + HashMap treeNodes = new HashMap<>(); + + public TreeNode buildTree(int[] preorder, int[] inorder) { + preList = preorder; + inList = Arrays.asList(Arrays.stream(inorder).boxed().toArray(Integer[]::new)); + List preL = Arrays.asList(Arrays.stream(preorder).boxed().toArray(Integer[]::new)); + + int preIndex = 0; + int rootVal = preorder[0]; + TreeNode root = new TreeNode(rootVal); + treeNodes.put(rootVal, root); + + while (preIndex < preList.length - 1) { + System.out.println(preIndex); + int inIndex = inList.indexOf(preList[preIndex]); + int inNextIndex = inList.indexOf(preList[preIndex + 1]); + + TreeNode node = new TreeNode(preList[preIndex + 1]); + treeNodes.put(preList[preIndex + 1], node); + + if (inIndex > inNextIndex) { + // 현재 node의 왼쪽 자식으로 + TreeNode nowNode = treeNodes.get(preList[preIndex]); + nowNode.setLeft(node); + } else { + // inorder의 앞 중에서 가장 처음 inList에서 앞인게 + int value = 0; + int ii = inNextIndex; + while (ii >= 0) { + value = inorder[ii - 1]; + int i = preL.indexOf(value); + if (i < preIndex + 1) { + treeNodes.get(preList[i]).setRight(node); + break; + } + ii--; + } + + } + + preIndex++; + } + + return treeNodes.get(rootVal); + } + +} + + diff --git a/valid-anagram/donghyeon95.java b/valid-anagram/donghyeon95.java new file mode 100644 index 000000000..9a231e60a --- /dev/null +++ b/valid-anagram/donghyeon95.java @@ -0,0 +1,32 @@ +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +/* + map에 넣고 빼는 시간이 O(n)이라고는 하나, 시간이 걸린다. + 반복문으로 처리할 수 있는 방법이 없을까? +*/ + +class Solution { + public boolean isAnagram(String s, String t) { + boolean result = false; + + char[] chars = s.toCharArray(); + Map counter = new HashMap<>(); + for (char c: chars) { + counter.put(c, counter.getOrDefault(c, 0)+1); + } + + char[] tChars = t.toCharArray(); + for (char c: tChars) { + if (!counter.containsKey(c)) return false; + counter.put(c, counter.get(c)-1); + + if (counter.get(c) == 0) + counter.remove(c); + } + + return counter.isEmpty(); + } +} +