일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- UserDefaults
- programmers
- 코딩테스트
- AsyncImage
- ios15
- ViewModifier
- autolayout
- NotificationCenter
- cocoapods
- swiftUI
- CoreLocation
- alamofire
- segue
- image
- SWIFT
- Appearance변경
- android
- Kotlin
- format형식
- UIStackView
- ios
- SwiftUI_Preview_Provider
- Alert
- snapkit
- Java
- MapKit
- EventKit
- 백준
- pod install
- kakaomap
Archives
- Today
- Total
감자주먹밥
[LeetCode] 1431. Kids With the Greatest Number of Candies 본문
728x90
여분의 사탕을 각 아이들한테 다 줬을때, 그 아이가 가지고 있는 사탕이 제일 많으면 true 아니면 false로 배열을 만들어 리턴해주면 된다.
class Solution {
func kidsWithCandies(_ candies: [Int], _ extraCandies: Int) -> [Bool] {
let max = candies.max()!
var result: [Bool] = []
for candy in candies {
if max <= candy + extraCandies {
result.append(true)
} else { result.append(false) }
}
return result
}
}
강제 언래핑시 오류는 일어날 일 없어보여서 그냥 해버렸다.
단계별로 해결했는데 다른 솔루션 중 재밋는 솔루션을 확인했다.
class Solution {
func kidsWithCandies(_ candies: [Int], _ extraCandies: Int) -> [Bool] {
let requiredCount = (candies.max() ?? 0) - extraCandies
return candies.map({ $0 >= requiredCount })
}
}
가장 많은 사탕 갯수와 여분의 사탕을 뺀 값을 가지고, 아이들이 가지고 있는 사탕이 이 값보다 적으면 가장 많은 사탕을 가질 수 없다는 걸 알 수 있다...
728x90
'알고리즘' 카테고리의 다른 글
[LeetCode] 345. Reverse Vowels of a String (0) | 2023.06.22 |
---|---|
[LeetCode] 1071. Greatest Common Divisor of Strings (0) | 2023.06.22 |
Comments