[프로그래머스] Level 1

OOING·2023년 6월 22일
0

코테 환경 적응+IDE 없이 풀기를 위해 프로그래머스 풀어보는 중~.~

달리기 경주

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

vector<string> solution(vector<string> players, vector<string> callings) {

    map<string, int> m1;
    map<int, string> m2;
    
    for(int i = 0; i<players.size(); i++){
        m1[players[i]] = i;
        m2[i] = players[i];
    }
    
    string temp;
    for(int i = 0; i<callings.size(); i++){
        int now = m1[callings[i]];
        string before = m2[now-1];
        m2[now] = before;
        m2[now-1] = callings[i];
        m1[callings[i]] = now-1;
        m1[before] = now;
        
    }
        vector<string> answer;
    for(auto m : m2) answer.push_back(m.second);
    return answer;
}

C++에도 auto 자료형이 있다길래 써봤다

추억 점수

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

vector<int> solution(vector<string> name, vector<int> yearning, vector<vector<string>> photo) {
    vector<int> answer;
    map<string, int> m1;
    for(int i = 0; i<name.size();i++){
        m1[name[i]] = yearning[i];
    }
    
    for(auto i : photo){
        int sum = 0;
        for(auto j : i){
            if(m1.find(j) != m1.end()) sum+=m1[j];
        }
        answer.push_back(sum);
    }
    return answer;
}

map을 쓰는 문제들이 꽤 자주 등장하는 것 같다

조건에 부합하는 중고거래 댓글 조회하기

SELECT B.TITLE, B.BOARD_ID, R.REPLY_ID, R.WRITER_ID, R.CONTENTS, 
	   DATE_FORMAT(R.CREATED_DATE, '%Y-%m-%d') AS CREATED_DATE
FROM USED_GOODS_BOARD B, USED_GOODS_REPLY R
WHERE B.BOARD_ID = R.BOARD_ID 
	  AND DATE_FORMAT(B.CREATED_DATE, '%Y-%m-%d') >= DATE_FORMAT('2022-10-01', '%Y-%m-%d') 
      AND DATE_FORMAT(B.CREATED_DATE, '%Y-%m-%d') <= DATE_FORMAT('2022-10-31', '%Y-%m-%d')
ORDER BY R.CREATED_DATE, B.TITLE ASC

DATE 타입 포맷 변경
DATE_FORMAT(field명, 'format')
➡️ DATE_FORMAT(CREATED_DATE, '%Y-%m-%d')

DATE 타입 비교
DATE_FORMAT(B.CREATED_DATE, '%Y-%m-%d') >= DATE_FORMAT('2022-10-01', '%Y-%m-%d')

바탕화면 정리

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<string> wallpaper) {
    vector<int> drag;
    drag.push_back(wallpaper.size());
    drag.push_back(wallpaper[0].length());
    drag.push_back(0);
    drag.push_back(0);
    
    for(int i = 0; i < wallpaper.size(); i++) {
        string now = wallpaper[i];
        for(int j = 0; j < now.length(); j++){
            if(now[j] == '#'){
                drag[0] = drag[0] > i ? i : drag[0];
                drag[1] = drag[1] > j ? j : drag[1];
                drag[2] = drag[2] < i + 1 ? i+1 : drag[2];
                drag[3] = drag[3] < j + 1 ? j+1 : drag[3];
            }
        }
    }
    return drag;
}

깔끔하게 코드 써보겠다고 삼항연산자( ? : )를 오랜만에 썼다. 평소엔 쓰지도 않으면서..

덧칠하기

#include <string>
#include <vector>

using namespace std;

int solution(int n, int m, vector<int> section) {
    int answer = 1;
    int now = section[0];
    int nowSec = now + m;
    
    for(int i : section){
        if(i >= now && i < nowSec){
            continue;
        }
        else{
            now = i;
            nowSec = now + m;
            ++answer;
        }
    }
    return answer;
}
profile
HICE 19

0개의 댓글