forked from lunchScreen/Problem_Solving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[프로그래머스] 신재웅_메뉴 리뉴얼.swift
52 lines (42 loc) · 1.69 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
import Foundation
func solution(_ orders:[String], _ course:[Int]) -> [String] {
var resultArray: [[String]] = []
for number in course {
let bests = bestCourse(orders: orders, n: number).filter{ $0.count > 0 }
bests.count > 0 ? resultArray.append(bests) : Void()
}
return Array( resultArray.reduce([], +) ).sorted()
}
func bestCourse(orders: [String], n: Int) -> [String] {
var menuToCalledNumber: [String: Int] = [:]
orders.compactMap {
course(of: $0, n: n)
}.forEach { map in
map.sorted().forEach {
if let calledNumber = menuToCalledNumber[$0] {
menuToCalledNumber[$0] = calledNumber + 1
} else {
menuToCalledNumber[$0] = 1
}
}
}
let maxNumber = menuToCalledNumber.map{ $0.value }.max()
guard let max = maxNumber else { return [] }
return max > 1 ? menuToCalledNumber.filter{ $0.value == max }.map { $0.key } : []
}
func course(of string: String, n: Int) -> [String]? {
guard string.count >= n else { return nil }
var result: [String] = []
func dfs(depth: Int, head: Int, totalString: String, currentString: String) {
if depth == currentString.count {
result.append(String(currentString.sorted()))
return
}
for index in head..<totalString.count {
let character = totalString[totalString.index(totalString.startIndex, offsetBy: index)]
dfs(depth: depth, head: index+1, totalString: totalString, currentString: currentString + String(character))
}
}
dfs(depth: n, head: 0, totalString: string, currentString: "")
return result
}