Skip to content

[Hyun] Week 5 Solution Explanation #108

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
Jun 4, 2024
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
41 changes: 41 additions & 0 deletions 3sum/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
25 changes: 25 additions & 0 deletions longest-consecutive-sequence/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
27 changes: 27 additions & 0 deletions product-of-array-except-self/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
24 changes: 24 additions & 0 deletions top-k-frequent-elements/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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๊ฐ’)
Comment on lines +17 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

์ŠคํŠธ๋ฆผ(์Šค์œ„ํ”„ํŠธ์—์„œ๋„ ํ•ด๋‹น ์šฉ์–ด๊ฐ€ ๋งž์„๊นŒ์š”?)๋กœ ํ•ด๊ฒฐํ•˜๋ฉด ๊น”๋”ํ•˜๊ฒ ๋‹ค ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์—ญ์‹œ๋‚˜ ๋ฉ‹์žˆ๋„ค์š”! ๐Ÿ‘

}
}