알고리즘 오답노트

substr 사용법

substr(시작주소, 자를길이);

split

#include<iostream>
int main(){
    string str="java c c++ python";
    istringstream ss(str);
    string stringBuffer;
    
    while (getline(ss, stringBuffer, ' ')){
        x.push_back(stringBuffer);
        cout<<stringBuffer<<" ";
    }
}

max

{
        land[1+i][0] += max(max(land[i][1], land[i][2]), land[i][3]);
        land[1+i][1] += max(max(land[i][0], land[i][2]), land[i][3]);
        land[1+i][2] += max(max(land[i][0], land[i][1]), land[i][3]);
        land[1+i][3] += max(max(land[i][0], land[i][1]), land[i][2]);
        i++;
    }

    answer = *max_element(land[s_len - 1].begin(), land[s_len-1].end());

map

     for (auto tmp : m)
        answer *= (tmp.second.size() + 1);
        

삼항연산자

max, max_element

벡터를 스텍처럼.

벡터를 스택처럼 사용할 수 잇음. ( 벡터는 스텍의 모든 기능이 구현된다. 효율성은 모르겟음. )

transform

벡터의 원소에 tolower같은 함수들을 적용시킬수 있음. (다른것도 가능)

split

문자열 쪼개기!! c++에서는 필수

sort 람다식

람다식으로 세번째 인자에 함수주소가 아니라, 그자리에서 바로 함수를 작성할수 있음.

벡터 erase

벡터도 erase 가능. 인덱스 접근 가능한건 다 erase할수 있지 않을까?

next_permutation

조합, dfs 로만들어도 되겠지만, next_permutation이 쉽겠지?!

max_element 람다식

람다식으로도 가능!!

sort 세번째 인자의 함수.

다익스트라(프로그래머스 배달 문제)


#include <iostream>
#include <vector>
#include <queue>
#include <map>

using namespace std;

int solution(int N, vector<vector<int> > road, int K) {
    int answer = 0;

    map<int, vector<vector<int>>> m;
    
    for(int i = 0; i < road.size(); i++)
    {
        m[road[i][0]].push_back(road[i]);
        m[road[i][1]].push_back({road[i][1], road[i][0], road[i][2]});
    }
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int>>> pq;
    pq.push({1,0});
    vector<int> cost(N + 1, 99999999);
    cost [1] = 0;
    while(!pq.empty())
    {
        //
        //pair<int,int> f_pq = pq.front();
        int now = pq.top().first;
        int add_cost = pq.top().second;
        pq.pop();

        if(cost[now] >= add_cost)
        {
            for(int i = 0; i < m[now].size(); i++)
            {
                // 이게 옮겨지는곳.
                //m[f_pq.first()][i][1];
                if(cost[m[now][i][1]] > add_cost + m[now][i][2])
                {  // now : 현재 위치. 
                    cost[m[now][i][1]] =  add_cost + m[now][i][2];
                    pq.push({m[now][i][1] , add_cost + m[now][i][2]});
                }
            }
        }
    }
    for(int i = 1 ; i < cost.size(); i++)
        if(cost[i] <= K)
            answer++;
    return answer;
} // map쓸때 상황봐가며 쓸것, 아무때나 쓰면 m접근자 쓰다가 복잡해짐

다익스트라를 쓰는 이유가 있음.

좋은 웹페이지 즐겨찾기