From c527927c580ec4610ad891133df1c562b1dd7501 Mon Sep 17 00:00:00 2001 From: jeldo Date: Tue, 3 Dec 2024 21:19:01 +0900 Subject: [PATCH 1/5] 217 --- contains-duplicate/jeldo.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 contains-duplicate/jeldo.py diff --git a/contains-duplicate/jeldo.py b/contains-duplicate/jeldo.py new file mode 100644 index 000000000..9289d2cd8 --- /dev/null +++ b/contains-duplicate/jeldo.py @@ -0,0 +1,4 @@ +class Solution: + # O(n) + def containsDuplicate(self, nums: list[int]) -> bool: + return len(nums) != len(set(nums)) # O(n) From 119514ce03e5027b9904309d367a5bf9487de510 Mon Sep 17 00:00:00 2001 From: jeldo Date: Tue, 3 Dec 2024 21:19:07 +0900 Subject: [PATCH 2/5] 237 --- top-k-frequent-elements/jeldo.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 top-k-frequent-elements/jeldo.py diff --git a/top-k-frequent-elements/jeldo.py b/top-k-frequent-elements/jeldo.py new file mode 100644 index 000000000..72070aeff --- /dev/null +++ b/top-k-frequent-elements/jeldo.py @@ -0,0 +1,9 @@ +from collections import Counter +import heapq + + +class Solution: + # O(nlogn) + def topKFrequent(self, nums: list[int], k: int) -> list[int]: + ls = [(key, value) for key, value in Counter(nums).items()] # O(n) + return [key for _, key in heapq.nlargest(n=k, iterable=ls)] # O(nlogn) From a302fff6300d86f386b8eb1e5b492818364dd23f Mon Sep 17 00:00:00 2001 From: jeldo Date: Tue, 3 Dec 2024 21:19:11 +0900 Subject: [PATCH 3/5] 220 --- valid-palindrome/jeldo.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 valid-palindrome/jeldo.py diff --git a/valid-palindrome/jeldo.py b/valid-palindrome/jeldo.py new file mode 100644 index 000000000..f72d84fed --- /dev/null +++ b/valid-palindrome/jeldo.py @@ -0,0 +1,5 @@ +class Solution: + # O(n) + def isPalindrome(self, s: str) -> bool: + s = ''.join(ch.lower() for ch in s if ch.isalnum()) # O(n) + return s == s[::-1] # O(n) From 7f6ea6f53663f07ec588ffbf17515fa4abeeb557 Mon Sep 17 00:00:00 2001 From: jeldo Date: Tue, 3 Dec 2024 21:45:04 +0900 Subject: [PATCH 4/5] 198 --- house-robber/jeldo.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 house-robber/jeldo.py diff --git a/house-robber/jeldo.py b/house-robber/jeldo.py new file mode 100644 index 000000000..f0d53f73c --- /dev/null +++ b/house-robber/jeldo.py @@ -0,0 +1,9 @@ +class Solution: + # O(n) + def rob(self, nums: list[int]) -> int: + if len(nums) <= 2: + return max(nums) + nums[2] += nums[0] + for i in range(3, len(nums)): + nums[i] += max(nums[i-3], nums[i-2]) + return max(nums[-1], nums[-2]) From 7baf829b5ab8623d9fe94fac4457ab2170ddc19e Mon Sep 17 00:00:00 2001 From: jeldo Date: Sat, 7 Dec 2024 21:17:38 +0900 Subject: [PATCH 5/5] 128 --- longest-consecutive-sequence/jeldo.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 longest-consecutive-sequence/jeldo.py diff --git a/longest-consecutive-sequence/jeldo.py b/longest-consecutive-sequence/jeldo.py new file mode 100644 index 000000000..c645fe334 --- /dev/null +++ b/longest-consecutive-sequence/jeldo.py @@ -0,0 +1,13 @@ +class Solution: + # O(n) + def longestConsecutive(self, nums: list[int]) -> int: + max_length = 0 + nums_set = set(nums) + for n in nums_set: + if n - 1 not in nums_set: + length = 0 + while n + length in nums_set: + length += 1 + max_length = max(max_length, length) + + return max_length