Skip to content

Commit 185bb4d

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 0269cec + 36cfe67 commit 185bb4d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1502
-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+

contains-duplicate/Totschka.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// https://leetcode.com/problems/contains-duplicate/
2+
function containsDuplicate(nums: number[]): boolean {
3+
const counter = {};
4+
for (const n of nums) {
5+
if (!counter[n]) {
6+
counter[n] = 1;
7+
} else {
8+
return true;
9+
}
10+
}
11+
return false;
12+
}

contains-duplicate/Yjason-K.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Set 자료구조를 사용하여 중복 확인 (시간 복잡도: O(n) )
3+
* @param nums 중복 검사할 숫자 배열
4+
* @returns boolean 중복 여부
5+
*/
6+
7+
function containsDuplicate(nums: number[]): boolean {
8+
let unique: Set<number> = new Set([]);
9+
for (const num of nums) {
10+
if (unique.has(num)) {
11+
return true
12+
} else {
13+
unique.add(num)
14+
}
15+
}
16+
return false;
17+
};
18+

contains-duplicate/csucom.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <malloc.h>
2+
#include <string.h>
3+
4+
bool containsDuplicate(int* nums, int numsSize) {
5+
char* pflag = (char*)malloc(1000000001);
6+
char* mflag = (char*)malloc(1000000001);
7+
memset(pflag, 0, 1000000001);
8+
memset(mflag, 0, 1000000001);
9+
for (int i = 0; i < numsSize; ++i) {
10+
if (nums[i] < 0) {
11+
if (mflag[-nums[i]] == 1) {
12+
free(pflag);
13+
free(mflag);
14+
return true;
15+
}
16+
mflag[-nums[i]] = 1;
17+
}
18+
else {
19+
if (pflag[nums[i]] == 1) {
20+
free(pflag);
21+
free(mflag);
22+
return true;
23+
}
24+
pflag[nums[i]] = 1;
25+
}
26+
}
27+
free(pflag);
28+
free(mflag);
29+
return false;
30+
}
31+
32+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 풀이
2+
// map으로 중복된 값이 있는지 체크
3+
4+
// TC
5+
// 중복이 하나도 없는 경우에 최대 n번 조회
6+
// n번 반복시 총 작업의 복잡도는 O(n)
7+
8+
// SC
9+
// n개의 숫자를 저장하면 map이 사용하는 공간은 최대 O(n)
10+
11+
// (+) 정렬을 사용한다면?
12+
// 입력된 배열을 정렬해서 서로 인접한 값을 비교하면 O(1)의 SC로 중복 확인 가능.
13+
// 그러나 정렬을 사용하면 TC가 O(nlogn).
14+
15+
func containsDuplicate(nums []int) bool {
16+
m := make(map[int]int)
17+
for _, num := range nums {
18+
if _, ok := m[num]; ok {
19+
return true
20+
}
21+
m[num] = num
22+
}
23+
return false
24+
}

contains-duplicate/ekgns33.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
/*
5+
start 7:06~7:14 PASS
6+
input : integer array
7+
output : return if input contains duplicate
8+
constraint :
9+
1) empty array?
10+
nope. at least one
11+
2) size?
12+
[1, 10^5]
13+
3) sorted?
14+
nope.
15+
4) range of elements in the array?
16+
[-10^9, 10^9] >> max 2 * 10*9 +1
17+
18+
brute force:
19+
ds : array. algo : just nested for-loop
20+
iterate through the array, for index i 0 to n. n indicates the size of input
21+
nested loop fo r index j from i+1 to n
22+
if nums[j] == nums[i] return true;
23+
24+
return false;
25+
26+
time : O(n^2), space : O(1)
27+
28+
better:
29+
ds : hashset. algo : one for-loop
30+
iterate through the array:
31+
if set contains current value:
32+
return true;
33+
else
34+
add current value to set
35+
36+
return false;
37+
time : O(n) space :O(n)
38+
39+
*/
40+
class Solution {
41+
public boolean containsDuplicate(int[] nums) {
42+
Set<Integer> set = new HashSet<>();
43+
for(int num : nums) {
44+
if(set.contains(num)) return true;
45+
set.add(num);
46+
}
47+
return false;
48+
}
49+
}

contains-duplicate/hancrysta1.java

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+
}

0 commit comments

Comments
 (0)