[프로그래머스][C++]추석 트래픽

C++/알고리즘 문제|2019. 9. 14. 18:55

문제:https://programmers.co.kr/learn/courses/30/lessons/17676

#include <string>
#include <vector>
#include <iostream>
using namespace std;

struct mytime {
    int hour;
    int minute;
    double sec;
};

double convertDouble(const mytime& t) {
    double dt=t.sec;
    dt+=t.minute*60;
    dt+=t.hour*60*60;
    return dt;
}
mytime convertTime(const double& d) {
    mytime t{};
    t.sec=d;
    while (t.sec>=60) {
        t.sec-=60;
        t.minute++;
        if (t.minute>=60) {
            t.hour++;
            t.minute-=60;
        }
    }
    return t;
}

mytime getTime(const string& origin) {
    mytime t{};
    string text="";
    for (int i=0;i<origin.size();i++) {
        char c=origin[i];
        if (c==' ') break;
        if (('0'<=c && c<='9') || c=='.') {
            text+=c;
        }
    }
    t.hour=stoi(text.substr(0,2));
    t.minute=stoi(text.substr(2,2));
    t.sec=stod(text.substr(4));
    return t;
}


double getRes(const string& origin) {
    string t="";
    for (auto it=origin.begin()+13; it!=origin.end();it++) {
        char c=*it;
        if (c=='s') continue;
        t+=c;
    }
    return stod(t);
}


int solution(vector<string> lines) {
    //times 세팅완료
    vector<vector<double>> times;
    for (int i=0;i<lines.size();i++) {
        lines[i].erase(lines[i].begin(),lines[i].begin()+11);//2016-09-15 제거
        double endTime=convertDouble(getTime(lines[i]));
        double startTime=endTime-getRes(lines[i])+0.001f;
        vector<double> v(2);
        v[0]=startTime;
        v[1]=endTime;
        times.push_back(v);
    }
    
    
    
    int max=1;
    for (int i=0;i<times.size();i++) {
        int sum = 1;
        double backTime=times[i][1]+1;
        for (int j=i+1;j<times.size();j++) {
            double frontTime=times[j][0];
            if (frontTime<backTime) {
                sum++;
            }
        }
        if (sum>max) {
            max=sum;
        }
    }

    return max;
}

'C++ > 알고리즘 문제' 카테고리의 다른 글

[프로그래머스][C++]스킬트리  (0) 2019.09.14
[프로그래머스][C++] 기능개발  (0) 2019.09.14

댓글()

[프로그래머스][C++]스킬트리

C++/알고리즘 문제|2019. 9. 14. 16:48

문제: https://programmers.co.kr/learn/courses/30/lessons/49993

#include <string>
#include <vector>
#include <map>

using namespace std;
map<char,char> needs;

bool isCheck(string skillTree) {
    for (int i=0;i<skillTree.size();i++) {
        if (needs.find(skillTree[i])==needs.end()) continue;
        bool isExists=false;
        for (int j=0;j<i;j++) {
            if (skillTree[j]==needs[skillTree[i]]) {
                isExists=true;
                break;
            }
        }
        if (!isExists) return false;
    }
    return true;
}

int solution(string skill, vector<string> skill_trees) {
    for (int i=1;i<skill.size();i++){
        needs[skill[i]]=skill[i-1];
    }
    
    int answer = 0;
    for (int i=0;i<skill_trees.size();i++) {
        if (isCheck(skill_trees[i])) {
            answer++;
        }
    }

    return answer;
}

'C++ > 알고리즘 문제' 카테고리의 다른 글

[프로그래머스][C++]추석 트래픽  (0) 2019.09.14
[프로그래머스][C++] 기능개발  (0) 2019.09.14

댓글()

[프로그래머스][C++] 기능개발

C++/알고리즘 문제|2019. 9. 14. 16:45

문제: https://programmers.co.kr/learn/courses/30/lessons/42586

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> days;
    for (int i=0;i<progresses.size();i++) {
        int progress=progresses[i];     
        int speed=speeds[i];
        int day=(100-progress)/speed;
        if ((100-progress) % speed != 0) {
            day++;
        }
        days.push_back(day);
    }
    vector<int> answer;
    int delay=days[0];
    int count=1;
    for (int i=1;i<days.size();i++) {
        if (days[i]>delay) {
            delay=days[i];
            answer.push_back(count);
            count=1;
        } else {
            count++;
        }
    }
    answer.push_back(count);

    return answer;
}

'C++ > 알고리즘 문제' 카테고리의 다른 글

[프로그래머스][C++]추석 트래픽  (0) 2019.09.14
[프로그래머스][C++]스킬트리  (0) 2019.09.14

댓글()