Skip to content

Commit 04e072c

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 2e471ad + 67949d5 commit 04e072c

File tree

24 files changed

+861
-0
lines changed

24 files changed

+861
-0
lines changed

contains-duplicate/gwbaik9717.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {boolean}
7+
*/
8+
var containsDuplicate = function (nums) {
9+
const set = new Set(nums);
10+
11+
return set.size !== nums.length;
12+
};

contains-duplicate/heypaprika.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Big-O 예상 : O(n)
2+
class Solution:
3+
def containsDuplicate(self, nums: List[int]) -> bool:
4+
num_dict = {}
5+
for num in nums:
6+
if num in num_dict:
7+
return True
8+
else:
9+
num_dict[num] = 1
10+
return False
11+

contains-duplicate/mike2ox.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Source: https://leetcode.com/problems/contains-duplicate/
3+
* 풀이방법: Set을 이용하여 중복된 값이 있는지 확인
4+
* 시간복잡도: O(n)
5+
* 공간복잡도: O(n)
6+
*
7+
* 생각나는 풀이방법
8+
* 1. 단순하게 sorted를 이용하여 이전값과 비교하여 중복된 값이 있는지 확인
9+
* 2. 정렬하지 않고 nums의 길이만큼의 배열을 만들어서 중복된 값이 있는지 저장하면서 확인
10+
*/
11+
function containsDuplicate(nums: number[]): boolean {
12+
// 중복된 값이 없는 자료구조 Set 활용
13+
const set = new Set<number>(nums);
14+
// Set의 size와 nums의 length를 비교하여 중복된 값이 있는지 확인
15+
return set.size !== nums.length;
16+
}

contains-duplicate/neverlish.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func Test(t *testing.T) {
9+
result1 := containsDuplicate([]int{1, 2, 3, 1})
10+
11+
if result1 != true {
12+
t.Fatal("failed test1")
13+
}
14+
15+
result2 := containsDuplicate([]int{1, 2, 3, 4})
16+
17+
if result2 != false {
18+
t.Fatal("failed test2")
19+
}
20+
}
21+
22+
func containsDuplicate(nums []int) bool {
23+
data := make(map[int]bool)
24+
25+
for _, num := range nums {
26+
if data[num] {
27+
return true
28+
} else {
29+
data[num] = true
30+
}
31+
}
32+
return false
33+
}

contains-duplicate/ysle0.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package contains_duplicate
2+
3+
import "sort"
4+
5+
/*
6+
1. 문제
7+
8+
주어진 int 배열 nums에 숫자가 중복되는 경우가 한 번이라도 있으면 true, 그렇지 않으면 false 를 리턴
9+
10+
2. 풀이
11+
12+
고유값만 저장하는 set(go 에서는 map)의 성질을 활용하여
13+
nums를 순회하며 set에 값이 있는지 없는지 체크하여
14+
숫자가 중복되는 경우를 체크
15+
16+
3. 분석
17+
- 시간 복잡도: O(N)
18+
nums 탐색: O(N)
19+
배열 nums의 모든 원소를 단 한 번 순회
20+
map 삽입, 탐색: O(1)
21+
map의 내부 구현은 해시 테이블.
22+
O(N)보다 작아 무시됨
23+
- 공간 복잡도: O(N)
24+
최악의 경우라도 사용공간은 nums 의 크기만큼 + nums의 모든 원소를 포함한 map
25+
*/
26+
func containsDuplicate(nums []int) bool {
27+
seen := map[int]int{}
28+
29+
for _, n := range nums {
30+
if _, ok := seen[n]; ok {
31+
return true
32+
}
33+
34+
seen[n] = 1
35+
}
36+
37+
return false
38+
}
39+
40+
func containsDuplicate_SortedApproach(nums []int) bool {
41+
// early exit for small slices
42+
if len(nums) < 2 {
43+
return false
44+
}
45+
46+
// sort in ascending order and check adjacent elements
47+
sort.Ints(nums)
48+
for i := 1; i < len(nums); i++ {
49+
if nums[i] == nums[i-1] {
50+
return true
51+
}
52+
}
53+
54+
return false
55+
}

house-robber/gwbaik9717.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var rob = function (nums) {
9+
const n = nums.length;
10+
const dp = Array.from({ length: n + 1 }, () => 0);
11+
dp[1] = nums[0];
12+
13+
for (let i = 2; i < n + 1; i++) {
14+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
15+
}
16+
17+
return dp.at(-1);
18+
};

house-robber/heypaprika.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Big-O 예상 : O(n)
2+
class Solution:
3+
def rob(self, nums: List[int]) -> int:
4+
a = [0] * len(nums)
5+
6+
if len(nums) == 1:
7+
return nums[0]
8+
elif len(nums) == 2:
9+
return max(nums[0], nums[1])
10+
11+
a[0] = nums[0]
12+
a[1] = nums[1]
13+
a[2] = max(a[0] + nums[2], a[1])
14+
15+
for i in range(3, len(nums)):
16+
a[i] = max(a[i-3], a[i-2]) + nums[i]
17+
18+
return max(a)
19+

house-robber/mike2ox.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Source: https://leetcode.com/problems/house-robber/
3+
* 풀이방법: DP를 이용하여 집을 털 때 최대값을 구함
4+
* 시간복잡도: O(n)
5+
* 공간복잡도: O(n)
6+
*
7+
* 생각나는 풀이방법
8+
*/
9+
function rob(nums: number[]): number {
10+
if (nums.length === 0) return 0;
11+
if (nums.length === 1) return nums[0];
12+
if (nums.length === 2) return Math.max(nums[0], nums[1]);
13+
14+
let prev = nums[0];
15+
let maxResult = Math.max(nums[0], nums[1]);
16+
let current = 0;
17+
18+
// 남은 집을 순회하면서 최대값을 구함
19+
for (let i = 2; i < nums.length; i++) {
20+
current = Math.max(maxResult, prev + nums[i]);
21+
prev = maxResult;
22+
maxResult = current;
23+
}
24+
return maxResult;
25+
}

house-robber/neverlish.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func Test(t *testing.T) {
11+
result1 := rob([]int{1, 2, 3, 1})
12+
13+
if result1 != 4 {
14+
t.Fatal("failed test1")
15+
}
16+
17+
result2 := rob([]int{2, 7, 9, 3, 1})
18+
19+
if result2 != 12 {
20+
t.Fatal("failed test2")
21+
}
22+
}
23+
24+
func rob(nums []int) int {
25+
length := len(nums)
26+
27+
if length == 0 {
28+
return 0
29+
}
30+
if length == 1 {
31+
return nums[0]
32+
}
33+
34+
moneys := make([]int, length)
35+
36+
moneys[0] = nums[0]
37+
moneys[1] = max(nums[0], nums[1])
38+
39+
for position := 2; position < length; position++ {
40+
moneys[position] = max(moneys[position-1], moneys[position-2]+nums[position])
41+
}
42+
43+
return moneys[length-1]
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var longestConsecutive = function (nums) {
9+
let answer = 0;
10+
const consecutiveDict = new Map();
11+
12+
for (const num of nums) {
13+
consecutiveDict.set(num, true);
14+
}
15+
16+
for (const num of nums) {
17+
if (consecutiveDict.has(num - 1)) {
18+
continue;
19+
}
20+
21+
let length = 1;
22+
while (consecutiveDict.has(num + length)) {
23+
length++;
24+
}
25+
26+
answer = Math.max(answer, length);
27+
}
28+
29+
return answer;
30+
};

0 commit comments

Comments
 (0)