반응형
오늘 풀 문제는 Partition Labels라는 문제이며
링크는 https://leetcode.com/problems/partition-labels/입니다
위 문제는 string을 파티션으로 나누는 문제입니다
조건은 각 문자가 한 파티션에 최대한 나타나게 잘게 쪼개는 게 조건이었습니다
저는 각각의 알파벳마다 마지막 위치를 찾아서 map에 저장 한 뒤에
각각의 파티션의 끝을 찾을 때마다 결과 배열에 저장하였습니다
func max(x, y int) int {
if x > y {
return x
}
return y
}
func partitionLabels(S string) []int {
alpha := make(map[rune]int)
for idx, c := range(S) {
alpha[c] = idx
}
start := 0
end := 0
var ans []int
for idx, _ := range(S) {
end = max(end, alpha[rune(S[idx])])
if idx == end {
ans = append(ans, idx - start + 1)
start = idx + 1
}
}
return ans
}
읽어주셔서 감사합니다
'Language > Go' 카테고리의 다른 글
[Golang으로 도전하는 Leetcode] Check If Two String Arrays are Equivalent (0) | 2020.11.30 |
---|---|
[Golang으로 도전하는 Leetcode] All Elements in Two Binary Search Trees (0) | 2020.09.07 |
[Golang으로 도전하는 Leetcode] Repeated Substring Pattern (0) | 2020.09.04 |
[Golang으로 도전하는 Leetcode] Contains Duplicate III (0) | 2020.09.02 |
[Golang으로 도전하는 Leetcode] Largest Time for Given Digits (0) | 2020.09.02 |