-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path[프로그래머스] 신재웅_보석 쇼핑.swift
43 lines (36 loc) · 1.06 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
import Foundation
func solution(_ gems:[String]) -> [Int] {
var totalGemKinds: Set<String> = []
gems.forEach{ totalGemKinds.update(with: $0) }
var gemDictionary: Dictionary<String, Int> = [:]
var head: Int = 0
var tail: Int = 0
var result: (Int, Int) = (head, tail)
while gemDictionary.keys.count < totalGemKinds.count {
defer { tail += 1 }
let gem = gems[tail]
gemDictionary[gem, default: 0] += 1
}
result = (head + 1, tail)
while tail <= gems.count {
defer {
if tail - (head + 1) < result.1 - result.0 {
result = (head + 1, tail)
}
}
let headGem = gems[head]
if gemDictionary[headGem]! >= 2 {
gemDictionary[headGem]! -= 1
head += 1
continue
}
if tail == gems.count {
break
} else {
let tailGem = gems[tail]
gemDictionary[tailGem]! += 1
tail += 1
}
}
return [result.0, result.1]
}