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.
Maria 在大學打籃球時想要成為職業的球員,每一個賽季他都會保存他的比賽紀錄。 他列出了他在一場比賽中打破賽季最高分和最低分的紀錄次數。 從第一場比賽中的得分開始計算。
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.
#重點 回傳一個陣列包含她打破紀錄的這些次數。 陣列中第 0 個位置是她打破最高分的紀錄,陣列中第 1 個位置是打破最低分的紀錄。
Example 1:
scores = [12, 24, 10, 24]
solution:
先假設 scores 陣列中第 0 個位置的數值為基準點,並以 maxCount 與 minCount 作為打破最高、最低分的次數紀錄。
運用 for 迴圈遍歷 scores 陣列中的元素,判斷高於基準點則 maxCount += 1,低於基準點則 minCount += 1,
同時更新 maximum 與 minimum 的分數,並將打破次數放入陣列中後回傳。
Code 1: BigO(n)
'use strict'; const fs = require('fs'); process.stdin.resume(); process.stdin.setEncoding('utf-8'); let inputString = ''; let currentLine = 0; process.stdin.on('data', function(inputStdin) { inputString += inputStdin; }); process.stdin.on('end', function() { inputString = inputString.split('\n'); main(); }); function readLine() { return inputString[currentLine++]; } /* * Complete the 'breakingRecords' function below. * * The function is expected to return an INTEGER_ARRAY. * The function accepts INTEGER_ARRAY scores as parameter. */ function breakingRecords(scores) { // Write your code here let maximum = scores[0], minimum = scores[0], maxCount = 0, minCount = 0 for (let i = 0; i < scores.length; i++) { if (scores[i] > maximum) { maximum = scores[i] maxCount += 1 } else if (scores[i] < minimum) { minimum = scores[i] minCount += 1 } } return [maxCount, minCount]; } function main() { const ws = fs.createWriteStream(process.env.OUTPUT_PATH); const n = parseInt(readLine().trim(), 10); const scores = readLine().replace(/\s+$/g, '').split(' ').map(scoresTemp => parseInt(scoresTemp, 10)); const result = breakingRecords(scores); ws.write(result.join(' ') + '\n'); ws.end(); }
FlowChart:
Example 1
scores = [12, 24, 10, 24] scores[0] = 12 // scores[0] = 12 Maximum = 12, Minimum = 12, maxCount = 0, minCount = 0 // scores[1] = 24 Maximum = 24, Minimum = 12, maxCount = 1, minCount = 0 // scores[2] = 10 Maximum = 24, Minimum = 10, maxCount = 1, minCount = 1 // scores[3] = 24 Maximum = 24, Minimum = 10, maxCount = 1, minCount = 1 return [maxCount, minCount]; //[1, 1]