728x90

https://programmers.co.kr/learn/courses/30/lessons/42883

 

코딩테스트 연습 - 큰 수 만들기

 

programmers.co.kr

일단 이렇게 풀었는데 너무 복잡하게 푼 것 같아서 다시 풀어봐야겠다.

#include <string>
#include <vector>

using namespace std;

string solution(string number, int k) {
	int n = 0;
	int targetLength = number.length() - k;
	int length = number.length();
	string targetStr = "";
	bool flag = false;
	int maxNum = -1;
	int maxIndex;
	while (true) {
		for (int i = 0; i <= length - targetLength; i++) {
			if (stoi(number.substr(i, 1)) > maxNum) {
				maxNum = stoi(number.substr(i, 1));
				maxIndex = i;
				if (maxNum == 9)
					break;
			}
		}
		targetStr += number[maxIndex];
		number.erase(0, maxIndex + 1);
		length -= maxIndex;
		if (targetStr.length() == targetLength)
			break;
		maxNum = 0;
		maxIndex = 0;
	}
	return targetStr;
}
728x90

'알고리즘 문제' 카테고리의 다른 글

[프로그래머스] 행렬의 곱셈  (0) 2020.08.01
[프로그래머스] 기능개발  (0) 2020.07.31
[프로그래머스] 땅따먹기  (0) 2020.07.30
[프로그래머스] 폰켓몬  (0) 2020.07.28
[백준] 1780번 종이의 개수  (0) 2020.04.11

+ Recent posts