diff --git a/3sum/WhiteHyun.swift b/3sum/WhiteHyun.swift new file mode 100644 index 000000000..c3492952a --- /dev/null +++ b/3sum/WhiteHyun.swift @@ -0,0 +1,41 @@ +// +// 15. 3Sum +// https://leetcode.com/problems/3sum/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/06/04. +// + +class Solution { + func threeSum(_ nums: [Int]) -> [[Int]] { + var result: [[Int]] = [] + let sorted = nums.sorted() + + for (index, element) in sorted.enumerated() where index <= 0 || element != sorted[index - 1] { + var left = index + 1 + var right = sorted.count - 1 + + while left < right { + let threeSum = element + sorted[left] + sorted[right] + if threeSum > 0 { + right -= 1 + continue + } + if threeSum < 0 { + left += 1 + continue + } + + result.append([element, sorted[left], sorted[right]]) + left += 1 + + while sorted[left] == sorted[left - 1] && left < right { + left += 1 + } + } + } + + + return result + } +} diff --git a/longest-consecutive-sequence/WhiteHyun.swift b/longest-consecutive-sequence/WhiteHyun.swift new file mode 100644 index 000000000..fb0780ba9 --- /dev/null +++ b/longest-consecutive-sequence/WhiteHyun.swift @@ -0,0 +1,25 @@ +// +// 128. Longest Consecutive Sequence +// https://leetcode.com/problems/longest-consecutive-sequence/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/06/01. +// + +final class Solution { + func longestConsecutive(_ nums: [Int]) -> Int { + let set = Set(nums) + var best = 0 + for number in set where !set.contains(number - 1) { + var next = number + 1 + while set.contains(next) { + next += 1 + } + if best < next - number { + best = next - number + } + } + + return best + } +} diff --git a/product-of-array-except-self/WhiteHyun.swift b/product-of-array-except-self/WhiteHyun.swift new file mode 100644 index 000000000..93ebcf561 --- /dev/null +++ b/product-of-array-except-self/WhiteHyun.swift @@ -0,0 +1,27 @@ +// +// 238. Product of Array Except Self +// https://leetcode.com/problems/product-of-array-except-self/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/06/01. +// + +final class Solution { + func productExceptSelf(_ nums: [Int]) -> [Int] { + var answer: [Int] = .init(repeating: 1, count: nums.count) + + var left_product = 1 + for i in nums.indices { + answer[i] = left_product + left_product *= nums[i] + } + + var right_product = 1 + for i in nums.indices.reversed() { + answer[i] *= right_product + right_product *= nums[i] + } + + return answer + } +} diff --git a/top-k-frequent-elements/WhiteHyun.swift b/top-k-frequent-elements/WhiteHyun.swift new file mode 100644 index 000000000..41558ad6e --- /dev/null +++ b/top-k-frequent-elements/WhiteHyun.swift @@ -0,0 +1,24 @@ +// +// 347. Top K Frequent Elements +// https://leetcode.com/problems/top-k-frequent-elements/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/06/01. +// + +final class Solution { + func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { + // Python의 Counter 모듈처럼 만드는 것과 동일 + var counter: [Int: Int] = [:] + for number in nums { + counter[number, default: 0] += 1 + } + + return counter + .sorted { lhs, rhs in + lhs.value > rhs.value // 빈도 수를 기준으로 내림차순 정렬 + } + .prefix(k) // 앞에서부터 k만큼 Subarray로 가져옴 + .map(\.key) // 제일 빈도가 많았던 것의 숫자를 가져옴 (Dictionary의 Key값) + } +}