hyelie
hyelie
Hyeil Jeong
       
글쓰기    관리    수식입력
  • 전체보기 (495)
    • PS (283)
      • Algorithm (28)
      • PS Log (244)
      • Contest (6)
      • Tips (5)
    • Development (52)
      • Java (14)
      • Spring (23)
      • SQL (2)
      • Node.js (2)
      • Socket.io (3)
      • Study (4)
      • Utils (4)
    • DevOps (36)
      • Git (5)
      • Docker (4)
      • Kubernetes (2)
      • GCP (3)
      • Environment Set Up (8)
      • Tutorial (12)
      • Figma (2)
    • CS (74)
      • OOP (7)
      • OS (24)
      • DB (2)
      • Network (24)
      • Architecture (0)
      • Security (2)
      • Software Design (0)
      • Parallel Computing (15)
    • Project (15)
      • Project N2T (5)
      • Project ASG (0)
      • Project Meerkat (1)
      • Model Checking (7)
      • Ideas (2)
    • 내가 하고싶은 것! (34)
      • Plan (16)
      • Software Maestro (10)
      • 취준 (8)
hELLO · Designed By 정상우.
hyelie

hyelie

PS/PS Log

23.04.03. 풀었던 문제들

Leetcode 881. Boats to Save People

 two pointer + greedy 문제.

 각 boat에 최대 2명의 인원이 올라가고, limit보다 넘으면 안 되므로 오름차순 정렬한 이후 제일 큰 것을 올리고, 이후 작은 것을 올릴 수 있으면 올린다. 이것을 반복하면 된다.

class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        sort(people.begin(), people.end(), less<int>());
        int s = 0, e = people.size()-1;
        int answer = 0;
        while(s<=e){
            int sum= people[e];
            e--; answer++;
            if(sum + people[s] <= limit){
                s++;
            }
        }
        return answer;
    }
};

 

시간복잡도

 정렬에 O(nlogn), two-pointer에 O(n)이므로 O(nlogn)이다.

 

 

 

 

 

저작자표시 (새창열림)

'PS > PS Log' 카테고리의 다른 글

23.04.05. 풀었던 문제들  (0) 2023.04.05
23.04.04. 풀었던 문제들  (0) 2023.04.04
23.04.02. 풀었던 문제들  (0) 2023.04.02
23.04.01. 풀었던 문제들  (0) 2023.04.02
23.03.31. 풀었던 문제들  (0) 2023.03.31
    hyelie
    hyelie

    티스토리툴바