Skip to content

Commit 2bb9fb8

Browse files
committed
Merge branch 'main' of github.com:bus710/leetcode-study into week01
2 parents 3acda42 + 8e55614 commit 2bb9fb8

File tree

29 files changed

+840
-0
lines changed

29 files changed

+840
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
'''
4+
풀이:
5+
μ€‘λ³΅λœ μš”μ†Œκ°€ μžˆλŠ”μ§€ μ°ΎλŠ” λ¬Έμ œμž…λ‹ˆλ‹€.
6+
7+
hash set 으둜 쀑볡을 μ œκ±°ν•˜κ³ 
8+
κΈ°μ‘΄ nums 의 길이와 쀑볡 제거된 nums_set 의 길이가 같은지 return ν–ˆμŠ΅λ‹ˆλ‹€.
9+
10+
μ‹œκ°„ λ³΅μž‘λ„:
11+
O(n) - has set 을 λ§Œλ“œλŠ” μ‹œκ°„
12+
13+
곡간 λ³΅μž‘λ„:
14+
O(n) - n개의 μš”μ†Œλ₯Ό set에 λ‹΄κΈ° λ•Œλ¬Έ
15+
'''
16+
17+
18+
class Solution:
19+
def containsDuplicate(self, nums: List[int]) -> bool:
20+
nums_set = set(nums)
21+
return len(nums_set) != len(nums)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//μ‹œκ°„λ³΅μž‘λ„ : O(nlogn)
2+
class Solution {
3+
public boolean containsDuplicate(int[] nums) {
4+
Arrays.sort(nums);
5+
for(int i = 0; i < nums.length - 1; i++) {
6+
if(nums[i] == nums[i+1])
7+
return true;
8+
}
9+
return false;
10+
}
11+
}
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/mmyeon.jsβ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
6+
/**
7+
*
8+
* μ ‘κ·Ό 방법
9+
* - Set 객체 μ‚¬μš©ν•΄μ„œ 숫자 쀑볡 μ œκ±°ν•˜κΈ°
10+
* - 원본 λ°°μ—΄κ³Ό 길이 λΉ„κ΅ν•˜κΈ°
11+
*
12+
* μ‹œκ°„λ³΅μž‘λ„ :
13+
* - λ°°μ—΄ μˆœνšŒν•΄μ„œ μš”μ†Œ Set에 μ‚½μž… : O(n)
14+
* - Set의 μ‚¬μ΄μ¦ˆ 크기 비ꡐ : O(1)
15+
*
16+
* κ³΅κ°„λ³΅μž‘λ„ :
17+
* - Set에 μœ λ‹ˆν¬ν•œ 숫자 μ €μž₯ : O(n)
18+
*/
19+
20+
var containsDuplicate = function (nums) {
21+
return new Set(nums).size !== nums.length;
22+
};

β€Ž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))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 217. Contains Duplicate
3+
* 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.
4+
* https://leetcode.com/problems/contains-duplicate/description/
5+
*/
6+
function containsDuplicate(nums: number[]): boolean {
7+
const set = new Set<number>();
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
if (set.has(nums[i])) {
11+
return true;
12+
}
13+
set.add(nums[i]);
14+
}
15+
16+
return false;
17+
}
18+
19+
// TC: O(n)
20+
// https://262.ecma-international.org/15.0/index.html#sec-get-set.prototype.size
21+
// Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of Set objects. It is not intended to be a viable implementation model.

β€Žhouse-robber/Chaedie.pyβ€Ž

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
'''
2+
Solution:
3+
슀슀둜 ν’€μ§€ λͺ»ν•΄ ν•™μŠ΅λ§Œ μ§„ν–‰ν–ˆμŠ΅λ‹ˆλ‹€.
4+
λ‹€μŒ κΈ°νšŒμ— λ‹€μ‹œ 풀어보도둝 ν•˜κ² μŠ΅λ‹ˆλ‹€.
5+
'''
6+
class Solution:
7+
def rob(self, nums: List[int]) -> int:
8+
prev, curr = 0, 0
9+
for num in nums:
10+
prev, curr = curr, max(num + prev, curr)
11+
12+
return curr
13+
14+
15+
# class Solution:
16+
# '''
17+
# 4. λ‹¬λ ˆμ˜ μ½”λ“œ 풀이 - DP, prev, curr
18+
# O(n) time
19+
# O(1) space
20+
# '''
21+
22+
# # λ‹¬λ ˆμ˜ μ½”λ“œ 풀이 - DP, prev, cur
23+
24+
# def rob(self, nums: List[int]) -> int:
25+
# prev, curr = 0, 0
26+
# for num in nums:
27+
# prev, curr = curr, max(num + prev, curr)
28+
29+
# return curr
30+
31+
# '''
32+
# 3. λ‹¬λ ˆμ˜ μ½”λ“œ 풀이 - DP
33+
# [1,2,3,1]
34+
# [1, 2, 3, 1]
35+
# DP:[0, 1, 2, 4, 4]
36+
# MAX(1 + DP[2], DP[1]) = MAX(1 + 2, 4) = 4
37+
# '''
38+
# # λ‹¬λ ˆμ˜ μ½”λ“œ 풀이 - DP
39+
# # def rob(self, nums: List[int]) -> int:
40+
# # dp = [0] * (len(nums) + 1)
41+
# # dp[1] = nums[0]
42+
# # for n in range(2,len(nums) + 1):
43+
# # dp[n] = max(nums[n - 1] + dp[n - 2], dp[n - 1])
44+
# # return dp[-1]
45+
# '''
46+
# 2. λ‹¬λ ˆμ˜ μ½”λ“œ 풀이 - μž¬κ·€, λ©”λͺ¨μ΄μ œμ΄μ…˜
47+
# time: O(n)
48+
# space: O(n)
49+
# '''
50+
# # λ‹¬λ ˆμ˜ μ½”λ“œ 풀이 - μž¬κ·€, λ©”λͺ¨μ΄μ œμ΄μ…˜
51+
# # def rob(self, nums: List[int]) -> int:
52+
# # memo = {}
53+
54+
# # def dfs(start):
55+
# # if start in memo:
56+
# # return memo[start]
57+
# # if not start < len(nums):
58+
# # memo[start] = 0
59+
# # else:
60+
# # memo[start] = max(nums[start] + dfs(start + 2), dfs(start + 1))
61+
# # return memo[start]
62+
# # return dfs(0)
63+
# '''
64+
# 1. λ‹¬λ ˆμ˜ μ½”λ“œ 풀이 - μž¬κ·€
65+
# time: O(2^n)
66+
# space: O(n)
67+
68+
# F([1,2,3,1]) => MAX(1 + F([3,1], f([2,3,1])))
69+
# F([3,1]) => MAX(3 + F([]), F([1]))
70+
# F([]) => 0
71+
# F([1]) => MAX(1 + F([]), F([])) => MAX(1 + 0, 0) => 1
72+
# F([]) => 0
73+
# F([]) => 0
74+
# F([2,3,1]) => MAX(2 + F([1]), F([3,1]))
75+
# F([1]) => MAX(1 + F([]), F([])) => MAX(1 + 0, 0) => 1
76+
# F([]) => 0
77+
# F([]) => 0
78+
# F([3,1]) => MAX(3 + F([]), F([1]))
79+
# F([]) => 0
80+
# F([1]) => MAX(1 + F([]), F([])) => MAX(1 + 0, 0) => 1
81+
# F([]) => 0
82+
# F([]) => 0
83+
# μž¬κ·€κ°€ λΆˆν•„μš”ν•˜κ²Œ 반볡되고 μžˆλ‹€.
84+
# λ©”λͺ¨μ΄μ œμ΄μ…˜μœΌλ‘œ 기얡해두면 λ°˜λ³΅μ€ μŠ€ν‚΅ν•  수 μžˆλ‹€.
85+
# '''
86+
# # λ‹¬λ ˆμ˜ μ½”λ“œ 풀이 - μž¬κ·€
87+
# # def rob(self, nums: List[int]) -> int:
88+
89+
# # def dfs(start):
90+
# # if not start < len(nums):
91+
# # return 0
92+
# # return max(nums[start] + dfs(start + 2), dfs(start + 1))
93+
# # return dfs(0)
94+
95+
# # neetcode 풀이 - DP, μ΄ν•΄μ•ˆλ¨...
96+
# # def rob(self, nums: List[int]) -> int:
97+
# # rob1, rob2 = 0, 0
98+
# # # [rob1, rob2, n, n+1, ...]
99+
# # for n in nums:
100+
# # temp = max(n + rob1, rob2)
101+
# # rob1 = rob2
102+
# # rob2 = temp
103+
# # return rob2

β€Žhouse-robber/dalpang81.javaβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//μ‹œκ°„λ³΅μž‘λ„ O(n)
2+
class Solution {
3+
public int rob(int[] nums) {
4+
if (nums.length == 1) {
5+
return nums[0];
6+
}
7+
8+
int temp2 = nums[0];
9+
int temp1 = Math.max(nums[0], nums[1]);
10+
11+
for (int i = 2; i < nums.length; i++) {
12+
int current = Math.max(temp1, nums[i] + temp2);
13+
temp2 = temp1;
14+
temp1 = current;
15+
}
16+
17+
return temp1;
18+
}
19+
}

β€Žhouse-robber/limlimjo.jsβ€Ž

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var rob = function(nums) {
6+
// 1. nums λ°°μ—΄ 0일 λ•Œμ™€ 1일 λ•Œ
7+
if (nums.length === 0) return 0;
8+
if (nums.length === 1) return nums[0];
9+
10+
// 2. i=1일 λ•Œ
11+
nums[1] = Math.max(nums[0], nums[1]);
12+
13+
// 3. i=2일 λ•ŒλΆ€ν„° forλ¬Έ 순회
14+
for (let i=2; i<nums.length; i++) {
15+
nums[i] = Math.max(nums[i-2] + nums[i], nums[i-1])
16+
}
17+
return nums[nums.length-1];
18+
};
19+
20+
// μ‹œκ°„λ³΅μž‘λ„μ™€ κ³΅κ°„λ³΅μž‘λ„
21+
// μ‹œκ°„λ³΅μž‘λ„: λ°°μ—΄μ˜ 길이 n 만큼 forλ¬Έ μˆœνšŒν•˜λ―€λ‘œ -> O(n)
22+
// κ³΅κ°„λ³΅μž‘λ„: nums λ°°μ—΄ κ·ΈλŒ€λ‘œ μˆ˜μ •ν•˜μ—¬ 계산 -> O(1)

β€Žhouse-robber/mmyeon.jsβ€Ž

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
*
3+
* μ ‘κ·Ό 방법 : dp μ‚¬μš©
4+
* - λ°°μ—΄ 길이가 1개 μΌλ•ŒλŠ” 첫 번쨰 κ°’ λ¦¬ν„΄ν•˜κ³ , 2개면 더 큰 수λ₯Ό λ¦¬ν„΄ν•œλ‹€.
5+
* - 3개 λΆ€ν„°λŠ” λ°”λ‘œ μ˜†μ§‘ 값에 ν˜„μž¬μ§‘ 값을 λ”ν•œ κ°’κ³Ό μ˜†μ˜†μ§‘μ˜ 값을 λΉ„κ΅ν•΄μ„œ 큰 값을 κ³„μ‚°ν•œλ‹€.
6+
* - λ‹€μŒ μ§‘ 값에 ν˜„μž¬κΉŒμ§€μ˜ κ°’ ν™œμš©ν•˜κΈ° μœ„ν•΄μ„œ, λ°”λ‘œ μ˜†μ§‘, μ˜†μ˜†μ§‘ 값을 μ—…λ°μ΄νŠΈν•΄μ€€λ‹€.
7+
* - ν˜„μž¬ 값이 μ €μž₯된 μ˜†μ§‘κ°’μ„ λ¦¬ν„΄ν•œλ‹€.
8+
*
9+
* μ‹œκ°„λ³΅μž‘λ„ :
10+
* - μ£Όμ–΄μ§„ 숫자 λ°°μ—΄ 길이만큼 1회 μˆœνšŒν•˜λ‹ˆκΉŒ O(n)
11+
*
12+
* κ³΅κ°„λ³΅μž‘λ„ :
13+
* - μ˜†μ§‘κ³Ό μ˜†μ˜†μ§‘ 값을 2개의 λ³€μˆ˜μ— μ €μž₯ν•΄μ•Όν•˜λ‹ˆκΉŒ O(1)
14+
*
15+
*/
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var rob = function (nums) {
21+
if (nums.length === 1) return nums[0];
22+
if (nums.length === 2) return Math.max(nums[0], nums[1]);
23+
24+
let prevPrevHouse = nums[0];
25+
let prevHouse = Math.max(nums[0], nums[1]);
26+
27+
for (let i = 2; i < nums.length; i++) {
28+
const current = Math.max(prevHouse, prevPrevHouse + nums[i]);
29+
prevPrevHouse = prevHouse;
30+
prevHouse = current;
31+
}
32+
33+
return prevHouse;
34+
};

0 commit comments

Comments
Β (0)