-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path메뉴 리뉴얼.swift
More file actions
66 lines (57 loc) · 1.81 KB
/
메뉴 리뉴얼.swift
File metadata and controls
66 lines (57 loc) · 1.81 KB
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
//
// 메뉴 리뉴얼.swift
//
//
// Created by chihoooon on 2022/01/19.
//
import Foundation
extension Array {
var combination: [[Element]] {
guard !isEmpty else { return [[]] }
return Array(self[1...]).combination.flatMap { [$0, [self[0]] + $0] }
}
}
func solution(_ orders:[String], _ course:[Int]) -> [String] {
var combArr: [[Array<Character>]] = []
var sortedOrders: [String] = []
var answer: [String] = []
orders.forEach {
sortedOrders.append(String($0.sorted()))
}
for setCount in course {
combArr = []
var setMenu: [Array<Character>: Int] = [:]
sortedOrders.forEach {
var arr = Array($0)
.combination
.filter { $0.count == setCount }
combArr.append(arr)
}
for i in 0..<combArr.count - 1 {
var maxCount = 0
for j in 0..<combArr[i].count {
var count = 0
for k in i+1..<combArr.count {
if combArr[k].contains(combArr[i][j]) {
count += 1
}
}
if count > 0 && maxCount <= count {
maxCount = count
if !setMenu.contains(where: { $0.0 == combArr[i][j] }) {
setMenu.updateValue(count, forKey: combArr[i][j])
}
}
}
}
let sortedSetMenu = setMenu.sorted { $0.1 > $1.1 }
if let count = sortedSetMenu.first?.value {
let menu = sortedSetMenu.filter { $0.value == count }.map { $0.0 }
menu.forEach {
answer.append(String($0))
}
}
}
answer.sort()
return answer
}