From c527927c580ec4610ad891133df1c562b1dd7501 Mon Sep 17 00:00:00 2001 From: jeldo Date: Tue, 3 Dec 2024 21:19:01 +0900 Subject: [PATCH 01/23] 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 02/23] 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 03/23] 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 04/23] 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 f4ac4335b61246204fdbc0e2929249cab8ba028b Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:26:45 +0900 Subject: [PATCH 05/23] add solution: contains duplicate --- contains-duplicate/SunaDu.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 contains-duplicate/SunaDu.py diff --git a/contains-duplicate/SunaDu.py b/contains-duplicate/SunaDu.py new file mode 100644 index 000000000..7daa1e6c7 --- /dev/null +++ b/contains-duplicate/SunaDu.py @@ -0,0 +1,22 @@ +''' +# Leetcode 217. Contains Duplicate + +use set to store distinct elements ๐Ÿ—‚๏ธ + +## Time and Space Complexity + +``` +TC: O(n) +SC: O(n) +``` + +### TC is O(n): +- iterating through the list just once to convert it to a set. + +### SC is O(n): +- creating a set to store the distinct elements of the list. +''' + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(nums) != len(set(nums)) From 227415b0d01287b5c4e9368cf85677ddc70696d6 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:34:22 +0900 Subject: [PATCH 06/23] fix: add new line for lint rules --- contains-duplicate/SunaDu.py | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/SunaDu.py b/contains-duplicate/SunaDu.py index 7daa1e6c7..fb9255c70 100644 --- a/contains-duplicate/SunaDu.py +++ b/contains-duplicate/SunaDu.py @@ -20,3 +20,4 @@ class Solution: def containsDuplicate(self, nums: List[int]) -> bool: return len(nums) != len(set(nums)) + From 75e6d4456ba0f40a287c395b704adfb0f7160b4d Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Wed, 4 Dec 2024 15:40:27 +0900 Subject: [PATCH 07/23] fix: change filename to GitHub username --- contains-duplicate/{SunaDu.py => dusunax.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contains-duplicate/{SunaDu.py => dusunax.py} (100%) diff --git a/contains-duplicate/SunaDu.py b/contains-duplicate/dusunax.py similarity index 100% rename from contains-duplicate/SunaDu.py rename to contains-duplicate/dusunax.py From be09081902970ef30f0c73b60b83ca1c59536e71 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Wed, 4 Dec 2024 22:53:52 +0900 Subject: [PATCH 08/23] add solution: valid-palindrome --- valid-palindrome/dusunax.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 valid-palindrome/dusunax.py diff --git a/valid-palindrome/dusunax.py b/valid-palindrome/dusunax.py new file mode 100644 index 000000000..2b21482f5 --- /dev/null +++ b/valid-palindrome/dusunax.py @@ -0,0 +1,28 @@ +''' +# Leetcode 125. Valid Palindrome + +use regex to filter out non-alphanumeric characters ๐Ÿ” + +## Time and Space Complexity + +``` +TC: O(n) +SC: O(n) +``` + +### TC is O(n): +- iterating through the string just once to filter out non-alphanumeric characters. + +### SC is O(n): +- creating a new string to store the filtered characters. +''' + +class Solution: + def isPalindrome(self, s: str) -> bool: + if s is " ": + return True + + reg = "[^a-z0-9]" + converted_s = re.sub(reg, "", s.lower()) + + return converted_s == converted_s[::-1] From 9a395e9cb8f4ccb48101b62d2cb2524f2c7cfbb4 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Thu, 5 Dec 2024 01:48:10 +0900 Subject: [PATCH 09/23] add solution: top-k-frequent-elements --- top-k-frequent-elements/dusunax.py | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 top-k-frequent-elements/dusunax.py diff --git a/top-k-frequent-elements/dusunax.py b/top-k-frequent-elements/dusunax.py new file mode 100644 index 000000000..f57df5ab9 --- /dev/null +++ b/top-k-frequent-elements/dusunax.py @@ -0,0 +1,33 @@ +''' +# Leetcode 347. Top K Frequent Elements + +use **Counter** to count the frequency of each element in the list ๐Ÿš€ +use **sorted()** to sort the elements by their frequency in descending order. + +## Time and Space Complexity + +``` +TC: O(n log n) +SC: O(n) +``` + +### TC is O(n log n): +- iterating through the list just once to count the frequency of each element. = O(n) +- sorting the elements by their frequency in descending order. = O(n log n) + +### SC is O(n): +- using a Counter to store the frequency of each element. = O(n) +- sorted() creates a new list that holds the elements of frequency_map. = O(n) +- result list that holds the top k frequent elements. = O(k) +''' + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + frequency_map = Counter(nums) # TC: O(n), SC: O(n) + sorted_frequencies = sorted(frequency_map.items(), key=lambda x: x[1], reverse=True) # TC: O(n log n), SC: O(n) + + result = [] # SC: O(k) + for e in sorted_frequencies: # TC: O(k) + result.append(e[0]) + + return result[0:k] \ No newline at end of file From 86658cbf96bd830216e5bbff433cef48271b185c Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Thu, 5 Dec 2024 01:56:51 +0900 Subject: [PATCH 10/23] fix: add new line for lint rules --- top-k-frequent-elements/dusunax.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/top-k-frequent-elements/dusunax.py b/top-k-frequent-elements/dusunax.py index f57df5ab9..4bb9ab3d1 100644 --- a/top-k-frequent-elements/dusunax.py +++ b/top-k-frequent-elements/dusunax.py @@ -30,4 +30,5 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: for e in sorted_frequencies: # TC: O(k) result.append(e[0]) - return result[0:k] \ No newline at end of file + return result[0:k] + \ No newline at end of file From 395f249375d7deda585237d8dad6230f79736048 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Thu, 5 Dec 2024 02:17:48 +0900 Subject: [PATCH 11/23] fix: fix new line --- top-k-frequent-elements/dusunax.py | 1 - 1 file changed, 1 deletion(-) diff --git a/top-k-frequent-elements/dusunax.py b/top-k-frequent-elements/dusunax.py index 4bb9ab3d1..ed2bf8547 100644 --- a/top-k-frequent-elements/dusunax.py +++ b/top-k-frequent-elements/dusunax.py @@ -31,4 +31,3 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: result.append(e[0]) return result[0:k] - \ No newline at end of file From e6c66d59a5a5fcc75b02f4194fa3de59260fa4a8 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Thu, 5 Dec 2024 03:25:18 +0900 Subject: [PATCH 12/23] fix solution: use equality operator, remove regex --- valid-palindrome/dusunax.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/valid-palindrome/dusunax.py b/valid-palindrome/dusunax.py index 2b21482f5..3cc7eff6e 100644 --- a/valid-palindrome/dusunax.py +++ b/valid-palindrome/dusunax.py @@ -19,10 +19,11 @@ class Solution: def isPalindrome(self, s: str) -> bool: - if s is " ": + if s == " ": return True - reg = "[^a-z0-9]" - converted_s = re.sub(reg, "", s.lower()) + s = s.lower() + converted_s = ''.join(c for c in s if c.isalnum()) return converted_s == converted_s[::-1] + From 89739e6e37f72e1f240e3a9b0ffc501d55cbe4b8 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Thu, 5 Dec 2024 03:33:59 +0900 Subject: [PATCH 13/23] fix solution: update comment --- valid-palindrome/dusunax.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/valid-palindrome/dusunax.py b/valid-palindrome/dusunax.py index 3cc7eff6e..52ee0fd1a 100644 --- a/valid-palindrome/dusunax.py +++ b/valid-palindrome/dusunax.py @@ -1,7 +1,7 @@ ''' # Leetcode 125. Valid Palindrome -use regex to filter out non-alphanumeric characters ๐Ÿ” +use `isalnum()` to filter out non-alphanumeric characters ๐Ÿ” ## Time and Space Complexity @@ -11,10 +11,12 @@ ``` ### TC is O(n): -- iterating through the string just once to filter out non-alphanumeric characters. +- iterating through the string just once to filter out non-alphanumeric characters. O(n) ### SC is O(n): -- creating a new string to store the filtered characters. +- `s.lower()` creates a new string. O(n) +- creating a new string `converted_s` to store the filtered characters. O(n) +- `converted_s[::-1]` creates a new reversed string. O(n) ''' class Solution: From 24792fe69be72a00ea96de2e10df3012c8913ea0 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Fri, 6 Dec 2024 18:12:08 +0900 Subject: [PATCH 14/23] add solution: longest-consecutive-sequence --- longest-consecutive-sequence/dusunax.py | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 longest-consecutive-sequence/dusunax.py diff --git a/longest-consecutive-sequence/dusunax.py b/longest-consecutive-sequence/dusunax.py new file mode 100644 index 000000000..b912c4807 --- /dev/null +++ b/longest-consecutive-sequence/dusunax.py @@ -0,0 +1,38 @@ +''' +# Leetcode 128. Longest Consecutive Sequence + +keep time complexity O(n) by iterating through the set and accessing elements in O(1) time. โš–๏ธ + +## Time and Space Complexity +``` +TC: O(n) +SC: O(n) +``` + +### TC is O(n): +- iterating through the set. O(n) +- accessing elements in the set. O(1) +- while loop incrementing `current_num` while `current_num + 1 in nums_set`. O(1) + +### SC is O(n): +- creating a set from the list of numbers. O(n) +''' + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + nums_set = set(nums) # O(n) + longest_sequence = 0 + + for num in nums_set: # O(n) + if (num - 1) not in nums_set: + current_num = num + current_sequence = 1 + + while current_num + 1 in nums_set: # O(1) + current_num += 1 + current_sequence += 1 + + longest_sequence = max(current_sequence, longest_sequence) + + return longest_sequence + From 3dff9204762bdd11ac3073420553983789fa4241 Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Fri, 6 Dec 2024 18:42:30 +0900 Subject: [PATCH 15/23] update solution: top-k-frequent-elements --- top-k-frequent-elements/dusunax.py | 42 +++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/top-k-frequent-elements/dusunax.py b/top-k-frequent-elements/dusunax.py index ed2bf8547..1e8ee7a4c 100644 --- a/top-k-frequent-elements/dusunax.py +++ b/top-k-frequent-elements/dusunax.py @@ -2,23 +2,44 @@ # Leetcode 347. Top K Frequent Elements use **Counter** to count the frequency of each element in the list ๐Ÿš€ -use **sorted()** to sort the elements by their frequency in descending order. + +- **solution 1**: use **sorted()** to sort the elements by their frequency in descending order. +- **solution 2**: use **bucket sort** to sort the elements by their frequency in descending order. (efficient!) ## Time and Space Complexity +### solution 1: topKFrequent() + ``` TC: O(n log n) SC: O(n) ``` -### TC is O(n log n): +#### TC is O(n log n): - iterating through the list just once to count the frequency of each element. = O(n) - sorting the elements by their frequency in descending order. = O(n log n) -### SC is O(n): +#### SC is O(n): - using a Counter to store the frequency of each element. = O(n) - sorted() creates a new list that holds the elements of frequency_map. = O(n) - result list that holds the top k frequent elements. = O(k) + +### solution 2: topKFrequentBucketSort() + +``` +TC: O(n) +SC: O(n) +``` + +#### TC is O(n): +- iterating through the list just once to count the frequency of each element. = O(n) +- creating **buckets** to store the elements by their frequency. = O(n) +- iterating through the buckets in reverse order to get only the top k frequent elements. = O(n) + +#### SC is O(n): +- using a Counter to store the frequency of each element. = O(n) +- using buckets to store the elements by their frequency. = O(n) +- result list that holds only the top k frequent elements. = O(k) ''' class Solution: @@ -31,3 +52,18 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: result.append(e[0]) return result[0:k] + + def topKFrequentBucketSort(self, nums: List[int], k: int) -> List[int]: + frequency_map = Counter(nums) + n = len(nums) + buckets = [[] for _ in range(n + 1)] + + for num, freq in frequency_map.items(): + buckets[freq].append(num) + + result = [] + for i in range(len(buckets) - 1, 0, -1): + for num in buckets[i]: + result.append(num) + if len(result) == k: + return result From 4b036afa9e24f5ca12c760af524ce2832c95ef7a Mon Sep 17 00:00:00 2001 From: JisooPyo Date: Sat, 7 Dec 2024 14:26:07 +0900 Subject: [PATCH 16/23] contains duplicate --- contains-duplicate/JisooPyo.kt | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 contains-duplicate/JisooPyo.kt diff --git a/contains-duplicate/JisooPyo.kt b/contains-duplicate/JisooPyo.kt new file mode 100644 index 000000000..a15e170e8 --- /dev/null +++ b/contains-duplicate/JisooPyo.kt @@ -0,0 +1,38 @@ +package leetcode_study + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test + +/** + * Leetcode + * 217. Contains Duplicate + * Easy + */ +class ContainsDuplicate { + /** + * Runtime: 17 ms(Beats: 80.99 %) + * Time Complexity: O(n) + * - ๋ฐฐ์—ด ์ˆœํšŒ + * + * Memory: 50.63 MB(Beats: 70.32 %) + * Space Complexity: O(n) + * - HashSet์— ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ฐฐ์—ด ์›์†Œ ๋ชจ๋‘ ์ €์žฅ + */ + fun containsDuplicate(nums: IntArray): Boolean { + val set = hashSetOf() + for (i in nums) { + if (set.contains(i)) { + return true + } + set.add(i) + } + return false + } + + @Test + fun test() { + containsDuplicate(intArrayOf(1, 2, 3, 1)) shouldBe true + containsDuplicate(intArrayOf(1, 2, 3, 4)) shouldBe false + containsDuplicate(intArrayOf(1, 1, 1, 3, 3, 4, 3, 2, 4, 2)) shouldBe true + } +} From 7c19091c0be1e1dbed60d08d4bdfd4282b3e19fd Mon Sep 17 00:00:00 2001 From: JisooPyo Date: Sat, 7 Dec 2024 14:26:14 +0900 Subject: [PATCH 17/23] house robber --- house-robber/JisooPyo.kt | 74 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 house-robber/JisooPyo.kt diff --git a/house-robber/JisooPyo.kt b/house-robber/JisooPyo.kt new file mode 100644 index 000000000..6c2b049d6 --- /dev/null +++ b/house-robber/JisooPyo.kt @@ -0,0 +1,74 @@ +package leetcode_study + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test +import kotlin.math.max + +/** + * Leetcode + * 198. House Robber + * Medium + * + * ์‚ฌ์šฉ๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜: Dynamic Programming + * + * i๋ฒˆ์งธ ์ง‘์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ธˆ์•ก์€ ๋‹ค์Œ ๋‘ ๊ฐ€์ง€ ์ค‘ ํฐ ๊ฐ’์ž…๋‹ˆ๋‹ค. + * - (i-2)๋ฒˆ์งธ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ธˆ์•ก + ํ˜„์žฌ ์ง‘์˜ ๊ธˆ์•ก + * - (i-1)๋ฒˆ์งธ ์ง‘๊นŒ์ง€์˜ ์ตœ๋Œ€ ๊ธˆ์•ก + */ +class HouseRobber { + /** + * Runtime: 0 ms(Beats: 100.00 %) + * Time Complexity: O(n) + * + * Memory: 34.65 MB(Beats: 40.50 %) + * Space Complexity: O(n) + */ + fun rob(nums: IntArray): Int { + if (nums.size == 1) { + return nums[0] + } + if (nums.size == 2) { + return max(nums[0], nums[1]) + } + val dp = IntArray(nums.size) + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + for (i in 2 until nums.size) { + dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]) + } + return dp[nums.size - 1] + } + + /** + * ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐœ์„  + * Runtime: 0 ms(Beats: 100.00 %) + * Time Complexity: O(n) + * + * Memory: 34.95 MB(Beats: 36.98 %) + * Space Complexity: O(1) + */ + fun rob2(nums: IntArray): Int { + if (nums.size == 1) return nums[0] + if (nums.size == 2) return max(nums[0], nums[1]) + + var twoBack = nums[0] + var oneBack = max(nums[0], nums[1]) + var current = oneBack + + for (i in 2 until nums.size) { + current = max(twoBack + nums[i], oneBack) + twoBack = oneBack + oneBack = current + } + + return current + } + + @Test + fun test() { + rob(intArrayOf(1, 2, 3, 1)) shouldBe 4 + rob(intArrayOf(2, 7, 9, 3, 1)) shouldBe 12 + rob2(intArrayOf(1, 2, 3, 1)) shouldBe 4 + rob2(intArrayOf(2, 7, 9, 3, 1)) shouldBe 12 + } +} From 40c2fe9c324524cbf6c01e52e66101e732732e92 Mon Sep 17 00:00:00 2001 From: JisooPyo Date: Sat, 7 Dec 2024 14:26:24 +0900 Subject: [PATCH 18/23] longest consecutive sequence --- longest-consecutive-sequence/JisooPyo.kt | 81 ++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 longest-consecutive-sequence/JisooPyo.kt diff --git a/longest-consecutive-sequence/JisooPyo.kt b/longest-consecutive-sequence/JisooPyo.kt new file mode 100644 index 000000000..7bf137517 --- /dev/null +++ b/longest-consecutive-sequence/JisooPyo.kt @@ -0,0 +1,81 @@ +package leetcode_study + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test +import kotlin.math.max + +/** + * Leetcode + * 128. Longest Consecutive Sequence + * Medium + */ +class LongestConsecutiveSequence { + /** + * Runtime: 58 ms(Beats: 79.06 %) + * Time Complexity: O(n) + * - while ๋ฃจํ”„์˜ ์ด ๋ฐ˜๋ณต ํšŸ์ˆ˜๋Š” n์„ ๋„˜์„ ์ˆ˜ ์—†๋‹ค. + * + * Memory: 62.65 MB(Beats: 10.48 %) + * Space Complexity: O(n) + */ + fun longestConsecutive(nums: IntArray): Int { + val numsSet: MutableSet = nums.toHashSet() + val startSet: MutableSet = hashSetOf() + + // ์ˆ˜์—ด์˜ ์‹œ์ž‘์ ์ด ๋  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค. + for (num in numsSet) { + if (!numsSet.contains(num - 1)) { + startSet.add(num) + } + } + var answer = 0 + for (start in startSet) { + // ์ˆ˜์—ด์˜ ์‹œ์ž‘์ ๋ถ€ํ„ฐ ๋ช‡ ๊ฐœ ๊นŒ์ง€ numsSet์— ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. + var count = 0 + var first = start + while (numsSet.contains(first)) { + first++ + count++ + } + // ์ตœ๋Œ€ ์ˆ˜์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. + answer = max(answer, count) + } + return answer + } + + /** + * ์œ„ ํ’€์ด์—์„œ startSet์„ ์ œ๊ฑฐํ•˜์—ฌ ๊ณต๊ฐ„์ ์œผ๋กœ ํšจ์œจ์ ์ธ ํ’€์ด + * Runtime: 63 ms(Beats: 65.70 %) + * Time Complexity: O(n) + * + * Memory: 58.47 MB(Beats: 70.81 %) + * Space Complexity: O(n) + */ + fun longestConsecutive2(nums: IntArray): Int { + val numsSet = nums.toHashSet() + var maxLength = 0 + + for (num in numsSet) { + if (!numsSet.contains(num - 1)) { + var currentNum = num + var currentLength = 0 + + while (numsSet.contains(currentNum)) { + currentLength++ + currentNum++ + } + maxLength = max(maxLength, currentLength) + } + } + return maxLength + } + + @Test + fun test() { + longestConsecutive(intArrayOf(100, 4, 200, 1, 3, 2)) shouldBe 4 + longestConsecutive(intArrayOf(0, 3, 7, 2, 5, 8, 4, 6, 0, 1)) shouldBe 9 + + longestConsecutive2(intArrayOf(100, 4, 200, 1, 3, 2)) shouldBe 4 + longestConsecutive2(intArrayOf(0, 3, 7, 2, 5, 8, 4, 6, 0, 1)) shouldBe 9 + } +} From 920d92b032479cb1e7fc361e4d6901ce10f2d652 Mon Sep 17 00:00:00 2001 From: JisooPyo Date: Sat, 7 Dec 2024 14:26:34 +0900 Subject: [PATCH 19/23] top k frequent elements --- top-k-frequent-elements/JisooPyo.kt | 76 +++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 top-k-frequent-elements/JisooPyo.kt diff --git a/top-k-frequent-elements/JisooPyo.kt b/top-k-frequent-elements/JisooPyo.kt new file mode 100644 index 000000000..d600753f7 --- /dev/null +++ b/top-k-frequent-elements/JisooPyo.kt @@ -0,0 +1,76 @@ +package leetcode_study + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test +import java.util.* + +/** + * Leetcode + * 347. Top K Frequent Elements + * Medium + */ +class TopKFrequentElements { + /** + * Runtime: 30 ms(Beats: 68.62 %) + * Time Complexity: O(n log n) + * - list ์ •๋ ฌ + * + * Memory: 42.20 MB(Beats: 58.82 %) + * Space Complexity: O(n) + */ + fun topKFrequent(nums: IntArray, k: Int): IntArray { + val countMap: MutableMap = HashMap() + + for (num in nums) { + countMap[num] = countMap.getOrDefault(num, 0) + 1 + } + + val list = mutableListOf() + for (key in countMap.keys) { + list.add(Node(key, countMap[key]!!)) + } + list.sortDescending() + + val answer = IntArray(k) + for (i in 0 until k) { + answer[i] = list[i].value + } + return answer + } + + /** + * ๊ฐœ์„ ๋œ ๋ฒ„์ „: ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉ + * + * Runtime: 19 ms(Beats: 96.30 %) + * Time Complexity: O(n log n) + * + * Memory: 44.83 MB(Beats: 18.35 %) + * Space Complexity: O(n) + */ + fun topKFrequent2(nums: IntArray, k: Int): IntArray { + val countMap = nums.groupBy { it } + .mapValues { it.value.size } + + val pq = PriorityQueue(compareByDescending { it.count }) + countMap.forEach { (num, count) -> + pq.offer(Node(num, count)) + } + + return IntArray(k) { pq.poll().value } + } + + data class Node(var value: Int, var count: Int) : Comparable { + override fun compareTo(other: Node): Int { + return this.count.compareTo(other.count) + } + } + + @Test + fun test() { + topKFrequent(intArrayOf(1, 1, 1, 2, 2, 3), 2) shouldBe intArrayOf(1, 2) + topKFrequent(intArrayOf(1), 1) shouldBe intArrayOf(1) + + topKFrequent2(intArrayOf(1, 1, 1, 2, 2, 3), 2) shouldBe intArrayOf(1, 2) + topKFrequent2(intArrayOf(1), 1) shouldBe intArrayOf(1) + } +} From 920c9a9aaa3d1798a4e90410a5fcd9f62ce495a6 Mon Sep 17 00:00:00 2001 From: JisooPyo Date: Sat, 7 Dec 2024 14:26:53 +0900 Subject: [PATCH 20/23] valid palindrome --- valid-palindrome/JisooPyo.kt | 101 +++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 valid-palindrome/JisooPyo.kt diff --git a/valid-palindrome/JisooPyo.kt b/valid-palindrome/JisooPyo.kt new file mode 100644 index 000000000..2997dec8f --- /dev/null +++ b/valid-palindrome/JisooPyo.kt @@ -0,0 +1,101 @@ +package leetcode_study + +import io.kotest.matchers.shouldBe +import org.junit.jupiter.api.Test + +/** + * Leetcode + * 125. Valid Palindrome + * Easy + */ +class ValidPalindrome { + /** + * Runtime: 4 ms(Beats: 98.27 %) + * Time Complexity: O(n) + * + * Memory: 38.22 MB(Beats: 46.74 %) + * Space Complexity: O(1) + */ + fun isPalindrome(s: String): Boolean { + var left = 0 + var right = s.length - 1 + while (left <= right) { + when (s[left]) { + // ์™ผ์ชฝ์˜ ๋ฌธ์ž๊ฐ€ alphanumeric์ผ ๋•Œ + in 'a'..'z', in 'A'..'Z', in '0'..'9' -> { + + when (s[right]) { + // ์˜ค๋ฅธ์ชฝ์˜ ๋ฌธ์ž๊ฐ€ alphanumeric์ผ ๋•Œ + in 'a'..'z', in 'A'..'Z', in '0'..'9' -> { + // ๋ฌธ์ž ๋น„๊ต + if (s[left].equals(s[right], true)) { + left++ + right-- + continue + } else { + return false + } + } + // ์˜ค๋ฅธ์ชฝ์˜ ๋ฌธ์ž๊ฐ€ alphanumeric์ด ์•„๋‹ ๋•Œ + else -> { + right-- + continue + } + } + } + + // ์™ผ์ชฝ์˜ ๋ฌธ์ž๊ฐ€ alphanumeric์ด ์•„๋‹ ๋•Œ + else -> { + left++ + continue + } + } + } + return true + } + + /** + * ๊ฐœ์„ ํ•œ ๋ฒ„์ „ + * Runtime: 5 ms(Beats: 87.14 %) + * Time Complexity: O(n) + * + * Memory: 37.76 MB(Beats: 61.52 %) + * Space Complexity: O(1) + */ + fun isPalindrome2(s: String): Boolean { + var left = 0 + var right = s.length - 1 + + while (left < right) { + // ์™ผ์ชฝ์—์„œ ์œ ํšจํ•œ ๋ฌธ์ž๋ฅผ ์ฐพ์Œ + while (left < right && !s[left].isLetterOrDigit()) { + left++ + } + + // ์˜ค๋ฅธ์ชฝ์—์„œ ์œ ํšจํ•œ ๋ฌธ์ž๋ฅผ ์ฐพ์Œ + while (left < right && !s[right].isLetterOrDigit()) { + right-- + } + + // ๋ฌธ์ž ๋น„๊ต + if (!s[left].equals(s[right], ignoreCase = true)) { + return false + } + + left++ + right-- + } + return true + } + + @Test + fun test() { + isPalindrome("A man, a plan, a canal: Panama") shouldBe true + isPalindrome("race a car") shouldBe false + isPalindrome(" ") shouldBe true + + isPalindrome2("A man, a plan, a canal: Panama") shouldBe true + isPalindrome2("race a car") shouldBe false + isPalindrome2(" ") shouldBe true + } +} From 055969d5f165bc9d0cdecc2bdbf1ac9611a65cfe Mon Sep 17 00:00:00 2001 From: Dusuna <94776135+dusunax@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:07:07 +0900 Subject: [PATCH 21/23] add solution: house-robber --- house-robber/dusunax.py | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 house-robber/dusunax.py diff --git a/house-robber/dusunax.py b/house-robber/dusunax.py new file mode 100644 index 000000000..38c238942 --- /dev/null +++ b/house-robber/dusunax.py @@ -0,0 +1,44 @@ +''' +# Leetcode 198. House Robber + +use **dynamic programming** to solve this problem. (bottom-up approach) ๐Ÿงฉ + +choose bottom-up approach for less space complexity. + +## DP relation + +``` +dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) +``` + +- **dp[i - 1]:** skip and take the value from the previous house +- **dp[i - 2]:** rob the current house, add its value to the maximum money from two houses before + +## Time and Space Complexity + +``` +TC: O(n) +SC: O(n) +``` + +### TC is O(n): +- iterating through the list just once to calculate the maximum money. = O(n) + +### SC is O(n): +- using a list to store the maximum money at each house. = O(n) + +''' + +class Solution: + def rob(self, nums: List[int]) -> int: + if len(nums) == 1: + return nums[0] + + dp = [0] * len(nums) + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + for i in range(2, len(nums)): + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]) + + return dp[-1] From 7baf829b5ab8623d9fe94fac4457ab2170ddc19e Mon Sep 17 00:00:00 2001 From: jeldo Date: Sat, 7 Dec 2024 21:17:38 +0900 Subject: [PATCH 22/23] 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 From f750a0b826f1154c9c9be7872889caf9b21498e7 Mon Sep 17 00:00:00 2001 From: HC-kang Date: Mon, 9 Dec 2024 19:22:17 +0900 Subject: [PATCH 23/23] chore: fix for integration filename test --- .github/workflows/integration.yaml | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index 3a5371562..15b4e4174 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -91,17 +91,14 @@ jobs: echo "## ํŒŒ์ผ๋ช… ๊ทœ์น™ ์œ„๋ฐ˜" >> $GITHUB_STEP_SUMMARY for file in $files; do - if [ -f "$file" ]; then + # ํŒŒ์ผ๋ช…๋งŒ ์ถ”์ถœ (๊ฒฝ๋กœ ์ œ์™ธ) + filename=$(basename "$file") - # ํŒŒ์ผ๋ช…๋งŒ ์ถ”์ถœ (๊ฒฝ๋กœ ์ œ์™ธ) - filename=$(basename "$file") - - # ํŒŒ์ผ๋ช…์ด GitHub๊ณ„์ •๋ช…์ธ์ง€ ํ™•์ธ - shopt -s nocasematch - if [[ ! "$filename" = "$pr_author"* ]]; then - echo "- $file" >> $GITHUB_STEP_SUMMARY - success=false - fi + # ํŒŒ์ผ๋ช…์ด GitHub๊ณ„์ •๋ช…์ธ์ง€ ํ™•์ธ + shopt -s nocasematch + if [[ ! "$filename" = "$pr_author"* ]]; then + echo "- $file" >> $GITHUB_STEP_SUMMARY + success=false fi done