Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- custombottomsheet
- BFS
- Tuist
- reactorkit
- task cancellation
- 타임라인 포맷팅
- claen architecture
- swift navigationcontroller
- custom navigation bar
- paragraph style
- identifiable
- coordinator
- swift 백준
- RxSwift
- rxdatasources
- swift custom ui
- UIKit
- swift bottomsheet
- 버튼 피드백
- swift 점선
- swift dashed line
- swift concurrency
- uikit toast
- button configuration
- traits
- SWIFT
- domain data
- custom ui
- task cancel
- DP
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 21921 블로그 본문
풀이 아이디어
- 가장 일반적인 슬라이딩 윈도우로 풀이합니다
- 단, 특이사항이 있는데 같은 구간합이 있는 경우가 몇번인지도 나태내줘야합니다
- 매번 result = max(a,b) 로 치환하는 대신 조건문을 통해 진행합니다
자세한 풀이는 코드의 주석을 참고해주세요
풀이
let nx = readLine()!.split(separator: " ").map { Int($0)! }
let (n, x) = (nx[0], nx[1])
let input = readLine()!.split(separator: " ").map { Int($0)! }
var initialSum = input[0..<x].reduce(0, +)
var result = initialSum
var resultCount = 1
for i in x..<n {
initialSum = initialSum - input[i - x] + input[i]
if initialSum > result {
result = initialSum
resultCount = 1 // 새로운 최대값이 등장했으므로 초기화
} else if initialSum == result {
resultCount += 1 // 같은 최대값이 등장했으므로 증가
}
}
if result == 0 {
print("SAD")
} else {
print(result)
print(resultCount)
}
'Algorithm' 카테고리의 다른 글
[Swift 백준] 1676 팩토리얼 0의 개수 (0) | 2025.01.26 |
---|---|
[Swift 백준] 음식물 피하기 (0) | 2025.01.24 |
[Swift 백준] 2559 수열 (0) | 2025.01.22 |
[Swift 백준] 14465 소가 길을 건너간 이유 5 (0) | 2025.01.21 |
[Swift 백준] 18870 좌표 압축 (0) | 2025.01.20 |