Orderly Queue | LeetCode 899 | Theory + Python code
one of the first k
letters of s
and append it at the end of the string
→ 앞의 K개의 문자 중에서 하나 골라서 가장 맨 뒤로 붙임
lexicographically smallest string
→ 사전적으로 가장 작은 문자열을 반환
테스트 케이스 1
Input: s = "cba", k = 1 Output: "acb" Explanation: In the first move, we move the 1st character 'c' to the end, obtaining the string "bac". In the second move, we move the 1st character 'b' to the end, obtaining the final result "acb".
⇒ k=1
인 경우
주어진 움직임 방법으로 구한 모든 경우의 수가 len(s)
개 임
이 모든 경우를 구하고 (1 <= k <= s.length <= 1000
로 모든 경우 고려 가능)
그 중에서 사전적으로 가장 작은 문자열 반환
브루트포스 방식
사용
테스트케이스 2
Input: s = "baaca", k = 3 Output: "aaabc" Explanation: In the first move, we move the 1st character 'b' to the end, obtaining the string "aacab". In the second move, we move the 3rd character 'c' to the end, obtaining the final result "aaabc".
Orderly Queue | LeetCode | Intuitive Solution with Explanation
k=2 으로 123을 정렬한 경우 123 순열 조합이 모두 나옴
k=3 으로 123을 정렬한 경우 123 순열 조합 모두 나옴
2이상의 k를 가진 경우 주어진 문자를 사용해 나열 가능한 모든 조합 나옴
→ 버블 소트
의 원리 생각하면 됨
⇒ k>1 인 경우
그냥 문자열 정렬해서 반환