Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- useEffect
- Vanilla JavaScript
- visual test
- next.js
- 사탕게임
- 리팩토링
- ZOD
- react
- React.memo
- next-auth
- Python
- interaction test
- context api
- TextFormField
- 프로그래머스
- Props Drilling
- locale data
- 백준
- custom hook
- kakao blind recruitment
- Flutter
- 피보나치 함수
- storybook
- suffixicon
- React-hook-form
- javascript
- Github Actions
- typescript
- 이메일 인증
- useMemo
Archives
- Today
- Total
Dev Diary
[프로그래머스] 로또의 최고 순위와 최저 순위 본문
SMALL
문제 이해
1등 -> 6개 일치
2등 -> 5개 일치
3등 -> 4개 일치
4등 -> 3개 일치
5등 -> 2개 일치
6등 -> 그 외
위에 주어진 등수와 등수를 결정하는 조건을 바탕으로 미리 ranks라는 배열에 저장해두고 시작한다.
ex) ranks[맞은 번호의 개수] = 등수
민우가 적은 숫자 중에서 win_nums에 있는 숫자는 확정된 숫자이므로 decidedRank 변수에 +1을 해주고 값이 0인 부분에는 아무 숫자나 넣을 수 있으므로 최고 순위인 경우에는 0인 부분이 모두 win_num에 있는 숫자와 일치할 것이고 최저 순위인 경우에는 전부 일치하지 않는 경우이므로 결과적으로
- 최고 순위: ranks[decidedRank+zeroCount]
- 최저 순위: ranks[decidedRank]
를 출력한다.
문제 풀이
function solution(lottos, win_nums) {
var answer = [0,0];
const ranks=[6,6,5,4,3,2,1]
let decidedRank=0
let zeroCount=0
for (const num of lottos){
if (num===0){
zeroCount+=1
}
else if (num!==0 && win_nums.includes(num)){
decidedRank+=1
}
}
answer[0]=ranks[decidedRank+zeroCount] !== undefined ? ranks[decidedRank+zeroCount]:6
answer[1]=ranks[decidedRank] !== undefined ? ranks[decidedRank]:6
return answer
}
LIST
'Algorithms' 카테고리의 다른 글
[백준] 11501 주식 (0) | 2024.07.27 |
---|---|
[프로그래머스] 2023 KAKAO BLIND RECRUITMENT 개인정보 수집 유효기간 (0) | 2023.06.21 |
[백준] 9375 패션왕 신해빈 (1) | 2023.06.04 |
[백준] 1003 피보나치 함수 (0) | 2023.06.02 |
[백준] 20365 블로그2 (0) | 2023.05.29 |