블로그

특별한 케이스로 나중을 위해 외워두면 좋을 문제들 본문

PS

특별한 케이스로 나중을 위해 외워두면 좋을 문제들

왕방토 2023. 1. 4. 17:54
728x90

숫자를 이어 붙였을 때 가장 큰 숫자가 되도록 정렬하기

--> 얘는 100% 이해가 가지 않았던 문제임 다시 한번 보는 게 좋을듯

 

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

 

프로그래머스

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

programmers.co.kr

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

bool cmp(const string &str1, const string &str2) {
    string s1(str1);
    string s2(str2);
    int i = 0;
    while(s1.length() != 4) {
        s1 += s1[i++];
    }
    i=0;
    while(s2.length() != 4) {
        s2 += s2[i++];
    }
    int a = stoi(s1);
    int b = stoi(s2);
    return a>b;
}

string solution(vector<int> numbers) {
    string answer = "";
    vector<string> strs;
    bool all_zero = true;
    for(int n:numbers) {
        if(n != 0) all_zero = false;
        strs.push_back(to_string(n));
    }
    if(all_zero) answer = "0";
    else {
        sort(strs.begin(), strs.end(), cmp);
        for(string s: strs) {
            answer += s;
        }
    }
    return answer;
}

 

왔던 길을 또 가는 것이 가능한 원형모양의 길 케이스에서 bfs

-> 한방향으로 쭉 가기 or 한 번 유턴하기 두 경우 모두 확인해 준 후 min 값

 

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

 

프로그래머스

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

programmers.co.kr

 

#include <string>
#include <vector>
#include <algorithm>

using namespace std;


int solution(string name) {
    int answer = 0;
    
    int len = name.length();
    int min_dis = len - 1;
    for (int i = 0; i < len; i++) {
        int c = name[i] - 'A';
        if (c > 13) c = 26 - c;
        answer += c;
        bool is_all_a = true;
        for (int j = i + 1; j < len; j++) {
            if (name[j] != 'A') {
                is_all_a = false;
                break;
            }
        }
        if (is_all_a) min_dis = min(min_dis, i);
    }
    
    for (int i = 0; i < len; i++) {
        
        if (name[i] == 'A') {
            int ni = i + 1;
            while (ni < len && name[ni] == 'A') ni++;

            int dis1 = 0;
            int dis2 = 0;
            if (i == 0) {
                dis1 = len - ni;
                dis2 = 2 * (len - ni);
            }
            else {
                dis1 = 2 * (i - 1) + len - ni;
                dis2 = 2 * (len - ni) + i - 1;
            }
            min_dis = min(min_dis, min(dis1, dis2));
        }
    }
    
    
    answer += min_dis;
    
    return answer;
}

'PS' 카테고리의 다른 글

[Softeer] 사물인식 최소 면적 산출 프로그램  (0) 2023.01.06
실수 모음(이게 뭐지?)  (0) 2023.01.04
[C++] 17928: 오큰수  (0) 2022.06.30
[C++] 5014: 스타트링크  (0) 2022.06.30
[C++] 2644: 촌수 계산  (0) 2022.06.29
Comments