From 81e9c8cd51ad7255bd41495f5c37446e04768d9a Mon Sep 17 00:00:00 2001 From: JungHm Date: Sun, 30 Mar 2025 20:09:40 +0900 Subject: [PATCH 1/7] solve: contains-duplicate --- contains-duplicate/JustHm.swift | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 contains-duplicate/JustHm.swift diff --git a/contains-duplicate/JustHm.swift b/contains-duplicate/JustHm.swift new file mode 100644 index 000000000..10a98dce1 --- /dev/null +++ b/contains-duplicate/JustHm.swift @@ -0,0 +1,5 @@ +class Solution { + func containsDuplicate(_ nums: [Int]) -> Bool { + nums.count != Set(nums).count + } +} From 3bf40445448d069eeb9af133c2a0157b60659c6d Mon Sep 17 00:00:00 2001 From: JungHm Date: Sun, 30 Mar 2025 20:40:45 +0900 Subject: [PATCH 2/7] solve: two sum --- two-sum/JustHm.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 two-sum/JustHm.swift diff --git a/two-sum/JustHm.swift b/two-sum/JustHm.swift new file mode 100644 index 000000000..fbb73ae8e --- /dev/null +++ b/two-sum/JustHm.swift @@ -0,0 +1,12 @@ +class Solution { + func twoSum(_ nums: [Int], _ target: Int) -> [Int] { + var dict = [Int: Int]() + for index in 0.. Date: Sun, 30 Mar 2025 21:17:59 +0900 Subject: [PATCH 3/7] solve: top k frequent elements --- top-k-frequent-elements/JustHm.swift | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 top-k-frequent-elements/JustHm.swift diff --git a/top-k-frequent-elements/JustHm.swift b/top-k-frequent-elements/JustHm.swift new file mode 100644 index 000000000..2b84334ee --- /dev/null +++ b/top-k-frequent-elements/JustHm.swift @@ -0,0 +1,9 @@ +class Solution { + func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { + var dict = [Int: Int]() + for num in nums { + dict[num, default: 0] += 1 + } + return [Int](dict.sorted{$0.value > $1.value}.map{$0.key}[.. Date: Tue, 1 Apr 2025 10:05:08 +0900 Subject: [PATCH 4/7] solve: longest consecutive sequence --- longest-consecutive-sequence/JustHm.swift | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 longest-consecutive-sequence/JustHm.swift diff --git a/longest-consecutive-sequence/JustHm.swift b/longest-consecutive-sequence/JustHm.swift new file mode 100644 index 000000000..b0243329f --- /dev/null +++ b/longest-consecutive-sequence/JustHm.swift @@ -0,0 +1,24 @@ +class Solution { + func longestConsecutive(_ nums: [Int]) -> Int { + guard !nums.isEmpty else { return 0 } + /* + 중복 제거 후 소팅 후 배열로 만드는것은 N + NlogN + N 으로 예상된다.. + 그래서 정렬만 하고 로직에서 중복 숫자는 그냥 무시하게 만드는 방식으로 속도를 개선 + 문제에서 O(N)으로 작성하자 라는 제약아닌 제약이 있었는데, O(N)은 도저히 떠오르지 않는다. + */ + let nums = nums.sorted()//Array(Set(nums).sorted()) + var answer = 1 + var count = 1 + for index in 0.. Date: Wed, 2 Apr 2025 01:45:40 +0900 Subject: [PATCH 5/7] solve: house robber --- house-robber/JustHm.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 house-robber/JustHm.swift diff --git a/house-robber/JustHm.swift b/house-robber/JustHm.swift new file mode 100644 index 000000000..d7230955e --- /dev/null +++ b/house-robber/JustHm.swift @@ -0,0 +1,20 @@ +class Solution { + func rob(_ nums: [Int]) -> Int { + guard nums.count != 1 else { return nums[0] } + guard nums.count != 2 else { return max(nums[0], nums[1]) } + + // var dp = [nums[0], max(nums[0], nums[1])] + var twoStepPrev = nums[0] + var oneStepPrev = max(nums[0], nums[1]) + for i in 2.. Date: Thu, 3 Apr 2025 10:04:04 +0900 Subject: [PATCH 6/7] =?UTF-8?q?solve:=20top=20k=20frequent=20elements=20-?= =?UTF-8?q?=20=EC=8B=9C=EA=B0=84=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- top-k-frequent-elements/JustHm.swift | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/top-k-frequent-elements/JustHm.swift b/top-k-frequent-elements/JustHm.swift index 2b84334ee..e80dc9370 100644 --- a/top-k-frequent-elements/JustHm.swift +++ b/top-k-frequent-elements/JustHm.swift @@ -1,3 +1,38 @@ +// MARK: 리뷰를 받고 시도해본 다른 풀이 (time: O(n), space O(n) +class Solution { + func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { + // 빈도수 별로 Dictionary에 저장 + var dict = [Int: Int]() + for num in nums { + dict[num, default: 0] += 1 + } + // Bucket sort 사용, 크기는 nums 만큼, 빈도수가 nums 공간보다 클수는 없으니까 + var bucket = Array(repeating: [Int](), count: nums.count + 1) + for (key, value) in dict { + // 빈도수를 인덱스로 key == num 을 저장함 + // 배열로 한 이유는 빈도수가 같은게 있을 수 있으니까! + bucket[value].append(key) + } + + // 결과 출력 + var answer = [Int]() + // bucket의 뒤에서부터 탐색 + for index in stride(from: nums.count, to: 0, by: -1) { + // 없으면 무시 + guard !bucket[index].isEmpty else { continue } + // 버켓의 현재 인덱스에 nil이 아니면 하나씩 결과에 추가해줌. + for item in bucket[index] { + answer.append(item) + // k개 만큼 가져왔다면 바로 리턴 + if answer.count == k { return answer } + } + } + + return answer + } +} +// MARK: time: O(nlogn), space: O(n) +// n+nlogn+n class Solution { func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { var dict = [Int: Int]() From 9290955ebd373a6da3ea1bcdad5cb119adf5631f Mon Sep 17 00:00:00 2001 From: JungHm Date: Thu, 3 Apr 2025 11:06:54 +0900 Subject: [PATCH 7/7] =?UTF-8?q?solve:=20longest=20consecutive=20sequence?= =?UTF-8?q?=20=EC=8B=9C=EA=B0=84=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-consecutive-sequence/JustHm.swift | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/longest-consecutive-sequence/JustHm.swift b/longest-consecutive-sequence/JustHm.swift index b0243329f..403b07b52 100644 --- a/longest-consecutive-sequence/JustHm.swift +++ b/longest-consecutive-sequence/JustHm.swift @@ -22,3 +22,31 @@ class Solution { return max(answer, count) } } +// MARK: time: O(n) 풀이 +/* + 몰랐던점 Set의 contains는 O(n) 아님, O(1)임 (내부적으로 Hash table로 구현되어있음) + 하지만 풀이 제출해보니까 속도가 sort 한거보다 덜나옴.. 왜? + Set은 contains를 확인할때, 들어온 값을 hash로 변경후 검색, 버킷찾기, 충돌 검사 등을 거치기 때문에... O(1)은 맞는데 시간이 조금 걸린다. + 그렇기에 set방식으로 O(n)에 짠거보다 sorted 내장함수를 이용한 O(nlogn)의 실제 실행시간이 더 빠른것. +*/ +class AnotherSolution { + func longestConsecutive(_ nums: [Int]) -> Int { + var numSet = Set(nums) + var answer = 0 + + for num in numSet { + // num-1이 없으면 시작지점이니까 여기부터 +1 하면서 길이 확인 + // num-1이 있으면 그냥 무시 + if !numSet.contains(num-1) { //Set의 contains는 O(1)임!! (내부적으로 Hash table로 구현되어있기 때문) + var count = 1 + var temp = num + 1 + while numSet.contains(temp) { + count += 1 + temp += 1 + } + answer = max(answer, count) + } + } + return answer + } +}