GameChoi
Choi Programming
GameChoi
전체 방문자
오늘
어제
  • 분류 전체보기 (468)
    • C++ Algorithm & Study (184)
      • C++ & Algorithm Strategies (45)
      • Game Math & DirectX 11 (72)
      • Server + UE5 (29)
      • Lyra Clone Coding (37)
    • Create Game (284)
      • [Window API] Game Client & .. (55)
      • [DirectX] DirectX 2D & 3D (155)
      • [UE5] BLUEPRINT & C++ (74)
    • odds and ends (0)
      • English (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Player Move Packet
  • server
  • UE5
  • RPG Game
  • Destination Move Packet
  • Direct11
  • Player State
  • job queue
  • Game Room
  • protobuf
  • Other Character
  • Direct3D
  • client
  • c++
  • session
  • Algorithm Strategies
  • Game Server
  • GAME Client
  • core
  • Network Worker

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
GameChoi

Choi Programming

[Programmers] 구명보트
C++ Algorithm & Study/C++ & Algorithm Strategies

[Programmers] 구명보트

2023. 1. 17. 20:58

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

0. Headers

#include <vector>
#include <algorithm>
using namespace std;

1. 알고리즘

 - 구명보트의 무게 제한은 항상 사람들의 몸무게 중 최댓값보다 크게 주어지므로 사람들을 구출할 수 없는 경우

   - 위 조건이 있으므로 정렬하고 앞과 뒤를 비교하면서 계산

int solution(vector<int> people, int limit) {
    int answer = 0;
    int front = 0, back = 0;
    
    sort(people.begin(), people.end());
}

 - 백터에서 마지막 값을 빼고 그 값과 처음 백터의 값을 더해 limit보다 작은 경우 통과

   - 통과되지 못하면 마지막 값만 나온 경우임

int solution(vector<int> people, int limit) {
    while(people.size() > front){
        back = people.back();
        people.pop_back();
        
        if(people[front] + back <= limit){ answer++; front++; }
        else answer++;
    }
}

2. 완성 코드

#include <vector>
#include <algorithm>
using namespace std;

int solution(vector<int> people, int limit) {
    int answer = 0;
    int front = 0, back = 0;
    
    sort(people.begin(), people.end());
    
    while(people.size() > front){
        back = people.back();
        people.pop_back();
        
        if(people[front] + back <= limit){ answer++; front++; }
        else answer++;
    }
    return answer;
}

저작자표시 (새창열림)

'C++ Algorithm & Study > C++ & Algorithm Strategies' 카테고리의 다른 글

[Programmers] 행렬의 곱셈  (0) 2023.01.23
[Programmers] 카펫  (0) 2023.01.21
[Programmers] 이상한 문자 만들기  (0) 2023.01.16
[Programmers] 예상 대진표  (0) 2023.01.10
[Programmers] 점프와 순간 이동  (0) 2023.01.09
    'C++ Algorithm & Study/C++ & Algorithm Strategies' 카테고리의 다른 글
    • [Programmers] 행렬의 곱셈
    • [Programmers] 카펫
    • [Programmers] 이상한 문자 만들기
    • [Programmers] 예상 대진표
    GameChoi
    GameChoi

    티스토리툴바