반응형

문제출처

https://programmers.co.kr/learn/courses/30/lessons/42891


효율성 테스트가 있는 문제이다. 

전체 탐색을 하면 효율성 테스트에서 통과하지 못한다. idea는 정렬을 통해 가장 작은 값의 시간을 한번에 빼는 방법을 사용하였다.


소스코드

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
32
33
34
35
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
bool comp(pair<intint> a, pair<intint> b) {
    return a.second < b.second;
}
 
int solution(vector<int> food_times, long long k) {
    vector<pair<intint>> food;
    int size = food_times.size();
    for(int i=0; i<size; i++)
        food.push_back(make_pair(food_times.at(i), i+1));
    
    sort(food.begin(), food.end());
    
    int time=0;
    for(vector<pair<intint>>::iterator it=food.begin(); it!=food.end(); it++size--) {
        long long leadTime = (long long)(it->first - time) * size;
        if(leadTime == 0)
            continue;
        
        if(leadTime <= k) {
            k-=leadTime;
            time=it->first;
        } else {
            k%=size;
            sort(it, food.end(), comp);
            return (it+k)->second;
        }       
    }
    return -1;
}
cs




반응형

'Algorithm' 카테고리의 다른 글

[BOJ 10809] 알파벳 찾기  (0) 2019.01.07
[BOJ_11724] 연결 요소의 개수  (0) 2019.01.07
[BOJ 11004] K번째 수  (0) 2019.01.07
[BOJ 3047] ABC  (0) 2019.01.06
[BOJ 1987] 알파벳  (0) 2019.01.05

+ Recent posts