Skip to content

Commit d72903d

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents a340f0c + 36cfe67 commit d72903d

File tree

11 files changed

+250
-0
lines changed

11 files changed

+250
-0
lines changed

โ€Ž3sum/easyone-jwlee.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ํ’€์ด
2+
// ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๊ณ 
3+
// two pointer ์‚ฌ์šฉ
4+
5+
// TC
6+
// ์ •๋ ฌ O(nlogn) + Two pointer ์ด์ค‘ ๋ฃจํ”„ O(n^2) = O(n^2)
7+
8+
// SC
9+
// Go์˜ sort.Ints()๋Š” TimSort๋ฅผ ์‚ฌ์šฉ.
10+
// Merge Sort์™€ Insertion Sort์˜ ์กฐํ•ฉ์œผ๋กœ ๋™์ž‘.
11+
// ์ •๋ ฌ O(n) + Two pointer O(1) + ๊ฒฐ๊ณผ ๋ฐฐ์—ด O(n) = O(n)
12+
13+
func threeSum(nums []int) [][]int {
14+
result := [][]int{}
15+
sort.Ints(nums) // nums๋ฅผ ์ •๋ ฌ
16+
17+
for i := 0; i < len(nums)-2; i++ {
18+
if i > 0 && nums[i] == nums[i-1] {
19+
continue // ์ค‘๋ณต๋œ ๊ฐ’ ๊ฑด๋„ˆ๋œ€
20+
}
21+
22+
left, right := i+1, len(nums)-1
23+
for left < right {
24+
sum := nums[i] + nums[left] + nums[right]
25+
if sum == 0 {
26+
result = append(result, []int{nums[i], nums[left], nums[right]})
27+
left++
28+
right--
29+
30+
// ์ค‘๋ณต ์ œ๊ฑฐ
31+
for left < right && nums[left] == nums[left-1] {
32+
left++
33+
}
34+
for left < right && nums[right] == nums[right+1] {
35+
right--
36+
}
37+
} else if sum < 0 {
38+
left++
39+
} else {
40+
right--
41+
}
42+
}
43+
}
44+
45+
return result
46+
}

โ€Žclimbing-stairs/easyone-jwlee.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// ํ’€์ด
2+
// 1 ์ผ ๋•Œ, ๊ฐ€๋Šฅํ•œ step์€ 1 -> 1๊ฐ€์ง€
3+
// 2 ์ผ ๋•Œ, ๊ฐ€๋Šฅํ•œ step์€ 1 1, 2 -> 2๊ฐ€์ง€
4+
// 3 ์ผ ๋•Œ, ๊ฐ€๋Šฅํ•œ step์€ 1 1 1, 1 2, 2 1 -> 3๊ฐ€์ง€
5+
// n ์ผ ๋•Œ, ๊ฐ€๋Šฅํ•œ stop์€ n-1์˜ ๊ฐ€์ง“์ˆ˜์— 1์ด ๋ถ™๊ณ , n-2์˜ ๊ฐ€์ง“์ˆ˜์— 2๊ฐ€ ๋ถ™๋Š”๋‹ค.
6+
7+
// TC
8+
// O(n)
9+
10+
// SC
11+
// intํƒ€์ž… ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ•ด์„œ O(1)
12+
13+
func climbStairs(n int) int {
14+
if n <= 2 {
15+
return n
16+
}
17+
prev := 1 // climb1
18+
curr := 2 // climb2
19+
for i := 3; i <= n; i++ {
20+
prev, curr = curr, (curr + prev)
21+
}
22+
return curr
23+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ํ’€์ด
2+
// preorder[0]์ด ์ œ์ผ ๊ผญ๋Œ€๊ธฐ
3+
// preorder ๋ฐฐ์—ด์˜ root index, inorder ๋ฐฐ์—ด์˜ ์‹œ์ž‘, inorder ๋ฐฐ์—ด์˜ ๋
4+
// ์œ„์˜ ์„ธ๊ฐ€์ง€๋ฅผ parameter๋กœ ๋ฐ›๋Š” ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ
5+
// ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ์ˆœํšŒ
6+
7+
// TC
8+
// map ๋งŒ๋“ค๊ธฐ O(n) + ์žฌ๊ท€ํ•จ์ˆ˜๋Š” ๊ฐ ๋…ธ๋“œ๋ฅผ ๋”ฑ ํ•œ๋ฒˆ์”ฉ๋งŒ ๋ฐฉ๋ฌธํ•˜๋ฏ€๋กœ O(n) = O(n)
9+
10+
// SC
11+
// map O(n) + ์žฌ๊ท€ํ•จ์ˆ˜ ์ตœ์•…์˜ ๊ฒฝ์šฐ ํ•œ์ชฝ์œผ๋กœ๋งŒ ๋…ธ๋“œ๊ฐ€ ์ด์–ด์ง€๋Š” ๊ฒฝ์šฐ O(n) = O(n)
12+
13+
func buildTree(preorder []int, inorder []int) *TreeNode {
14+
inorderMap := make(map[int]int)
15+
for i, n := range inorder {
16+
inorderMap[n] = i
17+
}
18+
19+
var recursive func(rootIndex, inStart, inEnd int) *TreeNode
20+
recursive = func(rootIndex, inStart, inEnd int) *TreeNode {
21+
if rootIndex >= len(preorder) || inStart > inEnd {
22+
return nil
23+
}
24+
rootVal := preorder[rootIndex]
25+
rootInorderIndex := inorderMap[rootVal]
26+
27+
result := &TreeNode{
28+
Val: rootVal,
29+
}
30+
31+
leftSize := rootInorderIndex - inStart
32+
33+
result.Left = recursive(rootIndex+1, inStart, rootInorderIndex-1)
34+
result.Right = recursive(rootIndex+1+leftSize, rootInorderIndex+1, inEnd)
35+
36+
return result
37+
}
38+
39+
return recursive(0, 0, len(inorder)-1)
40+
}

โ€Žcontains-duplicate/EstherKim97.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
2+
3+
def containsDuplicate(nums):
4+
seen = set()
5+
for num in nums:
6+
if num in seen:
7+
return True
8+
seen.add(num)
9+
return False
10+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.*;
2+
class Solution {
3+
public boolean containsDuplicate(int[] nums) {
4+
Set<Integer> numSet = Arrays.stream(nums).boxed().collect(Collectors.toSet());
5+
if(numSet.size()!=nums.length) return true;
6+
else return false;
7+
}
8+
}

โ€Ždecode-ways/easyone-jwlee.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// ํ’€์ด
2+
// dp๋กœ ํ’€์ด
3+
// ๋‘์ž๋ฆฌ ์ˆ˜์— ์ ํ•ฉํ•˜๋ฉด prev2(i-2)์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋”ํ•˜๊ธฐ
4+
5+
// TC
6+
// O(n)
7+
8+
// SC
9+
// data type์ด int์ธ ๋ณ€์ˆ˜๋งŒ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ O(1)
10+
11+
func numDecodings(s string) int {
12+
if len(s) == 0 || s[0] == '0' {
13+
return 0
14+
}
15+
prev2, prev1 := 1, 1
16+
for i := 1; i < len(s); i++ {
17+
curr := 0
18+
19+
// ํ•œ์ž๋ฆฌ์ˆ˜ ํ™•์ธ
20+
if s[i] != '0' {
21+
curr += prev1
22+
}
23+
24+
// ๋‘์ž๋ฆฌ์ˆ˜ ํ™•์ธ
25+
digit, _ := strconv.Atoi(s[i-1 : i+1])
26+
if digit >= 10 && digit <= 26 {
27+
curr += prev2
28+
}
29+
30+
prev2, prev1 = prev1, curr
31+
}
32+
return prev1
33+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def topKFrequent(self, nums, k):
3+
unique_no = set(nums)
4+
data = []
5+
6+
for i in unique_no:
7+
count = 0
8+
for j in nums:
9+
if i == j:
10+
count += 1
11+
data.append((count, i))
12+
13+
data = sorted(data, reverse=True, key=lambda x:[1])
14+
15+
return [x[1] for x in data[:k]]
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.*;
2+
class Solution {
3+
public int[] topKFrequent(int[] nums, int k) {
4+
Map<Integer,Integer> count = new HashMap<>();
5+
for(int i=0;i<nums.length;i++){
6+
count.put(nums[i],count.getOrDefault(nums[i],0)+1);
7+
}
8+
List<Integer> sortedCount = new ArrayList<>(count.keySet());
9+
sortedCount.sort((a,b)->count.get(b)-count.get(a));//value ๊ธฐ์ค€ ํ‚ค ์ •๋ ฌ
10+
int[] answer = new int[k];
11+
for(int i=0;i<k;i++){
12+
answer[i] = sortedCount.get(i);
13+
}
14+
15+
return answer;
16+
}
17+
}

โ€Žvalid-anagram/easyone-jwlee.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// ํ’€์ด
2+
// s์˜ rune์„ key๋กœ ๊ฐ™์€ ์ฒ ์ž์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋‹ด๊ฒŒํ•˜๊ณ 
3+
// t์™€ ๋น„๊ตํ•˜๋ฉด์„œ ๊ฐฏ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๊ฐ์†Œํ•˜๊ฒŒ ํ•ด์„œ
4+
// ๋‚จ์€ ์ฒ ์ž๊ฐ€ ์žˆ๊ฑฐ๋‚˜ (s์—๋Š” ์žˆ๊ณ , r์—๋Š” ์—†๋Š” ์ฒ ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ)
5+
// ๋” ๊ฐ์†Œ๋œ ์ฒ ์ž๊ฐ€ ์žˆ์œผ๋ฉด (s์—๋Š” ์—†๊ณ , r์—๋งŒ ์žˆ๋Š” ์ฒ ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ) false๊ฐ€ return ๋˜๊ฒŒ ํ•จ.
6+
7+
// TC
8+
// O(n+n+n) = O(n)
9+
10+
// SC
11+
// s์˜ ๊ธธ์ด๋งŒํผ ๋Š˜์–ด๋‚˜๋Š” map์œผ๋กœ ์ธํ•ด O(n)
12+
13+
func isAnagram(s string, t string) bool {
14+
m := make(map[rune]int)
15+
for _, r := range s {
16+
m[r]++
17+
}
18+
for _, r := range t {
19+
m[r]--
20+
}
21+
for _, count := range m {
22+
if count < 0 || count > 0 {
23+
return false
24+
}
25+
}
26+
return true
27+
}

โ€Žvalid-palindrome/EstherKim97.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def isPalindrome(self, s):
3+
import re
4+
# remove all non-alphanumeric characters
5+
s = re.sub(r'\W+', '', s.lower())
6+
7+
# check if the string is equal forward and backward
8+
for i in range(len(s)//2):
9+
if s[i] != s[-(i+1)]:
10+
return False
11+
return True
12+

0 commit comments

Comments
ย (0)