-
Notifications
You must be signed in to change notification settings - Fork 3
/
[프로그래머스] 신재웅_순위 검색.swift
75 lines (61 loc) · 2.17 KB
/
[프로그래머스] 신재웅_순위 검색.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Foundation
func solution(_ info:[String], _ query:[String]) -> [Int] {
let infoArr = info.map{$0.split(separator: " ").map{String($0)}}
let queryArr = query.map{$0.split(separator: " ").map{String($0)}}
var personHashMap = PersonHashMap()
infoArr.forEach {
personHashMap.append($0)
}
personHashMap.sortDB()
let result = queryArr.map {
personHashMap.numberOfSatisfiedWith($0[0], $0[2], $0[4], $0[6], Int($0[7])!)
}
return result
}
struct PersonHashMap {
var db: [String:[Int]] = [:]
mutating func append(_ person: [String]) {
let language = ["-", person[0]]
let jobGroup = ["-", person[1]]
let career = ["-", person[2]]
let soulFood = ["-", person[3]]
for lang in language {
for job in jobGroup {
for car in career {
for soul in soulFood {
let hashString = "\(lang)\(job)\(car)\(soul)"
if db[hashString] != nil {
db[hashString]!.append(Int(person[4])!)
} else {
db[hashString] = [Int(person[4])!]
}
}
}
}
}
}
func numberOfSatisfiedWith(_ language: String, _ jobGroup: String, _ career: String, _ soulFood: String, _ score: Int) -> Int {
let hashString = "\(language)\(jobGroup)\(career)\(soulFood)"
let numberSet: [Int] = db[hashString] ?? []
return numberOfGreaterNumbers(in: numberSet, standard: score)
}
mutating func sortDB() {
for datum in db {
db[datum.key] = datum.value.sorted()
}
}
private func numberOfGreaterNumbers(in array : [Int], standard: Int) -> Int {
var low = 0
var mid = 0
var high = array.count - 1
while low <= high {
mid = (low + high) / 2
if array[mid] < standard {
low = mid + 1
} else {
high = mid - 1
}
}
return array.count - low
}
}