본문 바로가기

코딩테스트/프로그래머스

[프로그래머스] 최댓값과 최솟값

728x90
반응형


🔗 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/12939

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


👩‍💻 코드

#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

string solution(string s) {
    vector<int> numbers;
    stringstream ss(s);
    string digit;
    
    while (ss >> digit) {
        numbers.push_back(stoi(digit));
    }
    
    return to_string(*min_element(numbers.begin(), numbers.end())) + " " + to_string(*max_element(numbers.begin(), numbers.end()));
}

📝 풀이

stringstream을 이용하여 공백으로 구분된 숫자들을 추출하여 numbers 벡터에 저장한다.

numbers 벡터에서 최솟값과 최댓값을 찾아 정답 형식으로 문자열을 생성하여 return 한다.

728x90
반응형