[C++] 프로그래머스 숫자게임

2024. 3. 5. 16:12·Algorithm/Programmers
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> A, vector<int> B) {
    int answer = 0;
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    
    int index = 0;
    for(int i = 0; i < B.size(); i++)
    {
        if(A[index] < B[i])
        {
            index++;
            answer++;
        }
    }
    
    return answer;
}

 

풀이

1. 낮은 순으로 정렬해준다.

2. A를 B로 하나씩 비교하면서 최대한 작은 수로 이길 수 있게 유도한다.

 

느낀점

탐욕적 풀이들은 웬만하면 다 쉽게 생각해야 쉽게 풀리는 것같다. 더 쉽게 생각해보자.

 

https://school.programmers.co.kr/learn/courses/30/lessons/12987

 

저작자표시 (새창열림)

'Algorithm > Programmers' 카테고리의 다른 글

[C++] 프로그래머스 이진 변환 반복하기  (0) 2024.04.01
[C++] 프로그래머스 징검다리건너기  (0) 2024.03.19
[C++] 프로그래머스 최고의 집합  (0) 2024.02.29
[C++] 프로그래머스 야근 지수  (0) 2024.02.29
[C++] 프로그래머스 숫자의 표현  (0) 2024.02.16
'Algorithm/Programmers' 카테고리의 다른 글
  • [C++] 프로그래머스 이진 변환 반복하기
  • [C++] 프로그래머스 징검다리건너기
  • [C++] 프로그래머스 최고의 집합
  • [C++] 프로그래머스 야근 지수
chanheess
chanheess
'왜' 그렇게 했는가?에 대한 생각으로 공부 및 작업의 저장관리
  • chanheess
    왜 그렇게 생각했는가?
    chanheess
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Backend Programming
      • Game Programming
        • Unreal
        • DirectX
      • C++
        • Memo
        • Basic
        • Effective Modern
      • Algorithm
        • Memo
        • Baekjoon
        • Programmers
        • HackerRank, LeetCode
      • Data Structure
      • Design Pattern
      • Etc
        • Memo
        • Daily Log
        • Book
  • 최근 글

  • 최근 댓글

  • 태그

    SpringSecurity
    dfs
    Java
    dp
    JWT
    티스토리챌린지
    spring
    프로그래머스
    오블완
    백준
    c++ 기초 플러스
    JPA
    위클리 챌린지
    알고리즘
  • hELLO· Designed By정상우.v4.10.0
chanheess
[C++] 프로그래머스 숫자게임
상단으로

티스토리툴바