diff --git a/contains-duplicate/doitduri.swift b/contains-duplicate/doitduri.swift new file mode 100644 index 000000000..59691f729 --- /dev/null +++ b/contains-duplicate/doitduri.swift @@ -0,0 +1,6 @@ +class Solution { + func containsDuplicate(_ nums: [Int]) -> Bool { + let numbericSet = Set(nums) + return numbericSet.count < nums.count + } +} diff --git a/house-robber/doitduri.swift b/house-robber/doitduri.swift new file mode 100644 index 000000000..1e7d6feec --- /dev/null +++ b/house-robber/doitduri.swift @@ -0,0 +1,23 @@ +class Solution { +func rob(_ nums: [Int]) -> Int { + let length = nums.count + + if length == 1 { + return nums[0] + } + + if length == 2 { + return max(nums[0], nums[1]) + } + + var tables = Array(repeating: 0, count: length) + tables[0] = nums[0] + tables[1] = max(nums[0], nums[1]) + + for i in 2.. Int { + var result = 1 + var maxResult = 1 + let sortedNums = nums.sorted() + + guard var previous = sortedNums.first else { + return 0 + } + + for index in 1.. [Int] { + var tables: [Int: Int] = [:] + + for num in nums { + tables[num] = (tables[num] ?? 0) + 1 + } + + let values = tables.values.sorted().reversed().prefix(k) // k개의 frequent + let result = tables.compactMap { (key: Int, value: Int) -> Int? in + if values.contains(value) { + return key + } + return nil + } + return result + } +} diff --git a/two-sum/doitduri.swift b/two-sum/doitduri.swift new file mode 100644 index 000000000..7ebe1cb5a --- /dev/null +++ b/two-sum/doitduri.swift @@ -0,0 +1,16 @@ +class Solution { + func twoSum(_ nums: [Int], _ target: Int) -> [Int] { + let dict = nums.enumerated().reduce(into: [Int: Int]()) { initialValue, num in + initialValue[num.element] = num.offset + } + + for startIndex in 0..