회고

[개발일지] - 296(주말)

Happy Programmer 2024. 4. 21. 23:08

(1).백준 28417번 스케이트보드는 점수 기준에 맞춰서

앞의 두 경기 중 최고점과 뒤의 5경기 중 최고점 2개 총 3개 점수의 합으로 비교한다고 할 때

우승자의 점수를 구해야 하는 문제였다.

 

요구 조건에 맞춰서 Math.max를 통해 앞 두개를 비교하고

나머지는 5개나 되기 때문에 sort를 통해 비교 후 뒤에서 2개를 가져와 더해주고

최종적으로 두 값의 합과 기존 max를 비교해 최고점수를 구했다.

 

const input = `2
1 2 4 2 3 1 1
5 5 5 5 5 5 5`.split('\n')

let max = 0

for(let i = 1 ; i < input.length ; i++){
    const arr = input[i].split(' ').map(Number)
    const twoPoint = Math.max(arr[0], arr[1])
    const anotherArr = [arr[2], arr[3], arr[4], arr[5], arr[6]].sort((a,b) => a - b)
    const anotherPoint = anotherArr[3] + anotherArr[4]
    max = Math.max(max, twoPoint + anotherPoint)
}

console.log(max)