Foma's ํ์ด
๋ชจ๋ ์ง์์๋ค์ ์ธ์ด, ์ง๊ตฐ, ๊ฒฝ๋ ฅ, ์์ธํธ๋, ์ ์์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
์ด ์ ๋ณด๋ฅผ ๊ฐ๊ธฐ ์ํด์ ๋์ด์ฐ๊ธฐ ๊ธฐ์ค์ผ๋ก ๋๋์ด์ ๋ฐฐ์ด์ ๋ฃ์ด์ค๋๋ค.
์ด๋ ๊ฒ ํ๋ฉด 0๋ฒ์งธ ์ธ์ด 1๋ฒ์งธ ์ง๊ตฐ 2๋ฒ์งธ ๊ฒฝ๋ ฅ 2๋ฒ์งธ ์์ธํธ๋ 4๋ฒ์งธ ์ ์๊ฐ ๋ ๊ฒ ์ ๋๋ค.
์ด๋ ๊ฒ ๋๋์ด์ง ์ง์์๋ค์ ์ ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํด์ค๋๋ค.
๊ทธ ๋ค์์ผ๋ก ์ ๊ฐ ํต์ฌ ์ด๋ผ๊ณ ์๊ฐํ๋ ์ฒซ๋ฒ์งธ์ ๋๋ค. (๋์ ํ ๋ชจ๋ฅด๊ฒ ์ด์ ์นด์นด์ค ๋ฌธ์ ํด์ค์ ์ฐธ๊ณ ํ์ต๋๋ค.....)
์ง์์๋ค์ 4๊ฐ์ง ์ ๋ณด๋ก ๋ชจ๋ ์ผ์ด์ค๋ค์ 16๊ฐ์ง๋ฅผ ๋ฏธ๋ฆฌ ๋ง๋ค์ด๋ฒ๋ฆฌ๋ ๊ฒ์ ๋๋ค.
์๋ฅผ ๋ค๋ฉด ์๋ฐ ๋ฐฑ์๋ ์๋์ด ํผ์๋ผ๋ฉด
์ ๋ณด์์
์๋ฐ
์๋ฐ ๋ฐฑ์๋
์๋ฐ ์๋์ด
์๋ฐ ํผ์
์๋ฐ ๋ฐฑ์๋ ์๋์ด
์๋ฐ ๋ฐฑ์๋ ํผ์
์๋ฐ ์๋์ด ํผ์
์๋ฐ ๋ฐฑ์๋ ์๋์ด ํผ์
๋ฐฑ์๋
๋ฐฑ์๋ ์๋์ด
๋ฐฑ์๋ ํผ์
๋ฐฑ์๋ ์๋์ด ํผ์
์๋์ด
์๋์ด ํผ์
ํผ์
๊ฐ ๋๊ฒ ์ฃ .
์ด๋ฐ์์ผ๋ก ๊ฐ ์ง์์๋ค์ 16๊ฐ์ง ๊ทธ๋ฃน์ผ๋ก ๋ง๋ค์ด์ ์ ์๋ค๋ง ๋ฐ๋ก ์ ์ฅํด์ค๋๋ค.
๊ทธ ๋ค์ ์ฟผ๋ฆฌ๊ฐ ์ฃผ์ด์ก์๋ ๋์ผํ ๊ทธ๋ฃน์ ์ ์๋ค๋ง ๋น๊ตํด์ ์นด์ดํ ํด์ค๋๋ค.
์ฌ๊ธฐ์ ์ ๊ฐ ํต์ฌ ์ด๋ผ๊ณ ์๊ฐํ๋ 2๋ฒ์งธ๋ ์ด์งํ์ ์ ๋๋ค.
๋์ผํ ๊ทธ๋ฃน์ ์ ์๋ค์ด ์์ฃผ ๋ง์ ๋ ํจ์จ์ ์ผ๋ก ์ด์งํ์์ผ๋ก ๊ฐ๋ค์ ๋น๊ตํด์ ์นด์ดํ ํด์ค์ผํฉ๋๋ค. (์๊ทธ๋ผ ํจ์จ์ฑ ๋คํ๋ฆผ..)
๋ง์ง๋ง์ผ๋ก ์ฟผ๋ฆฌ์ ๋ชจ๋ ์กฐ๊ฑด์ ๋ง๋ ์ง์์๋ค์ ์๋ฅผ ๋ฐฐ์ด์ ๋ด์ ๋ฐํํ๋ฉด ๋ฉ๋๋ค.
Source Code
ํ๋ ธ๋ ์ผ์ด์ค๋ค
1์ฐจ ์๋
์ ํ์ฑ์ ๋ชจ๋ ๋ง์์ง๋ง ํจ์จ์ฑ์์ ๋ค ํ๋ฆฐ ์ฝ๋
๋๋ฆ ์ด ์ฝ๋๋ฅผ ์ธ ๋๋ 4๊ฐ์ง ์ ๋ณด๋ก ์๋ผ์ ํจ์จ์ ์ผ๋ก ๋น๊ตํ๋ค๊ณ ์๊ฐํ๋๋ฐ.....
import Foundation
func solution(_ info:[String], _ query:[String]) -> [Int] {
var counts = [Int]()
var applicants = [[String]]()
info.forEach{applicants.append($0.components(separatedBy: " "))}
let sortedApplicants = applicants.sorted{Int($0[4])! > Int($1[4])!}
for q in query {
var count = 0
let split = q.replacingOccurrences(of: "and", with: "").replacingOccurrences(of: "-", with: "").components(separatedBy: " ").filter{$0 != ""}
var filterApplicant = sortedApplicants
for (i,s) in split.enumerated() {
if i == split.count - 1 {
for applicant in filterApplicant {
if Int(applicant[4])! >= Int(String(split.last!))! {
count += 1
}else {
break
}
}
counts.append(count)
}else {
if s == "backend" || s == "frontend" {
filterApplicant = filterApplicant.filter{$0[1] == s}
}else if s == "junior" || s == "senior" {
filterApplicant = filterApplicant.filter{$0[2] == s}
}else if s == "pizza" || s == "chicken" {
filterApplicant = filterApplicant.filter{$0[3] == s}
}else {
filterApplicant = filterApplicant.filter{$0[0] == s}
}
}
}
}
return counts
}
2์ฐจ ์๋
๋ชจ๋ ์ผ์ด์ค๋ค์ ๋ชจ์์ ๋๋ฆฌ๊ธด ํ์ง๋ง ์ด์ง ํ์์ ์ฐ์ง ์์ ๋ชจ๋ ํจ์จ์ฑ์์ ๋ค ํ๋ฆผ.
func solution(_ info:[String], _ query:[String]) -> [Int] {
var counts = [Int]()
var applicants = [[String]]()
info.forEach{applicants.append($0.components(separatedBy: " "))}
applicants.sort{Int($0[4])! > Int($1[4])!}
var language = String()
var backOrFront = String()
var career = String()
var food = String()
var caseDic = [String:[Int]]()
for applicant in applicants {
language = applicant[0]
backOrFront = applicant[1]
career = applicant[2]
food = applicant[3]
let allCases = ["- and - and - and -",
"- and - and - and \(food)",
"- and - and \(career) and -",
"- and \(backOrFront) and - and -",
"\(language) and - and - and -",
"\(language) and - and - and \(food)",
"\(language) and \(backOrFront) and - and -",
"\(language) and - and \(career) and -",
"- and \(backOrFront) and \(career) and -",
"- and \(backOrFront) and - and \(food)",
"- and - and \(career) and \(food)",
"\(language) and \(backOrFront) and \(career) and -",
"\(language) and \(backOrFront) and - and \(food)",
"\(language) and - and \(career) and \(food)",
"- and \(backOrFront) and \(career) and \(food)",
"\(language) and \(backOrFront) and \(career) and \(food)"]
for i in 0..<allCases.count {
if caseDic[allCases[i]] != nil {
caseDic[allCases[i]]!.append(Int(applicant[4])!)
}else{
caseDic[allCases[i]] = [Int(applicant[4])!]
}
}
}
for q in query {
var count = 0
var split = q.components(separatedBy: " ")
let score = Int(split.removeLast())!
let join = String(split.joined(separator: " "))
if caseDic[join] == nil {
counts.append(0)
}else {
for number in caseDic[join]! {
if number >= score {
count += 1
}else{
break
}
}
counts.append(count)
}
}
return counts
}
Reference
๋๊ธ