Skip to content

Commit 32d80da

Browse files
authored
[hj4645] WEEK 01 solutions
[hj4645] WEEK 01 solutions
2 parents 8702661 + 1ee09cc commit 32d80da

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
# ๋ฆฌ์ŠคํŠธ ์•ˆ์— ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ 2๊ฐœ ์ด์ƒ ์กด์žฌํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ
3+
# Set์œผ๋กœ ๋ณ€ํ™˜ ์‹œ ์ค‘๋ณต์ด ์ œ๊ฑฐ๋˜๋ฏ€๋กœ List์™€ Set์˜ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•ด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Œ
4+
def containsDuplicate(self, nums: List[int]) -> bool:
5+
return len(nums) != len(set(nums))
6+
7+
# Time Complexity
8+
# - set(nums) โ†’ O(n)
9+
# - len(nums), len(set(nums)) โ†’ O(1)
10+
# - Total: O(n) (n = number of list elements)
11+

โ€Žhouse-robber/hj4645.ktโ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
// ๋ฐฐ์—ด์—์„œ ์ธ์ ‘ํ•œ ํ•ญ์€ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์„ ๋•Œ, ์ธ์ ‘ํ•˜์ง€ ์•Š์€ ํ•ญ์„ ๋”ํ•ด ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
3+
// 1. DP๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ ์ง‘๋งˆ๋‹ค ์„ ํƒํ•˜๊ฑฐ๋‚˜ ๊ฑด๋„ˆ๋›ฐ๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ˆ„์  ๊ณ„์‚ฐ
4+
// 2. ํ˜„์žฌ ์ง‘์„ ํ„ธ ๋•Œ, ์ด์ „ ์ง‘์„ ํ„ธ์ง€ ์•Š์€ ๊ฒฝ์šฐ๋งŒ ๋”ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ๊ณ„์‚ฐ
5+
fun rob(nums: IntArray): Int {
6+
if (nums.isEmpty()) return 0
7+
var prev1 = 0 // ๋ฐ”๋กœ ์ด์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ์ด์ต
8+
var prev2 = 0 // ์ด์ „ ์ด์ „ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ์ด์ต
9+
10+
for (num in nums) {
11+
var temp = prev1
12+
prev1 = maxOf(prev2 + num, prev1)
13+
prev2 = temp
14+
}
15+
16+
return prev1
17+
}
18+
}
19+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
// ๋ฐฐ์—ด์—์„œ ์—ฐ์†๋œ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
3+
// 1. ์ค‘๋ณต์€ ์ œ๊ฑฐํ•˜๊ณ  ์นด์šดํŠธ
4+
// 2. ์ •๋ ฌํ•˜์ง€ ์•Š๊ณ  ๊ณ„์‚ฐ๋„ ๊ฐ€๋Šฅ
5+
fun longestConsecutive(nums: IntArray): Int {
6+
if(nums.isEmpty()) return 0
7+
8+
val numSet = nums.toHashSet()
9+
var maxLen = 0
10+
11+
for(num in nums){
12+
if((num - 1) !in numSet){
13+
var currNum = num
14+
var currLen = 1
15+
16+
while((currNum + 1) in numSet){
17+
currNum++
18+
currLen++
19+
}
20+
if(currLen > maxLen) maxLen = currLen
21+
}
22+
}
23+
return maxLen
24+
}
25+
}
26+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
# nums ์—์„œ ๊ฐ€์žฅ ๋นˆ๋„๊ฐ€ ๋†’์€ k๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ
3+
# ๋”•์…”๋„ˆ๋ฆฌ์™€ ์ •๋ ฌ์„ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐ
4+
# ์‹œ๊ฐ„๋ณต์žก๋„: O(n log n), ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
5+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
6+
freq_map = {}
7+
for num in nums:
8+
freq_map[num] = freq_map.get(num, 0) + 1
9+
10+
sorted_nums = sorted(freq_map.items(), key=lambda x:x[1], reverse=True)
11+
return [num for num, _ in sorted_nums[:k]]
12+

โ€Žtwo-sum/hj4645.pyโ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
# 2๊ฐœ์˜ ์ˆ˜๋ฅผ ํ•ฉํ•ด target์ด ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ
3+
# ์ˆœ์„œ๊ฐ€ ๋ณด์žฅ๋˜๋Š” python dictionary๋ฅผ ์‚ฌ์šฉํ•ด์„œ,
4+
# ์š”์†Œ x์— ๋Œ€ํ•ด์„œ target-x ๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ ๋‚ด์— ์žˆ๋Š”์ง€๋ฅผ ์ฐพ๋Š”๋‹ค.
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
dict = {}
7+
for i, num in enumerate(nums):
8+
remain = target - num
9+
if remain in dict:
10+
return [dict[remain], i]
11+
dict[num] = i

0 commit comments

Comments
ย (0)