From a48c32e01ed01d44332123b3762def53e528b413 Mon Sep 17 00:00:00 2001 From: csucom Date: Sun, 24 Nov 2024 22:40:24 +0900 Subject: [PATCH 01/37] add sample code for testing --- 3sum/csucom.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 3sum/csucom.py diff --git a/3sum/csucom.py b/3sum/csucom.py new file mode 100644 index 000000000..9def7cf98 --- /dev/null +++ b/3sum/csucom.py @@ -0,0 +1,3 @@ +''' + test code +''' \ No newline at end of file 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 02/37] 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 03/37] 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 04/37] 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 05/37] 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 06/37] 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 07/37] 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 08/37] 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 09/37] 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 10/37] 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 11/37] 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 12/37] 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 13/37] 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 14/37] 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 15/37] 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 16/37] 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 17/37] 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 aeb9221bea11f300da9dfc3a6260a08f84b86274 Mon Sep 17 00:00:00 2001 From: Yooncheol_Oh Date: Sat, 7 Dec 2024 17:19:12 +0900 Subject: [PATCH 18/37] Week1 solutions --- contains-duplicate/Yooncheol_Oh.java | 16 +++++++++ house-robber/Yooncheol_Oh.java | 19 +++++++++++ .../Yooncheol_Oh.java | 34 +++++++++++++++++++ top-k-frequent-elements/Yooncheol_Oh.java | 21 ++++++++++++ valid-palindrome/Yooncheol_Oh.java | 16 +++++++++ 5 files changed, 106 insertions(+) create mode 100644 contains-duplicate/Yooncheol_Oh.java create mode 100644 house-robber/Yooncheol_Oh.java create mode 100644 longest-consecutive-sequence/Yooncheol_Oh.java create mode 100644 top-k-frequent-elements/Yooncheol_Oh.java create mode 100644 valid-palindrome/Yooncheol_Oh.java diff --git a/contains-duplicate/Yooncheol_Oh.java b/contains-duplicate/Yooncheol_Oh.java new file mode 100644 index 000000000..c3c1cbd85 --- /dev/null +++ b/contains-duplicate/Yooncheol_Oh.java @@ -0,0 +1,16 @@ +class Solution { + /** + * ์‹œ๊ฐ„ ๋ณต์žก๋„: O(N) + * ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N) + */ + public boolean containsDuplicate(int[] nums) { + Set set = new HashSet<>(); + + for (int num : nums) { + if (set.contains(num)) return true; + set.add(num); + } + + return false; + } +} diff --git a/house-robber/Yooncheol_Oh.java b/house-robber/Yooncheol_Oh.java new file mode 100644 index 000000000..358308310 --- /dev/null +++ b/house-robber/Yooncheol_Oh.java @@ -0,0 +1,19 @@ +class Solution { + public int rob(int[] nums) { + //๋ฐฐ์—ด ๊ธธ์ด 0์ด๋ฉด ํ„ธ ์ˆ˜ ์žˆ๋Š” ์ง‘์ด ์—†์Œ. + if (nums.length == 0) return 0; + //๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ 1์ด๋ฉด ํ•œ ์ง‘๋งŒ ํ„ธ ์ˆ˜ ์žˆ์Œ. + if (nums.length == 1) return nums[0]; + + //๋™์  ๊ณ„ํš๋ฒ•์œผ๋กœ ํ’€์ด + int[] dp = new int[nums.length]; + dp[0] = nums[0]; + dp[1] = Math.max(nums[0], nums[1]); + + //๋ฐฐ์—ด ํฌ๊ธฐ๊ฐ€ 2์ด์ƒ์ผ ๊ฒฝ์šฐ ์ตœ๋Œ€ ๊ธˆ์•ก์˜ ๋ฒ”์œ„ ํ™•์žฅ + for (int i = 2; i < nums.length; i++) { + dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[nums.length - 1]; + } +} diff --git a/longest-consecutive-sequence/Yooncheol_Oh.java b/longest-consecutive-sequence/Yooncheol_Oh.java new file mode 100644 index 000000000..6932478b1 --- /dev/null +++ b/longest-consecutive-sequence/Yooncheol_Oh.java @@ -0,0 +1,34 @@ +import java.util.*; + +class Solution { + public int longestConsecutive(int[] nums) { + if (nums == null || nums.length == 0) return 0; + + //๋ชจ๋“  ์š”์†Œ HashSet ์‚ฝ์ž… + HashSet set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + int longest = 0; + + // ์‹œ์ž‘์ง€์  ์ฒดํฌ + for (int num : nums) { + //๋ฐฐ์—ด ์š”์†Œ๋ณด๋‹ค 1 ์ž‘์€ ์ˆ˜ ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ์‹œ์ž‘ ์ง€์ ์ด ๋จ + if (!set.contains(num - 1)) { + int start = num; + int currentLength = 1; + + // 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ์—ฐ์†๋œ ์ˆ˜์˜ ๊ฐœ์ˆ˜ ํƒ์ƒ‰ + while (set.contains(start + 1)) { + start++; + currentLength++; + } + // ๊ธฐ์กด longest์™€ ํ˜„์žฌ ์—ฐ์†๋œ ์ˆ˜๋ฅผ ๋น„๊ต + longest = Math.max(longest, currentLength); + } + } + + return longest; + } +} diff --git a/top-k-frequent-elements/Yooncheol_Oh.java b/top-k-frequent-elements/Yooncheol_Oh.java new file mode 100644 index 000000000..55e4f1598 --- /dev/null +++ b/top-k-frequent-elements/Yooncheol_Oh.java @@ -0,0 +1,21 @@ +import java.util.*; + +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map map = new HashMap<>(); + for (int num : nums) { + map.put(num, map.getOrDefault(num, 0) + 1); + } + + //value๊ฐ’์„ ํ†ตํ•œ key ์ •๋ ฌ + List list = new ArrayList<>(map.keySet()); + list.sort((a,b)->map.get(b) - map.get(a)); + + //์ƒ์œ„ k๊ฐœ key ์ถ”์ถœ + int[] res = new int[k]; + for (int i = 0; i < k; i++) { + res[i] = list.get(i); + } + return res; + } +} diff --git a/valid-palindrome/Yooncheol_Oh.java b/valid-palindrome/Yooncheol_Oh.java new file mode 100644 index 000000000..ed5a82675 --- /dev/null +++ b/valid-palindrome/Yooncheol_Oh.java @@ -0,0 +1,16 @@ +class Solution { + public boolean isPalindrome(String s) { + //์ •๊ทœ์‹์œผ๋กœ ์˜๋ฌธ,์ˆซ์ž ์•„๋‹Œ ๊ฒฝ์šฐ "" ์น˜ํ™˜ + //StringBuilder๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด ๊ฐ€๊ณต + //๊ฑฐ๊พธ๋กœ ๋’ค์ง‘์€ ๊ฒƒ๊ณผ ์›๋ž˜์˜ ๋ฌธ์ž์—ด์ด ์ผ์น˜ํ•  ๊ฒฝ์šฐ ํŒฐ๋ฆฐ๋“œ๋กฌ + s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); + StringBuilder sb = new StringBuilder(s); + if(sb.reverse().toString().equals(s)){ + return true; + } + + return false; + } +} + + From 16ac10c361de401dafedbe9b81a95a45c52d66bc Mon Sep 17 00:00:00 2001 From: Yooncheol_Oh Date: Sat, 7 Dec 2024 17:32:08 +0900 Subject: [PATCH 19/37] Week1 solutions --- contains-duplicate/{Yooncheol_Oh.java => 5YoonCheol.java} | 0 house-robber/{Yooncheol_Oh.java => 5YoonCheol.java} | 0 .../{Yooncheol_Oh.java => 5YoonCheol.java} | 0 top-k-frequent-elements/{Yooncheol_Oh.java => 5YoonCheol.java} | 0 valid-palindrome/{Yooncheol_Oh.java => 5YoonCheol.java} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename contains-duplicate/{Yooncheol_Oh.java => 5YoonCheol.java} (100%) rename house-robber/{Yooncheol_Oh.java => 5YoonCheol.java} (100%) rename longest-consecutive-sequence/{Yooncheol_Oh.java => 5YoonCheol.java} (100%) rename top-k-frequent-elements/{Yooncheol_Oh.java => 5YoonCheol.java} (100%) rename valid-palindrome/{Yooncheol_Oh.java => 5YoonCheol.java} (100%) diff --git a/contains-duplicate/Yooncheol_Oh.java b/contains-duplicate/5YoonCheol.java similarity index 100% rename from contains-duplicate/Yooncheol_Oh.java rename to contains-duplicate/5YoonCheol.java diff --git a/house-robber/Yooncheol_Oh.java b/house-robber/5YoonCheol.java similarity index 100% rename from house-robber/Yooncheol_Oh.java rename to house-robber/5YoonCheol.java diff --git a/longest-consecutive-sequence/Yooncheol_Oh.java b/longest-consecutive-sequence/5YoonCheol.java similarity index 100% rename from longest-consecutive-sequence/Yooncheol_Oh.java rename to longest-consecutive-sequence/5YoonCheol.java diff --git a/top-k-frequent-elements/Yooncheol_Oh.java b/top-k-frequent-elements/5YoonCheol.java similarity index 100% rename from top-k-frequent-elements/Yooncheol_Oh.java rename to top-k-frequent-elements/5YoonCheol.java diff --git a/valid-palindrome/Yooncheol_Oh.java b/valid-palindrome/5YoonCheol.java similarity index 100% rename from valid-palindrome/Yooncheol_Oh.java rename to valid-palindrome/5YoonCheol.java 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 20/37] 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 aa2e678ca6b1d47e6115e269f4681aa41f75e2fa Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:10:51 +0900 Subject: [PATCH 21/37] contains-duplicate --- contains-duplicate/jejufather.cpp | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 contains-duplicate/jejufather.cpp diff --git a/contains-duplicate/jejufather.cpp b/contains-duplicate/jejufather.cpp new file mode 100644 index 000000000..2b2418ea6 --- /dev/null +++ b/contains-duplicate/jejufather.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +bool containsDuplicate(int* nums, int numsSize) { + char* pflag = (char*)malloc(1000000001); + char* mflag = (char*)malloc(1000000001); + memset(pflag, 0, 1000000001); + memset(mflag, 0, 1000000001); + for (int i = 0; i < numsSize; ++i) { + if (nums[i] < 0) { + if (mflag[-nums[i]] == 1) { + free(pflag); + free(mflag); + return true; + } + mflag[-nums[i]] = 1; + } + else { + if (pflag[nums[i]] == 1) { + free(pflag); + free(mflag); + return true; + } + pflag[nums[i]] = 1; + } + } + free(pflag); + free(mflag); + return false; +} + +void main(void) { + int test[100001] = {0}; + printf("%d\n", containsDuplicate(test, 1)); +} \ No newline at end of file From cc905f54fa2ad90e2a2830069641ea1b252acfb3 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:29:27 +0900 Subject: [PATCH 22/37] contains-duplicate --- contains-duplicate/jejufather.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/jejufather.cpp b/contains-duplicate/jejufather.cpp index 2b2418ea6..81221d523 100644 --- a/contains-duplicate/jejufather.cpp +++ b/contains-duplicate/jejufather.cpp @@ -30,6 +30,7 @@ bool containsDuplicate(int* nums, int numsSize) { return false; } + void main(void) { int test[100001] = {0}; printf("%d\n", containsDuplicate(test, 1)); From c3e92cfecb4ecf6e9945af6e1fcc1063569b5f05 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:31:46 +0900 Subject: [PATCH 23/37] Delete 3sum/csucom.py --- 3sum/csucom.py | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 3sum/csucom.py diff --git a/3sum/csucom.py b/3sum/csucom.py deleted file mode 100644 index 9def7cf98..000000000 --- a/3sum/csucom.py +++ /dev/null @@ -1,3 +0,0 @@ -''' - test code -''' \ No newline at end of file From 7ed155864f2fa6c8da8a4a78de0ed7a13215942c Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:36:08 +0900 Subject: [PATCH 24/37] contains-duplicate --- contains-duplicate/jejufather.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contains-duplicate/jejufather.cpp b/contains-duplicate/jejufather.cpp index 81221d523..61544a620 100644 --- a/contains-duplicate/jejufather.cpp +++ b/contains-duplicate/jejufather.cpp @@ -30,8 +30,7 @@ bool containsDuplicate(int* nums, int numsSize) { return false; } - void main(void) { int test[100001] = {0}; printf("%d\n", containsDuplicate(test, 1)); -} \ No newline at end of file +} From 85aa203fe129599b815115462af85b7916e9cbe3 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:48:05 +0900 Subject: [PATCH 25/37] contains-duplicate --- contains-duplicate/csucom.cpp | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 contains-duplicate/csucom.cpp diff --git a/contains-duplicate/csucom.cpp b/contains-duplicate/csucom.cpp new file mode 100644 index 000000000..61544a620 --- /dev/null +++ b/contains-duplicate/csucom.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +bool containsDuplicate(int* nums, int numsSize) { + char* pflag = (char*)malloc(1000000001); + char* mflag = (char*)malloc(1000000001); + memset(pflag, 0, 1000000001); + memset(mflag, 0, 1000000001); + for (int i = 0; i < numsSize; ++i) { + if (nums[i] < 0) { + if (mflag[-nums[i]] == 1) { + free(pflag); + free(mflag); + return true; + } + mflag[-nums[i]] = 1; + } + else { + if (pflag[nums[i]] == 1) { + free(pflag); + free(mflag); + return true; + } + pflag[nums[i]] = 1; + } + } + free(pflag); + free(mflag); + return false; +} + +void main(void) { + int test[100001] = {0}; + printf("%d\n", containsDuplicate(test, 1)); +} From 622ff7aed7fcaab2fd5b5aed6535ea7675a115b1 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 09:19:37 +0900 Subject: [PATCH 26/37] Delete contains-duplicate/jejufather.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ํŒŒ์ผ๋ช…์„ ๊ฐ€์ง„ ํŒŒ์ผ ์‚ญ์ œ --- contains-duplicate/jejufather.cpp | 36 ------------------------------- 1 file changed, 36 deletions(-) delete mode 100644 contains-duplicate/jejufather.cpp diff --git a/contains-duplicate/jejufather.cpp b/contains-duplicate/jejufather.cpp deleted file mode 100644 index 61544a620..000000000 --- a/contains-duplicate/jejufather.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include - -bool containsDuplicate(int* nums, int numsSize) { - char* pflag = (char*)malloc(1000000001); - char* mflag = (char*)malloc(1000000001); - memset(pflag, 0, 1000000001); - memset(mflag, 0, 1000000001); - for (int i = 0; i < numsSize; ++i) { - if (nums[i] < 0) { - if (mflag[-nums[i]] == 1) { - free(pflag); - free(mflag); - return true; - } - mflag[-nums[i]] = 1; - } - else { - if (pflag[nums[i]] == 1) { - free(pflag); - free(mflag); - return true; - } - pflag[nums[i]] = 1; - } - } - free(pflag); - free(mflag); - return false; -} - -void main(void) { - int test[100001] = {0}; - printf("%d\n", containsDuplicate(test, 1)); -} From aef971c7ac6b286e43c15acb55b5d0a55a02b790 Mon Sep 17 00:00:00 2001 From: csucom Date: Mon, 9 Dec 2024 09:55:01 +0900 Subject: [PATCH 27/37] contains-duplicate solution --- contains-duplicate/csucom.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contains-duplicate/csucom.cpp b/contains-duplicate/csucom.cpp index 61544a620..1fcb4171e 100644 --- a/contains-duplicate/csucom.cpp +++ b/contains-duplicate/csucom.cpp @@ -1,4 +1,4 @@ -#include +//#include #include #include @@ -30,7 +30,7 @@ bool containsDuplicate(int* nums, int numsSize) { return false; } -void main(void) { - int test[100001] = {0}; - printf("%d\n", containsDuplicate(test, 1)); -} +// void main(void) { +// int test[100001] = {0}; +// printf("%d\n", containsDuplicate(test, 1)); +// } From 596e53ad8b318e2ddbb9cbeaf83e01be433cc939 Mon Sep 17 00:00:00 2001 From: HC-kang Date: Mon, 9 Dec 2024 20:50:43 +0900 Subject: [PATCH 28/37] chore: fix for integration filename --- .github/workflows/integration.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/integration.yaml b/.github/workflows/integration.yaml index 3a5371562..b74a5f646 100644 --- a/.github/workflows/integration.yaml +++ b/.github/workflows/integration.yaml @@ -85,7 +85,7 @@ jobs: - name: Check filename rules if: ${{ steps.pr-labels.outputs.has_maintenance != 'true' }} run: | - files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | tr -d '"') + files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | tr -d '"') pr_author="${{ github.event.pull_request.user.login }}" success=true From 1101bc145940719a1194c742bd623171579678ee Mon Sep 17 00:00:00 2001 From: csucom Date: Sun, 24 Nov 2024 22:40:24 +0900 Subject: [PATCH 29/37] add sample code for testing --- 3sum/csucom.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 3sum/csucom.py diff --git a/3sum/csucom.py b/3sum/csucom.py new file mode 100644 index 000000000..9def7cf98 --- /dev/null +++ b/3sum/csucom.py @@ -0,0 +1,3 @@ +''' + test code +''' \ No newline at end of file From 2bb46c531736a8d5a275520331450a4583aae9d2 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:10:51 +0900 Subject: [PATCH 30/37] contains-duplicate --- contains-duplicate/jejufather.cpp | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 contains-duplicate/jejufather.cpp diff --git a/contains-duplicate/jejufather.cpp b/contains-duplicate/jejufather.cpp new file mode 100644 index 000000000..2b2418ea6 --- /dev/null +++ b/contains-duplicate/jejufather.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +bool containsDuplicate(int* nums, int numsSize) { + char* pflag = (char*)malloc(1000000001); + char* mflag = (char*)malloc(1000000001); + memset(pflag, 0, 1000000001); + memset(mflag, 0, 1000000001); + for (int i = 0; i < numsSize; ++i) { + if (nums[i] < 0) { + if (mflag[-nums[i]] == 1) { + free(pflag); + free(mflag); + return true; + } + mflag[-nums[i]] = 1; + } + else { + if (pflag[nums[i]] == 1) { + free(pflag); + free(mflag); + return true; + } + pflag[nums[i]] = 1; + } + } + free(pflag); + free(mflag); + return false; +} + +void main(void) { + int test[100001] = {0}; + printf("%d\n", containsDuplicate(test, 1)); +} \ No newline at end of file From 07dc83002a66010740c51c4853e11bfb6c357956 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:29:27 +0900 Subject: [PATCH 31/37] contains-duplicate --- contains-duplicate/jejufather.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/contains-duplicate/jejufather.cpp b/contains-duplicate/jejufather.cpp index 2b2418ea6..81221d523 100644 --- a/contains-duplicate/jejufather.cpp +++ b/contains-duplicate/jejufather.cpp @@ -30,6 +30,7 @@ bool containsDuplicate(int* nums, int numsSize) { return false; } + void main(void) { int test[100001] = {0}; printf("%d\n", containsDuplicate(test, 1)); From 1da888d052773e8bb11ed5420758c5e471d3d5e3 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:36:08 +0900 Subject: [PATCH 32/37] contains-duplicate --- contains-duplicate/jejufather.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contains-duplicate/jejufather.cpp b/contains-duplicate/jejufather.cpp index 81221d523..61544a620 100644 --- a/contains-duplicate/jejufather.cpp +++ b/contains-duplicate/jejufather.cpp @@ -30,8 +30,7 @@ bool containsDuplicate(int* nums, int numsSize) { return false; } - void main(void) { int test[100001] = {0}; printf("%d\n", containsDuplicate(test, 1)); -} \ No newline at end of file +} From 84c2e41016cdacf7d45c32603d217192e9703937 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:31:46 +0900 Subject: [PATCH 33/37] Delete 3sum/csucom.py --- 3sum/csucom.py | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 3sum/csucom.py diff --git a/3sum/csucom.py b/3sum/csucom.py deleted file mode 100644 index 9def7cf98..000000000 --- a/3sum/csucom.py +++ /dev/null @@ -1,3 +0,0 @@ -''' - test code -''' \ No newline at end of file From 2750e33526873143b8535c5fdeb53f19cc684adf Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 00:48:05 +0900 Subject: [PATCH 34/37] contains-duplicate --- contains-duplicate/csucom.cpp | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 contains-duplicate/csucom.cpp diff --git a/contains-duplicate/csucom.cpp b/contains-duplicate/csucom.cpp new file mode 100644 index 000000000..61544a620 --- /dev/null +++ b/contains-duplicate/csucom.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +bool containsDuplicate(int* nums, int numsSize) { + char* pflag = (char*)malloc(1000000001); + char* mflag = (char*)malloc(1000000001); + memset(pflag, 0, 1000000001); + memset(mflag, 0, 1000000001); + for (int i = 0; i < numsSize; ++i) { + if (nums[i] < 0) { + if (mflag[-nums[i]] == 1) { + free(pflag); + free(mflag); + return true; + } + mflag[-nums[i]] = 1; + } + else { + if (pflag[nums[i]] == 1) { + free(pflag); + free(mflag); + return true; + } + pflag[nums[i]] = 1; + } + } + free(pflag); + free(mflag); + return false; +} + +void main(void) { + int test[100001] = {0}; + printf("%d\n", containsDuplicate(test, 1)); +} From 9e53feefd265d5a424b50cb09c27a9d619c06356 Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 23:22:49 +0900 Subject: [PATCH 35/37] delete wrong file --- contains-duplicate/jejufather.cpp | 36 ------------------------------- 1 file changed, 36 deletions(-) delete mode 100644 contains-duplicate/jejufather.cpp diff --git a/contains-duplicate/jejufather.cpp b/contains-duplicate/jejufather.cpp deleted file mode 100644 index 61544a620..000000000 --- a/contains-duplicate/jejufather.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include - -bool containsDuplicate(int* nums, int numsSize) { - char* pflag = (char*)malloc(1000000001); - char* mflag = (char*)malloc(1000000001); - memset(pflag, 0, 1000000001); - memset(mflag, 0, 1000000001); - for (int i = 0; i < numsSize; ++i) { - if (nums[i] < 0) { - if (mflag[-nums[i]] == 1) { - free(pflag); - free(mflag); - return true; - } - mflag[-nums[i]] = 1; - } - else { - if (pflag[nums[i]] == 1) { - free(pflag); - free(mflag); - return true; - } - pflag[nums[i]] = 1; - } - } - free(pflag); - free(mflag); - return false; -} - -void main(void) { - int test[100001] = {0}; - printf("%d\n", containsDuplicate(test, 1)); -} From b213d93ce31a9c7e07b897d15008330c17a5646c Mon Sep 17 00:00:00 2001 From: csucom Date: Mon, 9 Dec 2024 09:55:01 +0900 Subject: [PATCH 36/37] contains-duplicate solution --- contains-duplicate/csucom.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contains-duplicate/csucom.cpp b/contains-duplicate/csucom.cpp index 61544a620..1fcb4171e 100644 --- a/contains-duplicate/csucom.cpp +++ b/contains-duplicate/csucom.cpp @@ -1,4 +1,4 @@ -#include +//#include #include #include @@ -30,7 +30,7 @@ bool containsDuplicate(int* nums, int numsSize) { return false; } -void main(void) { - int test[100001] = {0}; - printf("%d\n", containsDuplicate(test, 1)); -} +// void main(void) { +// int test[100001] = {0}; +// printf("%d\n", containsDuplicate(test, 1)); +// } From 49876ae2bb059a7241fc95430c5428c30bbeeb3f Mon Sep 17 00:00:00 2001 From: Sungwook Choi Date: Mon, 9 Dec 2024 23:43:55 +0900 Subject: [PATCH 37/37] contains-duplicate solution --- contains-duplicate/csucom.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/contains-duplicate/csucom.cpp b/contains-duplicate/csucom.cpp index 1fcb4171e..18057c2f1 100644 --- a/contains-duplicate/csucom.cpp +++ b/contains-duplicate/csucom.cpp @@ -1,4 +1,3 @@ -//#include #include #include @@ -30,7 +29,4 @@ bool containsDuplicate(int* nums, int numsSize) { return false; } -// void main(void) { -// int test[100001] = {0}; -// printf("%d\n", containsDuplicate(test, 1)); -// } +