Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 20117
- 22869
- 호반우 상인
- 최소 #공배수 #최대 #공약수 #유클리드 #호제법 #lcm #gcd #c++ #boj #3343 #백준 #장미
- backtracking #codetree #디버깅 #삼성코테
- hcpc
- 줄어드는수
- LIS #가장긴증가하는부분수열 #
- boj #백준
- c++ #입출력 #속도 #ios #sync_with_stdio #cin #cout #tie
- 코딩
- N번째큰수
- 1174
- 3343
- c++ #boj #
- 진법변환 #2to10 #10to2 #이진법 #십진법 #변환 #bitset #c++
- 16202
- 백준 #다익스트라 #dijkstra #9370 #c++
- graph #최단경로
- 30870
- 3D #Reconstruction #computer #vision #volume #metric #tsdf #kinect #fusion
- graph
- 레드아보
- C++
- 사이클 없는 그래프
- 이분탐색 #dp #11053
- 백준
- BOJ
- 쌤쌤쌤
- 투포인터 #백준 #boj #20922 #22862
Archives
- Today
- Total
hyunjin
[level 1] 예산 문제, sort 사용 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/12982?language=cpp
난이도가 쉬운 문제였다.
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> d, int budget) {
int answer = 0;
sort(d.begin(),d.end());
for(auto iter = d.begin();iter!=d.end();iter++){
budget -= *iter;
if(budget < 0 )break;
else answer++;
}
return answer;
}
1. sort 함수 사용
- 헤더파일 : <algorithm>
- 3번째 인자로 내가 원하는 비교문을 넣을 수 있다. 이런 간단한 내림 오름 차순 말고 <key,value>이런 쌍이 있을 때 value값을 기준으로 key를 정렬할 수 있다.
bool compare(pair<int> a, pair<int> b){//first 기준 정렬
if(a.firts == b. first) //first가 같다면
return a.second < b.second; //second를 오름차순으로
return a.firts < b. first;//first가 다르다면 first 기준 오름차순으로
}
2.iterator 사용
728x90
'알고리즘 연습 > 프로그래머스' 카테고리의 다른 글
[level 2]탑 , 스택/큐 문제 (0) | 2020.07.07 |
---|---|
[level 1]키패드 누르기 , pair,abs 사용 (0) | 2020.07.06 |
[level 1] 수박수박수? 문제, 비트 연산자 사용 (0) | 2020.07.06 |
[level1] 주식가격 , reverse 함수 사용 , stack 사용 (0) | 2020.06.29 |
[level 1][2019 kakao blind recruitment] (0) | 2020.05.21 |