Skip to content

Commit fbe1ac8

Browse files
committed
ADD : #220 #237 #240 #264 by heypaprika
1 parent 29a569b commit fbe1ac8

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

house-robber/heypaprika.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
a = [0] * len(nums)
4+
5+
if len(nums) == 1:
6+
return nums[0]
7+
elif len(nums) == 2:
8+
return max(nums[0], nums[1])
9+
10+
a[0] = nums[0]
11+
a[1] = nums[1]
12+
a[2] = max(a[0] + nums[2], a[1])
13+
14+
for i in range(3, len(nums)):
15+
a[i] = max(a[i-3], a[i-2]) + nums[i]
16+
17+
return max(a)
18+
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+
nums = sorted(list(set(nums)))
4+
if len(nums) == 0:
5+
return 0
6+
elif len(nums) == 1:
7+
return 1
8+
cur_long = 1
9+
longest = 1
10+
for i, num in enumerate(nums):
11+
if i == 0:
12+
continue
13+
else:
14+
if nums[i-1] + 1 == nums[i]:
15+
cur_long += 1
16+
if longest < cur_long:
17+
longest = cur_long
18+
else:
19+
cur_long = 1
20+
return longest
21+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import heapq
2+
class Solution:
3+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
4+
5+
num_dict = {}
6+
for num in nums:
7+
num_dict[num] = num_dict.get(num, 0) + 1
8+
heap = []
9+
for k_, v in num_dict.items():
10+
heapq.heappush(heap, [-v, k_])
11+
ans = []
12+
for i in range(k):
13+
ans.append(heapq.heappop(heap)[1])
14+
return ans
15+

valid-palindrome/heypaprika.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
s = "".join(s.lower().split(" "))
4+
new_s = ""
5+
for item in s:
6+
if (ord("a") <= ord(item) <= ord("z")) or (ord("0") <= ord(item) <= ord("9")):
7+
new_s += item
8+
output = True
9+
for i in range(len(new_s) // 2):
10+
if new_s[i] != new_s[-i-1]:
11+
output = False
12+
break
13+
return output
14+

0 commit comments

Comments
 (0)