문제

입출력

풀이
#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++)
{
int start = commands[i][0]-1;
int end = commands[i][1]-1;
int num = commands[i][2]-1;
vector<int> tmp;
for(int j = start; j <= end; j++)
{
tmp.push_back(array[j]);
}
sort(tmp.begin(), tmp.end());
answer.push_back(tmp[num]);
}
return answer;
}
더보기

아놔
쓸데없는 실수로 인한 시간 낭비,....
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
string str = " ";
for(int a = 0; a < array.size(); a++)
{
str = array[a]; //쓸데없는 실수
}
for(int i = 0; i < commands.size(); i++)
{
int start = commands[i][0];
int end = commands[i][1];
int num = commands[i][2];
int count = (end - start) +1;
str.substr(start, end);
sort(str.begin(), str.end());
answer[i] = str[num+1]; //아니 벡터에는 pushback으로 받아야죠
}
return answer;
}
진짜 생각을 하고 좀 코드를 짜자고요.... 어이없는 실수 투성이 ~

아니 생각해보니 왜 string으로 받아서 괜히 어렵게 갔는지 모르겠다..
벡터를 사용해서 그냥 정렬하면 되는데.. 진짜
'Windows > Algorithm' 카테고리의 다른 글
| [프로그래머스] H-Index | C/C++ (0) | 2024.10.16 |
|---|---|
| [프로그래머스] 가장 큰 수 | C/C++ (4) | 2024.10.16 |
| [백준] 2533번 사회망 서비스(SNS) | C/C++ (0) | 2024.07.29 |
| [백준] 15649번 N과 M (1) | C/C++ (2) | 2024.07.05 |
| [프로그래머스] 오픈채팅방 | C/C++ (0) | 2024.06.26 |