일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Selenium 네이버 블로그
- 셀레니움
- Java
- kwoss2341
- 서로이웃추가 자동
- 서이추 자동
- 스크래퍼
- 크롤러
- 웹소켓
- 네이버 블로그
- Node
- amqplib
- 웹소켓 서버
- 실시간 웹소켓 서버
- 국세청
- nodejs
- socket.io
- 네이버 블로그 이웃추가 자동
- 실시간
- rabbitmq
- Selenium
- 크롤링
- node.js
- 서로이웃추가 매크로
- 서이추 매크로
Archives
- Today
- Total
defaultK
[프로그래머스] 카카오 외벽 점검 c++ 본문
programmers.co.kr/learn/courses/30/lessons/60062
코딩테스트 연습 - 외벽 점검
레스토랑을 운영하고 있는 스카피는 레스토랑 내부가 너무 낡아 친구들과 함께 직접 리모델링 하기로 했습니다. 레스토랑이 있는 곳은 스노우타운으로 매우 추운 지역이어서 내부 공사를 하는
programmers.co.kr
-기본적인 아이디어.
순열을 이용하여 dist배열의 모든 경우의 수에 대하여
weak에 배치하여 최솟값을 구한다.
-세부적인 아이디어.
거리 계산을 편하게 하기 위해 weak 벡터에 n+weak[i]를 push_back하여
원형의 효과가 나게 선형으로 구현한다.
dist의 순열을 반복하기 위해 <algorithm>에 구현된 prev_permutation()를 이용한다.
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool comp(int a, int b) {
return a > b;
}
//https://programmers.co.kr/learn/courses/30/lessons/60062
int solution(int n, vector<int> weak, vector<int> dist) {
int answer =10000;
int ws = weak.size();
int ds = dist.size();
sort(dist.begin(),dist.end(),comp);
for(int i=0; i<ws; i++) weak.push_back(n+weak[i]);
int std,stdist,cn,total,sw;
for(int stw=0; stw<ws; stw++)
{
while(1)
{
std=0;
stdist=dist[0];
cn=0;
total=0;
sw=0;
for(int i=stw; i<ws+stw; i++)
{
if(cn==0)
{
cn++;
continue;
}
if(stdist-(weak[i]-weak[i-1])<0)
{
i--;
cn=0;
total++;
if(total+1>=answer||total==ds)
{
sw=1;
break;
}
std++;
stdist=dist[std];
}
else
{
cn++;
stdist=stdist-(weak[i]-weak[i-1]);
}
}
if(sw==0&&answer>total+1)
{
answer=total+1;
}
if(!prev_permutation(dist.begin(),dist.end()))
{
break;
}
}
}
if(answer==10000) answer=-1;
return answer;
}
-피드백
처음에는 확실한 알고리즘이 있을 줄 알아서 시간을 많이 허비했다.
모든 경우의 수에 대하여 순열로 코딩을 하려하니 실력이 부족했다;;
덕분에 permutation 함수를 알게되었다. (너무간편)
지금 코드에서 좀 더 효율적으로
중복을 조금 더 추려내면 시간이 더 줄어들것 같다.
'알고리즘 ( C++ ) > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 카카오 무지의 먹방 라이브 c++ (0) | 2021.01.20 |
---|---|
[프로그래머스] (이진트리 탐색) 카카오 길 찾기 게임 c++ (0) | 2021.01.19 |
[프로그래머스] (그리디) 구명보트 c++ (0) | 2021.01.10 |
[프로그래머스] 카카오 기둥과 보 설치 c++ (0) | 2021.01.09 |
[프로그래머스] (문자열) 카카오 문자열 압축 c++ (0) | 2021.01.08 |
Comments