Juni_DEV

[Python, HackerRank] Breaking the Records 본문

Coding Interview

[Python, HackerRank] Breaking the Records

junni :p 2023. 4. 14. 11:01
반응형
 

Breaking the Records | HackerRank

Given an array of Maria's basketball scores all season, determine the number of times she breaks her best and worst records.

www.hackerrank.com

주어진 문제

더보기

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.

Input Format

The first line contains an integer , the number of games.
The second line contains  space-separated integers describing the respective values of score0, score1, ..., score (n-1).

Constraints

  • 1<= n <= 1000
  • 0<= score[i] <= 10^8


풀이

def breakingRecords(scores):
    # Write your code here
    max_sc, min_sc = -1,100000001
    max_ans, min_ans = 0,0
    
    for sc in scores :
        if sc > max_sc :
            max_sc = sc
            max_ans +=1
        if sc < min_sc :
            min_sc = sc
            min_ans +=1       
    
    return max_ans-1, min_ans-1

맨 처음에 max_sc, min_sc 초기화할 때 0,0으로 값을 설정했더니 score[0] 값이 0인 경우에 문제가 생김

-> 그래서 주어진 scores[i] 범위에 따라 -1, 100000001로 설정하고
    맨 처음 더해지는 1을 return 할 때 빼주는 식으로 번거롭게 했는데

==> 이럴게 아니라 그냥 score[0] 값으로 초기화를 하면 된다.

풀이2

def breakingRecords(scores):
    # Write your code here
    max_sc, min_sc = scores[0], scores[0]
    max_ans, min_ans = 0,0
    
    for sc in scores :
        if sc > max_sc :
            max_sc = sc
            max_ans +=1
        if sc < min_sc :
            min_sc = sc
            min_ans +=1       
    
    return max_ans, min_ans
반응형

'Coding Interview' 카테고리의 다른 글

[Python, HackerRank] Camel Case 4  (0) 2023.04.19
[Python, LeetCode] 9. Palindrome Number  (0) 2023.04.14
[Python, LeetCode] 1. Two Sum  (0) 2023.04.14
[Python, HackerRank] Mini-Max Sum  (0) 2023.04.13
[Python, HackerRank] Time-conversion  (0) 2023.04.13
Comments