[SWEA/C++] 1859 백만 장자 프로젝트

Hanbi·2022년 7월 28일
0

Problem Solving

목록 보기
25/108
post-thumbnail

문제

📈코테에 자주 나오는 주식 문제
https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5LrsUaDxcDFAXc&categoryId=AV5LrsUaDxcDFAXc&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=CCPP&select-1=2&pageSize=10&pageIndex=1

풀이

코드

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

using namespace std;

int main() {
	int test_case;
	int T;

	cin >> T;
	for (test_case = 1; test_case <= T; ++test_case) {
		int N;
		vector<int> v;
		long long ans = 0;

		cin >> N;
		for (int i = 0; i < N; i++) {
			int tmp;

			cin >> tmp;
			v.push_back(tmp);
		}

		int max_budget = v[N - 1]; //최고가
		for (int j = N - 1; j >= 0; j--) {
			if (max_budget > v[j]) { //현재가가 최고가보다 낮으면
				ans += max_budget - v[j]; //팔아서 이득을 남김
			}
			else {
				max_budget = v[j]; //현재가가 최고가
			}
		}

		cout << "#" << test_case << " " << ans << endl;
	}

	return 0;
}
profile
👩🏻‍💻

0개의 댓글