2024/02/11 3

프로그래머스 알고리즘 고득점 Kit - 스택/큐

떨어지지 않은 주식 가격 #include #include #include using namespace std; vector solution(vector prices) { vector answer(prices.size()); stack s; // 가격 안떨어지는 한 계속 push됨 for(int i=0; i prices[i]) { // 가격 떨어진 거 answer[s.top().first] = i - s.top().first; s.pop(); } else break; } s.push(make_pair(i, prices[i])); } while(!s.empty()) { answer[s.top().first] = (prices.size()-1) - s.top().first; s.pop(); } return a..

web/알고리즘 2024.02.11

c++ algorithm 관련 함수들

sort()로 정렬하기 - sort(v.begin(), v.end()) 하면 오름차순 정렬, sort(v.rbegin(), v.rend()) 하면 내림차순 정렬 - sort(v.begin(), v.end(), compare) 해서 사용자 정의 비교함수 정의해도 됨, 리턴타입 bool인 함수 정의 bool compare(pair a, pair b) { if(a.second == b.second) { return a.first b.second; } max element 구하기 - max_element(start, end)를 이용하면 [start, end) 범위 중에 가장 큰 값의 iterator를 반환 - *max_element(start, end)를 ..

web/알고리즘 2024.02.11