From cfe82dc93032abc470f3775fb669b1406f26902b Mon Sep 17 00:00:00 2001 From: easyone Date: Mon, 16 Dec 2024 07:27:09 +0900 Subject: [PATCH 1/6] Feat: Add solution of valid-anagram --- valid-anagram/easyone-jwlee.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 valid-anagram/easyone-jwlee.go diff --git a/valid-anagram/easyone-jwlee.go b/valid-anagram/easyone-jwlee.go new file mode 100644 index 000000000..136ac560c --- /dev/null +++ b/valid-anagram/easyone-jwlee.go @@ -0,0 +1,27 @@ +// 풀이 +// s의 rune을 key로 같은 철자의 갯수를 담게하고 +// t와 비교하면서 갯수를 하나씩 감소하게 해서 +// 남은 철자가 있거나 (s에는 있고, r에는 없는 철자가 있는 경우) +// 더 감소된 철자가 있으면 (s에는 없고, r에만 있는 철자가 있는 경우) false가 return 되게 함. + +// TC +// O(n+n+n) = O(n) + +// SC +// s의 길이만큼 늘어나는 map으로 인해 O(n) + +func isAnagram(s string, t string) bool { + m := make(map[rune]int) + for _, r := range s { + m[r]++ + } + for _, r := range t { + m[r]-- + } + for _, count := range m { + if count < 0 || count > 0 { + return false + } + } + return true +} From d95b42dcb78b3988876140c3a47c61f2cff8b4e2 Mon Sep 17 00:00:00 2001 From: easyone Date: Mon, 16 Dec 2024 07:58:42 +0900 Subject: [PATCH 2/6] Feat: Add solution of climbing-stairs --- climbing-stairs/easyone-jwlee.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 climbing-stairs/easyone-jwlee.go diff --git a/climbing-stairs/easyone-jwlee.go b/climbing-stairs/easyone-jwlee.go new file mode 100644 index 000000000..28e885655 --- /dev/null +++ b/climbing-stairs/easyone-jwlee.go @@ -0,0 +1,23 @@ +// 풀이 +// 1 일 때, 가능한 step은 1 -> 1가지 +// 2 일 때, 가능한 step은 1 1, 2 -> 2가지 +// 3 일 때, 가능한 step은 1 1 1, 1 2, 2 1 -> 3가지 +// n 일 때, 가능한 stop은 n-1의 가짓수에 1이 붙고, n-2의 가짓수에 2가 붙는다. + +// TC +// O(n) + +// SC +// int타입 변수만 사용해서 O(1) + +func climbStairs(n int) int { + if n <= 2 { + return n + } + prev := 1 // climb1 + curr := 2 // climb2 + for i := 3; i <= n; i++ { + prev, curr = curr, (curr + prev) + } + return curr +} From f83acab8d854b768a70c44045f79b96bf77d88d4 Mon Sep 17 00:00:00 2001 From: easyone Date: Mon, 16 Dec 2024 16:14:56 +0900 Subject: [PATCH 3/6] Feat: Add solution of 3sum --- 3sum/easyone-jwlee.go | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 3sum/easyone-jwlee.go diff --git a/3sum/easyone-jwlee.go b/3sum/easyone-jwlee.go new file mode 100644 index 000000000..5ae6116c3 --- /dev/null +++ b/3sum/easyone-jwlee.go @@ -0,0 +1,46 @@ +// 풀이 +// 배열을 정렬하고 +// two pointer 사용 + +// TC +// 정렬 O(nlogn) + Two pointer 이중 루프 O(n^2) = O(n^2) + +// SC +// Go의 sort.Ints()는 TimSort를 사용. +// Merge Sort와 Insertion Sort의 조합으로 동작. +// 정렬 O(n) + Two pointer O(1) + 결과 배열 O(n) = O(n) + +func threeSum(nums []int) [][]int { + result := [][]int{} + sort.Ints(nums) // nums를 정렬 + + for i := 0; i < len(nums)-2; i++ { + if i > 0 && nums[i] == nums[i-1] { + continue // 중복된 값 건너뜀 + } + + left, right := i+1, len(nums)-1 + for left < right { + sum := nums[i] + nums[left] + nums[right] + if sum == 0 { + result = append(result, []int{nums[i], nums[left], nums[right]}) + left++ + right-- + + // 중복 제거 + for left < right && nums[left] == nums[left-1] { + left++ + } + for left < right && nums[right] == nums[right+1] { + right-- + } + } else if sum < 0 { + left++ + } else { + right-- + } + } + } + + return result +} From 9264bff249bacad7710b67c0e2489abc27c32421 Mon Sep 17 00:00:00 2001 From: easyone Date: Tue, 17 Dec 2024 10:07:50 +0900 Subject: [PATCH 4/6] Feat: Add solution of construct-binary-tree-from-preorder-and-inorder-traversal --- .../easyone-jwlee.go | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go b/construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go new file mode 100644 index 000000000..5269e214e --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go @@ -0,0 +1,50 @@ +// 풀이 +// preorder[0]이 제일 꼭대기 +// preorder 배열의 root index, inorder 배열의 시작, inorder 배열의 끝 +// 위의 세가지를 parameter로 받는 재귀함수를 만들어서 +// 왼쪽 서브트리, 오른쪽 서브트리를 순회 + +// TC +// map 만들기 O(n) + 재귀함수는 각 노드를 딱 한번씩만 방문하므로 O(n) = O(n) + +// SC +// map O(n) + 재귀함수 최악의 경우 한쪽으로만 노드가 이어지는 경우 O(n) = O(n) + +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ + +func buildTree(preorder []int, inorder []int) *TreeNode { + inorderMap := make(map[int]int) + for i, n := range inorder { + inorderMap[n] = i + } + + var recursive func(rootIndex, inStart, inEnd int) *TreeNode + recursive = func(rootIndex, inStart, inEnd int) *TreeNode { + if rootIndex >= len(preorder) || inStart > inEnd { + return nil + } + rootVal := preorder[rootIndex] + rootInorderIndex := inorderMap[rootVal] + + result := &TreeNode{ + Val: rootVal, + } + + leftSize := rootInorderIndex - inStart + + result.Left = recursive(rootIndex+1, inStart, rootInorderIndex-1) + result.Right = recursive(rootIndex+1+leftSize, rootInorderIndex+1, inEnd) + + return result + } + + return recursive(0, 0, len(inorder)-1) +} + \ No newline at end of file From bc3a0b45c748f0136b493d8ee6426f2857a08be7 Mon Sep 17 00:00:00 2001 From: easyone Date: Tue, 17 Dec 2024 10:09:26 +0900 Subject: [PATCH 5/6] Fix: Fix line break. --- .../easyone-jwlee.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go b/construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go index 5269e214e..231f2c1e8 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/easyone-jwlee.go @@ -10,15 +10,6 @@ // SC // map O(n) + 재귀함수 최악의 경우 한쪽으로만 노드가 이어지는 경우 O(n) = O(n) -/** - * Definition for a binary tree node. - * type TreeNode struct { - * Val int - * Left *TreeNode - * Right *TreeNode - * } - */ - func buildTree(preorder []int, inorder []int) *TreeNode { inorderMap := make(map[int]int) for i, n := range inorder { @@ -47,4 +38,3 @@ func buildTree(preorder []int, inorder []int) *TreeNode { return recursive(0, 0, len(inorder)-1) } - \ No newline at end of file From df74dd463c9845e47c35a3f925cc9aafb0023a4e Mon Sep 17 00:00:00 2001 From: easyone Date: Thu, 19 Dec 2024 10:45:05 +0900 Subject: [PATCH 6/6] Feat: Add solution of decode-ways --- decode-ways/easyone-jwlee.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 decode-ways/easyone-jwlee.go diff --git a/decode-ways/easyone-jwlee.go b/decode-ways/easyone-jwlee.go new file mode 100644 index 000000000..29e7f85af --- /dev/null +++ b/decode-ways/easyone-jwlee.go @@ -0,0 +1,33 @@ +// 풀이 +// dp로 풀이 +// 두자리 수에 적합하면 prev2(i-2)에 해당하는 값을 더하기 + +// TC +// O(n) + +// SC +// data type이 int인 변수만 사용했으므로 O(1) + +func numDecodings(s string) int { + if len(s) == 0 || s[0] == '0' { + return 0 + } + prev2, prev1 := 1, 1 + for i := 1; i < len(s); i++ { + curr := 0 + + // 한자리수 확인 + if s[i] != '0' { + curr += prev1 + } + + // 두자리수 확인 + digit, _ := strconv.Atoi(s[i-1 : i+1]) + if digit >= 10 && digit <= 26 { + curr += prev2 + } + + prev2, prev1 = prev1, curr + } + return prev1 +}