일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- firestore
- five lines of cdde
- 리팩터링
- 코딩테스트입문
- css학습
- SWIFT
- mrc
- TDD
- Swift디자인패턴
- firebase
- unittest
- 앱의생명주기
- UIKit
- 프로그래머스
- alamofire
- 클린코드
- RC
- AutoLayout
- 카카오맵클론
- five lines of code
- IOS
- hackerrank
- storekit2
- RxSwift
- Di
- ARC
- algorithm
- Safari Inspector
- ios면접
- Swift코딩테스트
- Today
- Total
샘성의 iOS 개발 일지
[Implementation] Breaking the Records 본문
문제 설명:
Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.
Example
scores = [12, 24, 10, 24]
Scores are in the same order as the games played. She tabulates her results as follows:
Count
Game Score Minimum Maximum Min Max
0 12 12 12 0 0
1 24 12 24 0 1
2 10 10 24 1 1
3 24 10 24 1 1
Given the scores for a season, determine the number of times Maria breaks her records for most and least points scored during the season.
Function Description
Complete the breakingRecords function in the editor below.
breakingRecords has the following parameter(s):
- int scores[n]: points scored per game
Returns
- int[2]: An array with the numbers of times she broke her records. Index 0 is for breaking most points records, and index 1 is for breaking least points records.
내 풀이:
func breakingRecords(scores: [Int]) -> [Int] {
// 최고 점수를 저장할 변수, 몇번 최고 점수를 기록했는지 담을 변수
var bestScore = scores[0]
var bestRecordCount = 0
// 최저 점수를 저장할 변수, 몇번 최저 점수를 기록했는지 담을 변수
var leastScore = scores[0]
var leastRecordCount = 0
for score in scores {
// 기존의 최고점수가 새로운 점수보다 낮으면, 새로운 점수를 최고점수에 넣고 최고기록 + 1
if bestScore < score {
bestScore = score
bestRecordCount += 1
}
// 기존의 최저점수가 새로운 점수보다 높으면, 새로운 점수를 최저점수에 넣고 최저기록 + 1
if leastScore > score {
leastScore = score
leastRecordCount += 1
}
}
return [bestRecordCount, leastRecordCount]
}
'Algorithm > HackerRank' 카테고리의 다른 글
[Implementation] Migratory Birds (0) | 2023.05.17 |
---|---|
[Implementation] Divisible Sum Pairs (0) | 2023.05.17 |
[Implementation] Between Two Sets (0) | 2023.05.14 |
[Implementation] Number Line Jumps (0) | 2023.05.12 |
[Implementation] Apple and Orange (0) | 2023.05.12 |