샘성의 iOS 개발 일지

[Implementation] Breaking the Records 본문

Algorithm/HackerRank

[Implementation] Breaking the Records

SamusesApple 2023. 5. 14. 12:31
728x90

문제 설명:

  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]
}
728x90

'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