Skip to content

[kut7728] WEEK01 Solutions #1148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions contains-duplicate/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Time, Space Complexity O(N)

class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
var seen = Set<Int>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 unordered_map 으로 풀었는데 set 으로 푸는게 더 간결해 보이네요

for num in nums {
if seen.contains(num) {
return true
}
seen.insert(num)
}
return false
}
}
21 changes: 21 additions & 0 deletions house-robber/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
func rob(_ nums: [Int]) -> Int {
let n = nums.count
if n == 0 { return 0 }
if n == 1 { return nums[0] }
if n == 2 { return max(nums[0], nums[1]) }

//dp[i]는 i번까지 고려했을 때 가능한 최대 금액
var dp = [Int](repeating: 0, count: n)
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1]) //첫째 or 둘째 집 중 더 비싼 집 털기

//각 dp의 자리에는 바로 전 값을 그대로 가져오거나(i를 안털기), 전전집까지 턴거+i턴 값 중에서 비싼쪽 저장
for i in 2..<n {
dp[i] = max(dp[i-1], dp[i-2] + nums[i])
}

//마지막 집까지 고려한 최대 이익 반환하기
return dp[n - 1]
}
}
23 changes: 23 additions & 0 deletions longest-consecutive-sequence/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
func longestConsecutive(_ nums: [Int]) -> Int {
let numSet = Set(nums)
var longest = 0

for num in numSet {
// 연속 수열의 시작점인지 확인
if !numSet.contains(num - 1) {
var currentNum = num
var currentStreak = 1

while numSet.contains(currentNum + 1) {
currentNum += 1
currentStreak += 1
}

longest = max(longest, currentStreak)
}
}

return longest
}
}
51 changes: 51 additions & 0 deletions top-k-frequent-elements/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Solution {
/*
1차 시도
복잡도 - O(n log n)
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
var list: [Int:Int] = [:]
var result: [Int] = []
if nums.count == 1 { return nums }

for i in nums {
list[i, default: 0] += 1
}

var sortedList = list.sorted { $0.value > $1.value }
print(sortedList)

for i in 0..<k {
let target = sortedList[i].key
result.append(target)
// sortedList[target] = 0
}
return result
} */

// 2차 시도 : 복잡도 - O(n)
class Solution {
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
var freqMap = [Int: Int]()

for num in nums {
freqMap[num, default: 0] += 1
}

// 버켓에 빈도수를 인덱스로 값을 저장
var buckets = Array(repeating: [Int](), count: nums.count + 1)
for (num, freq) in freqMap {
buckets[freq].append(num)
}

var result = [Int]()
for i in (0 ..< buckets.count).reversed() {
result += buckets[i]
if result.count == k {
break
}
}

return result
}
}
}
29 changes: 29 additions & 0 deletions two-sum/kut7728.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


class Solution {
// 복잡도 O(n^2)
// func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
// for (i, num) in nums.enumerated() {
// guard let tempIndex = nums.firstIndex(of: target - num) else {continue}
// if tempIndex == i { continue }
// return [i, tempIndex]
// }
// return [0]
// }

// 시간, 공간 복잡도 O(n)
// 해쉬맵 사용하기!
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var list: [Int:Int] = [:]
for (i, num) in nums.enumerated() {
if let exist = list[target-num] {
return [exist, i]
} else {
list[num] = i
}
}
return [0]


}
}