Skip to content

Commit 92dcf72

Browse files
authored
Merge pull request #1159 from yayyz/main
[yayyz] WEEK 01 solutions
2 parents 651d248 + 9475b05 commit 92dcf72

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

contains-duplicate/yayyz.py

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

house-robber/yayyz.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
5+
if n == 1:
6+
return nums[0]
7+
8+
dp = [0] * n
9+
dp[0] = nums[0]
10+
dp[1] = max(nums[0], nums[1])
11+
12+
# i 번째를 털지 않은 조건과 i번째를 털은 조건 중 max를 비교
13+
for i in range(2, n):
14+
dp[i] = max(dp[i-1], dp[i-2] + nums[i])
15+
16+
return dp[-1]

longest-consecutive-sequence/yayyz.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution:
2+
def longestConsecutive(self, nums: List[int]) -> int:
3+
if len(nums) == 0:
4+
return 0
5+
6+
nums = sorted(set(nums))
7+
8+
currLength = 1
9+
lastNum = nums[0]
10+
result = 1
11+
12+
for i in range(1, len(nums)):
13+
if nums[i] == lastNum + 1:
14+
currLength += 1
15+
else:
16+
currLength = 1
17+
18+
result = max(result, currLength)
19+
lastNum = nums[i]
20+
21+
return result

product-of-array-except-self/yayyz.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
# prefix, postfix probelm :(
3+
def productExceptSelf(self, nums: List[int]) -> List[int]:
4+
output = [1] * len(nums)
5+
6+
prefix = 1
7+
for i in range(len(nums)):
8+
output[i] = prefix
9+
prefix *= nums[i]
10+
11+
postfix = 1
12+
for i in range(len(nums) -1, -1, -1):
13+
output[i] *= postfix
14+
postfix *= nums[i]
15+
16+
return output

two-sum/yayyz.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
hashmap = {}
4+
for i, num in enumerate(nums):
5+
complement = target - num
6+
if complement in hashmap:
7+
return [hashmap[complement], i]
8+
hashmap[num] = i
9+
10+

0 commit comments

Comments
 (0)