Skip to content

Commit fb56fde

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 2c6b564 + 89fffe7 commit fb56fde

File tree

15 files changed

+390
-0
lines changed

15 files changed

+390
-0
lines changed

contains-duplicate/WHYjun.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(set(nums)) != len(nums)

contains-duplicate/YuuuuuuYu.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Runtime: 14ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 93.59MB
6+
* Space Complexity: O(n)
7+
*
8+
* Approach: HashSet을 사용하여 중복 검사
9+
* - 배열을 순회하면서 각 원소를 HashSet에 저장
10+
* - 이미 존재하는 원소를 발견하면 즉시 true 반환
11+
*/
12+
class Solution {
13+
public boolean containsDuplicate(int[] nums) {
14+
Set<Integer> set = new HashSet<>();
15+
for (int num: nums) {
16+
if (set.contains(num)) {
17+
return true;
18+
}
19+
set.add(num);
20+
}
21+
return false;
22+
}
23+
}
24+

contains-duplicate/ys-han00.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
3+
class Solution {
4+
public:
5+
bool containsDuplicate(vector<int>& nums) {
6+
sort(nums.begin(), nums.end());
7+
8+
for(int i = 0; i < nums.size() - 1; i++)
9+
if(nums[i] == nums[i+1])
10+
return true;
11+
12+
return false;
13+
}
14+
};
15+
16+
// 첫 시도 및 틀린 풀이
17+
// 틀린 이유:
18+
// leetcode는 처음 풀어보는데, 백준보다 시간 제한이 엄격함
19+
// -> 20억 배열 선언하는것만으로도 시간초과 발생
20+
//
21+
// class Solution {
22+
// public:
23+
// bool containsDuplicate(vector<int>& nums) {
24+
// vector<bool> check(2'000'000'001, false);
25+
// int offset = 1'000'000'000;
26+
27+
// for(int i = 0; i < nums.size(); i++) {
28+
// int idx = nums[i] + offset;
29+
30+
// if(check[idx] == true)
31+
// return true;
32+
// check[idx] = true;
33+
// }
34+
// return false;
35+
// }
36+
// };
37+

house-robber/WHYjun.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
# memo for DP
4+
dp = {}
5+
return self.robs(nums, dp, len(nums) - 1)
6+
7+
def robs(self, nums: List[int], dp: Dict[int, int], houseToRob: int) -> int:
8+
if houseToRob < 0:
9+
return 0
10+
11+
if houseToRob - 2 not in dp:
12+
dp[houseToRob - 2] = self.robs(nums, dp, houseToRob - 2)
13+
14+
if houseToRob - 1 not in dp:
15+
dp[houseToRob - 1] = self.robs(nums, dp, houseToRob - 1)
16+
17+
18+
return max(
19+
dp[houseToRob - 2] + nums[houseToRob],
20+
dp[houseToRob - 1]
21+
)

house-robber/YuuuuuuYu.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Runtime: 0ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 42.72MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: 두 가지 변수만 사용한 DP 접근법
9+
* - withoutPrevHouse: 이전 집을 털지 않았을 때의 최대 금액
10+
* - withPrevHouse: 이전 집까지 고려한 최대 금액
11+
* - 각 집에서 "털거나 안 털거나" 두 가지 선택지 중 최대값 선택
12+
* 1) 현재 집을 안 털면: 이전 집까지의 최대값 유지
13+
* 2) 현재 집을 털면: 전전 집까지의 최대값 + 현재 집 금액
14+
*/
15+
class Solution {
16+
public int rob(int[] nums) {
17+
int withoutPrevHouse = 0;
18+
int withPrevHouse = 0;
19+
20+
for (int money: nums) {
21+
int maxMoney = Math.max(withPrevHouse, withoutPrevHouse+money);
22+
withoutPrevHouse = withPrevHouse;
23+
withPrevHouse = maxMoney;
24+
}
25+
26+
return withPrevHouse;
27+
}
28+
}

house-robber/ys-han00.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int rob(vector<int>& nums) {
4+
vector<int> dp(nums.size(), 0);
5+
6+
dp[0] = nums[0];
7+
if(dp.size() == 1)
8+
return dp[0];
9+
10+
dp[1] = nums[1];
11+
int ans = max(dp[0], dp[1]);
12+
13+
for(int i = 2; i < dp.size(); i++) {
14+
int maxi = -1;
15+
for(int j = 0; j < i - 1; j++)
16+
maxi = max(dp[j], maxi);
17+
dp[i] = maxi + nums[i];
18+
ans = max(ans, dp[i]);
19+
}
20+
21+
return ans;
22+
}
23+
};
24+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import heapq
2+
3+
class Solution:
4+
def longestConsecutive(self, nums: List[int]) -> int:
5+
# remove duplicates by using set
6+
numsSet = set(nums)
7+
8+
if len(numsSet) == 0 or len(numsSet) == 1:
9+
return len(numsSet)
10+
11+
# Use priority queue to sort in O(n)
12+
pq = heapq.heapify(list(numsSet))
13+
14+
prev = heapq.heappop(pq)
15+
answer, count = 1, 1
16+
for i in range(len(numsSet) - 1):
17+
popped = heapq.heappop(pq)
18+
if prev + 1 == popped:
19+
count += 1
20+
else:
21+
count = 1
22+
23+
prev = popped
24+
answer = max(answer, count)
25+
26+
return answer
27+
28+
def longestConsecutiveUsingSetOnly(self, nums: List[int]) -> int:
29+
# remove duplicates by using set
30+
numsSet = set(nums)
31+
32+
answer = 0
33+
34+
for num in numsSet:
35+
# continue if it's not the longest consecutive
36+
if num - 1 in numsSet:
37+
continue
38+
else:
39+
count = 1
40+
while num + count in numsSet:
41+
count += 1
42+
answer = max(answer, count)
43+
44+
return answer
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Runtime: 26ms
3+
* Time Complexity: O(n)
4+
* - HashSet 구성: O(n)
5+
* - 모든 원소 순회: 최악 O(n), 평균적으로 더 빠름 (조기 종료)
6+
* - 연속 수열 탐색: O(n)
7+
*
8+
* Memory: 95.11MB
9+
* Space Complexity: O(n)
10+
*
11+
* Approach: HashSet을 이용하여 중복 제거 후 연속 수열 길이 탐색
12+
* - 연속 수열의 시작점만 탐색하여 중복 작업 방지 (num-1이 없는 경우)
13+
* - 각 시작점에서 연속된 다음 숫자들을 순차적으로 탐색
14+
* - nums 사이즈에 도달 시 조기 종료하여 불필요한 탐색 방지
15+
*/
16+
class Solution {
17+
public int longestConsecutive(int[] nums) {
18+
if (nums.length == 0) return 0;
19+
20+
Set<Integer> set = new HashSet<>();
21+
for (int num: nums) {
22+
set.add(num);
23+
}
24+
25+
int longestLength = 0;
26+
for (int num: set) {
27+
if (!set.contains(num-1)) {
28+
int currentNum = num;
29+
int currentLength = 1;
30+
31+
while (set.contains(currentNum+1)) {
32+
currentNum++;
33+
currentLength++;
34+
}
35+
36+
longestLength = longestLength < currentLength ? currentLength : longestLength;
37+
if (longestLength == set.size()) {
38+
return longestLength;
39+
}
40+
}
41+
}
42+
43+
return longestLength;
44+
}
45+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
3+
class Solution {
4+
public:
5+
int longestConsecutive(vector<int>& nums) {
6+
if(nums.size() == 0)
7+
return 0;
8+
9+
sort(nums.begin(), nums.end());
10+
11+
int ans = 1, cnt = 1;
12+
for(int i = 1; i < nums.size(); i++) {
13+
if(nums[i] == nums[i - 1])
14+
continue;
15+
if(nums[i] - 1 == nums[i - 1])
16+
cnt++;
17+
else
18+
cnt = 1;
19+
ans = max(cnt, ans);
20+
}
21+
22+
return ans;
23+
}
24+
};
25+

top-k-frequent-elements/WHYjun.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
3+
# Time complexity: O(nlog(n))
4+
# Space Complexity: O(n)
5+
6+
if len(nums) <= k:
7+
return list(set(nums))
8+
9+
counts = {}
10+
for num in nums:
11+
if num not in counts:
12+
counts[num] = 0
13+
counts[num] += 1
14+
15+
# The key of dictionary is unique.
16+
sortedKeys = sorted(list(counts.keys()), key=lambda key: counts[key], reverse = True)
17+
return sortedKeys[:k]

0 commit comments

Comments
 (0)