[C++] 프로그래머스 K 번째 수

2020. 12. 8. 17:38·Algorithm/Programmers
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    
    for(int i = 0; i < commands.size(); i++)	//배열의 1번째 원소만큼 반복
    {
        vector<int> list;	//정렬을 시킬 배열의 빈값
        
        for(int j = commands[i][0] - 1; j < commands[i][1]; j++)
            list.push_back(array[j]);	//문제에서의 i에서 j까지의 범위만큼 대입
            
        if(list.size() > 1)	//한개만 있을 경우는 그냥 정렬을 안한다.
            sort(list.begin(), list.end());	//정렬
        
        answer.push_back(list[commands[i][2] - 1]);	//k번째의 값을 대입
    }
    
    return answer;
}

풀이

  1. vector<int> list에 i~j의 값을 넣는다.
  2. list 배열을 정렬한다.
  3. 그중에 k번째 값을 answer에 넣어준다.
  4. 1,2,3을 commands.size()만큼 반복한다.

 

 

다른코드

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

using namespace std;

vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    
    for(int i = 0; i < commands.size(); i++)
    {
        vector<int> list(commands[i][1] - commands[i][0] + 1, 0);
        
        if(list.size() == 1)
        {
            list[0] = array[commands[i][0] - 1];
        }
        else
        {
            int temp = 0;
            for(int j = commands[i][0] - 1; j < commands[i][1]; j++)
            {
                list[temp] = array[j];
                temp++;
            }
            sort(list.begin(), list.end());
        }
        
        answer.push_back(list[commands[i][2] - 1]);
    }
    
    return answer;
}
저작자표시 (새창열림)

'Algorithm > Programmers' 카테고리의 다른 글

[C++] 프로그래머스 모의고사 완전탐색  (0) 2021.04.21
[C++] 프로그래머스 네트워크 DFS, BFS  (0) 2021.04.19
[C++] 프로그래머스 타겟넘버 DFS/BFS  (0) 2021.04.02
[C++] 프로그래머스 H-index  (0) 2021.01.04
[C++] 프로그래머스 가장 큰 수  (0) 2020.12.30
'Algorithm/Programmers' 카테고리의 다른 글
  • [C++] 프로그래머스 네트워크 DFS, BFS
  • [C++] 프로그래머스 타겟넘버 DFS/BFS
  • [C++] 프로그래머스 H-index
  • [C++] 프로그래머스 가장 큰 수
chanheess
chanheess
'왜' 그렇게 했는가?에 대한 생각으로 공부 및 작업의 저장관리
  • chanheess
    왜 그렇게 생각했는가?
    chanheess
  • 전체
    오늘
    어제
    • 분류 전체보기
      • Backend Programming
      • Game Programming
        • Unreal
        • DirectX
      • C++
        • Memo
        • Basic
        • Effective Modern
      • Java
      • Algorithm
        • Memo
        • Baekjoon
        • Programmers
        • HackerRank, LeetCode
      • Data Structure
      • Design Pattern
      • Etc
        • Memo
        • Daily Log
        • Book
  • 최근 글

  • 최근 댓글

  • 태그

    spring
    c++ 기초 플러스
    백준
    JPA
    위클리 챌린지
    오블완
    dfs
    SpringSecurity
    dp
    프로그래머스
    Java
    JWT
    알고리즘
    티스토리챌린지
  • hELLO· Designed By정상우.v4.10.0
chanheess
[C++] 프로그래머스 K 번째 수

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.