Skip to content

Commit 42a63e2

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 2f505d9 + 8f1e53e commit 42a63e2

39 files changed

+1277
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode_study
2+
3+
/**
4+
* Set 자료 구조로 변경 후 원소의 개수를 비교해 문제 해결
5+
* 시간 복잡도 : O(n)
6+
* -> 모든 Array의 원소를 순회해야함.
7+
* 공간 복잡도 : O(n)
8+
* -> IntArray의 요소 개수에 비례하여 추가적인 공간이 필요함.
9+
*/
10+
fun containsDuplicate(nums: IntArray): Boolean {
11+
val changeSet = nums.toSet()
12+
return changeSet.size != nums.size
13+
}

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/limlimjo.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function(nums) {
6+
7+
// 첫 번째 방법: filter + indexOf 사용 => indexOf(),filter() 각각 시간복잡도 O(n) 두 개가 중첩이므로 시간복잡도 O(n^2)
8+
// Runtime: Time Limit Exceeded 발생
9+
const method1 = function() {
10+
const filterNums = nums.filter((item,index) => nums.indexOf(item) !== index);
11+
return filterNums.length > 0;
12+
}
13+
14+
// 두 번째 방법: Set 사용 => nums 배열을 set으로 변환할 때 한번씩 확인하면 되므로 시간복잡도 O(n)
15+
// Runtime: 14ms
16+
const method2 = function() {
17+
const setNums = new Set(nums);
18+
return setNums.size !== nums.length;
19+
}
20+
21+
// 위 두 가지 방법 중 Set을 사용하는 것이 성능상 훨씬 나음
22+
return method2();
23+
};

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/pmjuu.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
return len(nums) != len(set(nums))

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/EcoFriendlyAppleSu.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode_study
2+
3+
/**
4+
* DP를 사용한 문제 풀이.
5+
* DP를 사용하지 않고 모든 경우의 수를 계산하여 최대 값을 구하려면 100!에 해당하는 연산이 필요하며, 이는 시간 초과를 초래합니다.
6+
* 시간 복잡도 : O(n)
7+
* -> 주어진 숫자 배열 만큼 반복 진행
8+
* 공간 복잡도 : O(n)
9+
* -> 숫자 배열만큼의 가중치를 담을 배열 필요
10+
*/
11+
fun rob(nums: IntArray): Int {
12+
val dp = IntArray(nums.size)
13+
14+
if (nums.size == 1) {
15+
return nums[0]
16+
}
17+
18+
if (nums.size == 2) {
19+
return max(nums[0], nums[1])
20+
}
21+
22+
dp[0] = nums[0]
23+
dp[1] = nums[1]
24+
dp[2] = nums[2] + dp[0]
25+
26+
for (i in 3 until nums.size) {
27+
dp[i] = max(dp[i-3], dp[i-2]) + nums[i]
28+
}
29+
return dp.max()
30+
}

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

0 commit comments

Comments
 (0)