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 | 31 |
Tags
- MySQL
- css framework
- docker
- unity
- Google Developer API
- Digital Ocean
- Spring Boot
- Google Refund
- nodejs
- mongoDB
- Camera Movement
- java
- Unity Editor
- express
- Packet Network
- draganddrop
- --watch
- rpg server
- screencapture
- critical rendering path
- Git
- Unity IAP
- server
- OverTheWire
- springboot
- Camera Zoom
- react
- spread 연산자
- SDK upgrade
- linux
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 3 . 숫자 게임 본문
https://school.programmers.co.kr/learn/courses/30/lessons/12987
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
해결 방법
- 두 배열을 오름차순으로 정렬한다.
- 가장 큰값부터 비교를 한다. 만약 B 가 이길 수 있는 상황이라면 A와 B가 대결했음 처리를 하고, a++, b++ 한 뒤 다음으로 넘어간다.
- 만약 A가 B보다 크다면, A를 이길 수 있는 B는 없다는 의미이다. 그러므로 졌다고 판단하고 A를 넘긴다. B는 가장 작은 수로 대결할것이므로, B의 인덱스인 b는 증가하지 않고 A의 인덱스만 증가시킨다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> A, vector<int> B) {
int answer = 0; int a =0; int b= 0;
sort(A.rbegin(), A.rend());
sort(B.rbegin(), B.rend());
while(a<A.size() && b <B.size()){
if(B[b] > A[a]){ //B[b]가 A[a]를 이길 수 있음. 답을 증가하고, a++, b++수행
answer++;
b++;
}
a++; //A[a]를 이길 수 없으므로, 졌다고 생각하고 a를 다음으로 넘김 B[b]는 여전히 후보에 남아있음.
}
return answer;
}
'Algorithm(c++) > Level 3' 카테고리의 다른 글
[프로그래머스] Level 3. 기지국 설치 (0) | 2025.02.27 |
---|---|
[프로그래머스] Level 3. 단속 카메라 (0) | 2025.02.26 |
[프로그래머스] Level 3. 최고의 집합 (0) | 2025.02.25 |
[프로그래머스] Level 3. 이중 우선순위 큐 (1) | 2025.02.23 |
[프로그래머스] Level 3. 정수 삼각형 (0) | 2025.02.22 |