2022. 10. 5. 16:15ใํ๋ก๊ทธ๋๋จธ์ค-Swift
ํ๋ก๊ทธ๋๋จธ์ค - ์ ๊ณ ๊ฒฐ๊ณผ๋ฐ๊ธฐ(Swift)
๋ฌธ์ ์ค๋ช
https://school.programmers.co.kr/learn/courses/30/lessons/92334
๋์ ํ์ด
๋ ์ฝ์ผ๋ ์ฑ(nil์ผ๋ ?? ์ฐ์ฐ์๋ฅผ ํตํด ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ๋ ๊ฒ)์ ์ต์ํ์ง ์์์ if let์ ๋๋ฐฐํ ํ์ ์ด ๋ณด์ธ๋ค..
import Foundation
func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
var dictionaryReport: [String: [String]] = [:]
var checkReported: [String: Int] = [:]
for comp in report {
let people = comp.components(separatedBy: " ")
let reporter = people[0]
let reportedPerson = people[1]
if let res = dictionaryReport[reporter]?.contains(reportedPerson)
{
if res != false {
continue
}
}
if let res = dictionaryReport[reporter] {
dictionaryReport[reporter]!.append(reportedPerson)
} else {
dictionaryReport[reporter] = [reportedPerson]
}
if let res = checkReported[reportedPerson] {
checkReported[reportedPerson]! += 1
} else {
checkReported[reportedPerson] = 1
}
}
return id_list.map {
if let arr = dictionaryReport[$0] {
let cnt = arr.filter{ checkReported[$0]! >= k }.count
return cnt
}
return 0
}
}
if-let ๋ถ๋ถ๋ง ๋ ์ฝ์ผ๋ ์ฑ์ผ๋ก ๋ฐ๊ฟ๋ ์ฝ๋๊ฐ ํจ์ฌ ๊ฐ๊ฒฐํด์ง๋ค.
import Foundation
func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
var dictionaryReport: [String: [String]] = [:]
var checkReported: [String: Int] = [:]
for comp in report {
let people = comp.components(separatedBy: " ")
let reporter = people[0]
let reportedPerson = people[1]
if let res = dictionaryReport[reporter]?.contains(reportedPerson)
{
if res != false {
continue
}
}
// ๋ณ๊ฒฝ๋ ์ฝ๋
dictionaryReport[reporter] = (dictionaryReport[reporter] ?? []) + [reportedPerson]
checkReported[reportedPerson] = (checkReported[reportedPerson] ?? 0) + 1
}
return id_list.map {
if let arr = dictionaryReport[$0] {
let cnt = arr.filter{ checkReported[$0]! >= k }.count
return cnt
}
return 0
}
}
๋ค๋ฅธ ์ฌ๋์ ํ์ด
๋ฐฐ์ธ๊ฒ ๋ง์ ์ฝ๋์ด๋ค. ๊ฑฐ์ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ๋ ์ฝ์ด๋ ์ฑ์ ์ฌ์ฉํ๊ณ reduce๋ฅผ ์ด์ฉํด์ ์ ๊ณ ํ ์ฌ๋์ด ์ด๋ค ์ฌ๋์ ์ ๊ณ ํ๋์ง, ๊ทธ ์ฌ๋์ ๋ช๋ฒ ์ง๋ชฉ๋์๋์ง๋ฅผ ๊น๋ํ๊ฒ ์ฝ๋๋ก ์์ฑํ์ จ๋ค.
import Foundation
func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
var reported: [String: Int] = [:]
var user: [String: [String]] = [:]
for r in Set(report) {
let results = r.components(separatedBy: " ")
user[results[0]] = (user[results[0]] ?? []) + [results[1]]
reported[results[1]] = (reported[results[1]] ?? 0) + 1
}
return id_list.map { id in
return (user[id] ?? []).reduce(0) {
$0 + ((reported[$1] ?? 0) >= k ? 1 : 0)
}
}
}
ํผ๋๋ฐฑ
1. if-let ๋ฐ์ธ๋ฉ์ ๊ฐ์ด nil์ด ์๋ ๊ฒฝ์ฐ์ ์ฝ๋ ๋ธ๋ก์ ๋ค์ด์ค๊ฒ ๋๋ค. ์ฆ false๋ผ๋ ๊ฐ๋ ์ฝ๋๋ธ๋ก์ ๋ค์ด์ค๊ธฐ ๋๋ฌธ์ if์ ํผ๋ํ์ง ๋ง์.
2. if-let ๋ฐ์ธ๋ฉ์ ์ฌ์ฉํ๊ธฐ ์ ์ ๋ ์ฝ์ผ๋ ์ฑ์ผ๋ก ํด๊ฒฐ์ด ๊ฐ๋ฅํ์ง ๊ณ ๋ฏผํด๋ณธ๋ค.
3. reduce๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐฐ์ด์ ์ซ์๋ก ๋ง๋ค์ด์ฃผ๋ ํจ์์ด๋ค. ๋์ ๋๋ฆฌ์ ๊ฐ์ด ์์ ๋๋ ๋น ๋ฐฐ์ด์ ์ด๊ธฐ๊ฐ์ผ๋ก ๋ฌ์ ์งํํ๋ฉด ๋๋ค.
'ํ๋ก๊ทธ๋๋จธ์ค-Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค - ์ฌ๋ฐ๋ฅธ ๊ดํธ(Swift) (0) | 2022.10.08 |
---|---|
JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ - ํ๋ก๊ทธ๋๋จธ์ค(Swift) (0) | 2022.10.06 |
์ฑ๊ฒฉ์ ํ ๊ฒ์ฌํ๊ธฐ - ํ๋ก๊ทธ๋๋จธ์ค(Swift) (1) | 2022.10.05 |
ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ - ํ๋ก๊ทธ๋๋จธ์ค(Swift) (0) | 2022.10.04 |
ํคํจ๋ ๋๋ฅด๊ธฐ - ํ๋ก๊ทธ๋๋จธ์ค(Swift) (1) | 2022.10.03 |