From 5ed99e8319e20593b32bb23467df10b300b01355 Mon Sep 17 00:00:00 2001 From: abc <> Date: Tue, 22 Jul 2025 22:00:20 +0900 Subject: [PATCH 01/12] two sum solution --- two-sum/SeongA.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 two-sum/SeongA.swift diff --git a/two-sum/SeongA.swift b/two-sum/SeongA.swift new file mode 100644 index 000000000..423ece945 --- /dev/null +++ b/two-sum/SeongA.swift @@ -0,0 +1,13 @@ +class Solution { + func twoSum(_ nums: [Int], _ target: Int) -> [Int] { + var dictionary: [Int: Int] = [:] + for (index, value) in nums.enumerated() { + let difference = target - value + if let otherIndex = dictionary[difference] { + return [otherIndex, index] + } + dictionary[value] = index + } + return [] + } +} From 751a88128a38363493a98fb05b759da8a1454688 Mon Sep 17 00:00:00 2001 From: delight010 Date: Tue, 22 Jul 2025 22:29:52 +0900 Subject: [PATCH 02/12] Add line Two Sum solution --- two-sum/SeongA.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/two-sum/SeongA.swift b/two-sum/SeongA.swift index 423ece945..cfcc1adab 100644 --- a/two-sum/SeongA.swift +++ b/two-sum/SeongA.swift @@ -11,3 +11,6 @@ class Solution { return [] } } + + + From 98bc154d26110c16bbe3474323a269548c0963ad Mon Sep 17 00:00:00 2001 From: SeongA <81559026+delight010@users.noreply.github.com> Date: Tue, 22 Jul 2025 22:31:20 +0900 Subject: [PATCH 03/12] Update SeongA.swift --- two-sum/SeongA.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/two-sum/SeongA.swift b/two-sum/SeongA.swift index cfcc1adab..16a2ece77 100644 --- a/two-sum/SeongA.swift +++ b/two-sum/SeongA.swift @@ -12,5 +12,3 @@ class Solution { } } - - From c520c8415a4770a169d2429fe530e2fab44a30c6 Mon Sep 17 00:00:00 2001 From: SeongA <81559026+delight010@users.noreply.github.com> Date: Tue, 22 Jul 2025 22:32:13 +0900 Subject: [PATCH 04/12] Update SeongA.swift From c317868ab71441400207acb244e83ed69e87b55e Mon Sep 17 00:00:00 2001 From: SeongA <81559026+delight010@users.noreply.github.com> Date: Tue, 22 Jul 2025 22:35:26 +0900 Subject: [PATCH 05/12] Rename SeongA.swift to delight010.swift --- two-sum/{SeongA.swift => delight010.swift} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename two-sum/{SeongA.swift => delight010.swift} (100%) diff --git a/two-sum/SeongA.swift b/two-sum/delight010.swift similarity index 100% rename from two-sum/SeongA.swift rename to two-sum/delight010.swift From 33aca2b36094896ab2d41a58722535b5afd65f1c Mon Sep 17 00:00:00 2001 From: delight010 Date: Thu, 24 Jul 2025 21:34:39 +0900 Subject: [PATCH 06/12] contains duplicate --- contains-duplicate/delight010.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 contains-duplicate/delight010.swift diff --git a/contains-duplicate/delight010.swift b/contains-duplicate/delight010.swift new file mode 100644 index 000000000..683d7d20f --- /dev/null +++ b/contains-duplicate/delight010.swift @@ -0,0 +1,13 @@ +class Solution { + func containsDuplicate(_ nums: [Int]) -> Bool { + var dictionary: [Int: Int] = [:] + for (index, num) in nums.enumerated() { + if let value = dictionary[num], value != index { + return true + } + dictionary[num] = index + } + return false + } +} + From b2b77925f51da6c685732256871ede3955b22879 Mon Sep 17 00:00:00 2001 From: delight010 Date: Thu, 24 Jul 2025 23:14:50 +0900 Subject: [PATCH 07/12] top k frequent elements --- top-k-frequent-elements/delight010.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 top-k-frequent-elements/delight010.swift diff --git a/top-k-frequent-elements/delight010.swift b/top-k-frequent-elements/delight010.swift new file mode 100644 index 000000000..68ae04723 --- /dev/null +++ b/top-k-frequent-elements/delight010.swift @@ -0,0 +1,11 @@ +class Solution { + func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { + var dictionary: [Int: Int] = [:] + for num in nums { + dictionary[num, default: 0] += 1 + } + + return dictionary.sorted(by: { $0.value > $1.value }).prefix(k).map(\.key) + } +} + From ffaa6d8f6bc6807a7589ce4fe3644d2fa1c0da46 Mon Sep 17 00:00:00 2001 From: delight010 Date: Thu, 24 Jul 2025 23:22:14 +0900 Subject: [PATCH 08/12] Add comments to top-k-frequent-element --- top-k-frequent-elements/delight010.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/top-k-frequent-elements/delight010.swift b/top-k-frequent-elements/delight010.swift index 68ae04723..bd4e2248a 100644 --- a/top-k-frequent-elements/delight010.swift +++ b/top-k-frequent-elements/delight010.swift @@ -1,11 +1,19 @@ class Solution { func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { var dictionary: [Int: Int] = [:] - for num in nums { + for num in nums { // for loop를 돌면서 O(n)의 시간복잡도 dictionary[num, default: 0] += 1 + // Swift에서 dictionary 검색의 시간복잡도는 O(1) } - return dictionary.sorted(by: { $0.value > $1.value }).prefix(k).map(\.key) + return dictionary + .sorted(by: { $0.value > $1.value }) + // dictionary sorted()의 시간복잡도 O(n log n) + .prefix(k) + // k의 개수만큼 탐색합니다. 고로 시간복잡도는 O(n) + .map(\.key) + // prefix에서 k만큼 탐색하였습니다. + // .map의 시간복잡도는 O(n)입니다. } } From 0d7e8da1b65a515b175889c87701869a188d35b4 Mon Sep 17 00:00:00 2001 From: delight010 Date: Thu, 24 Jul 2025 23:25:09 +0900 Subject: [PATCH 09/12] Add comment to two-sum --- two-sum/delight010.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/two-sum/delight010.swift b/two-sum/delight010.swift index 16a2ece77..b08201145 100644 --- a/two-sum/delight010.swift +++ b/two-sum/delight010.swift @@ -2,6 +2,7 @@ class Solution { func twoSum(_ nums: [Int], _ target: Int) -> [Int] { var dictionary: [Int: Int] = [:] for (index, value) in nums.enumerated() { + // nums배열의 개수만큼 반복합니다. O(n) let difference = target - value if let otherIndex = dictionary[difference] { return [otherIndex, index] From ad607f497ad355deeb69240fbb2aa0328edc90d1 Mon Sep 17 00:00:00 2001 From: delight010 Date: Thu, 24 Jul 2025 23:27:47 +0900 Subject: [PATCH 10/12] Add comments to contains-duplicate --- contains-duplicate/delight010.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contains-duplicate/delight010.swift b/contains-duplicate/delight010.swift index 683d7d20f..9b3889bbe 100644 --- a/contains-duplicate/delight010.swift +++ b/contains-duplicate/delight010.swift @@ -2,8 +2,11 @@ class Solution { func containsDuplicate(_ nums: [Int]) -> Bool { var dictionary: [Int: Int] = [:] for (index, num) in nums.enumerated() { + // 배열 nums의 개수만큼 반복합니다. 시간복잡도 O(n) if let value = dictionary[num], value != index { return true + // 만일 동일한 숫자를 일찍 찾게 된다면 + // 시간복잡도는 O(n)보다 더 빨라질 수 있습니다. } dictionary[num] = index } From d7bf9b62a680308d9f885a73538c04773e57977f Mon Sep 17 00:00:00 2001 From: delight010 Date: Fri, 25 Jul 2025 14:17:28 +0900 Subject: [PATCH 11/12] solve longest consecutive sequence --- longest-consecutive-sequence/delight010.swift | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 longest-consecutive-sequence/delight010.swift diff --git a/longest-consecutive-sequence/delight010.swift b/longest-consecutive-sequence/delight010.swift new file mode 100644 index 000000000..dbed046f8 --- /dev/null +++ b/longest-consecutive-sequence/delight010.swift @@ -0,0 +1,26 @@ +class Solution { + func longestConsecutive(_ nums: [Int]) -> Int { + if nums.isEmpty { return 0 } + // .isEmpty 메서드를 사용하여 배열의 개수가 0일 시 바로 0을 리턴합니다. + // 시간복잡도 O(1) + var maxCount = 0 + var count = 1 + var prefixNumber = nums.sorted().first ?? 0 + for num in nums.sorted(by: <) { + // nums 배열을 오름차순 정리 후 반복문을 루프합니다. + // .sorted 시간복잡도 O(n log n) + // for문 시간복잡도 O(n) + if prefixNumber == num { + continue + } else if prefixNumber + 1 == num { + count += 1 + prefixNumber = num + } else { + maxCount = max(maxCount, count) + count = 1 + prefixNumber = num + } + } + return max(maxCount, count) + } +} From 37b336c6f64e14330fcbbdc48c6da7dd6de3b1c1 Mon Sep 17 00:00:00 2001 From: delight010 Date: Fri, 25 Jul 2025 19:38:24 +0900 Subject: [PATCH 12/12] solve house robber --- house-robber/delight010.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 house-robber/delight010.swift diff --git a/house-robber/delight010.swift b/house-robber/delight010.swift new file mode 100644 index 000000000..6452868c7 --- /dev/null +++ b/house-robber/delight010.swift @@ -0,0 +1,15 @@ +class Solution { + func rob(_ nums: [Int]) -> Int { + var sum1 = 0 + var sum2 = 0 + for num in nums { + // for문을 통해 루프. + // 시간복잡도는 O(n) + let temp = max(num + sum1, sum2) + sum1 = sum2 + sum2 = temp + } + return sum2 + } +} +