Coding Problem/프로그래머스

[프로그래머스/Lv1] - 문자열 내 p 와 y의 개수

마탁이 2021. 4. 18. 21:52

programmers.co.kr/learn/courses/30/lessons/12916

 

코딩테스트 연습 - 문자열 내 p와 y의 개수

대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를

programmers.co.kr

  • 코딩 테스트 준비를 위한 스터디에서 스터디원과 함께 진행하기 위한 단계로 풀지 않고 넘어갔던 문제들을 풀고있다.
  • 간단하지만 다시 기초를 다진다는 마음으로 진행한다.

더보기
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> arr, vector<vector<int>> commands) {
    vector<int> answer;

    const int commandsSize = commands.size();
    for (int cmdIdx = 0; cmdIdx < commandsSize; cmdIdx++)
    {
        std::vector<int> slice;
        const int startIdx = commands[cmdIdx][0] - 1;
        const int endIdx = commands[cmdIdx][1] - 1;
        const int k = commands[cmdIdx][2];

        const int arrSize = arr.size();
        for (int arrIdx = 0; arrIdx < arrSize; arrIdx++) 
        {
            if (startIdx <= arrIdx && endIdx >= arrIdx)
            {
                slice.push_back(arr[arrIdx]);
            }
        }
        
        sort(slice.begin(), slice.end());
        answer.push_back(slice[k - 1]);
    }

    return answer;
}


int main()
{
    // input
    std::vector<int> arr = { 1,5,2,6,3,7,4 };
    std::vector<int> cmd_1 = { 2,5,3 };
    std::vector<int> cmd_2 = { 4,4,1 };
    std::vector<int> cmd_3 = { 1,7,3 };
    std::vector<std::vector<int>> commands;
    commands.push_back(cmd_1);
    commands.push_back(cmd_2);
    commands.push_back(cmd_3);

    solution(arr, commands);

    return 0;
}