https://fringe-polyester-65b.notion.site/899-Orderly-Queue-dcb8358dcef34f508c186d5707687a24?pvs=4
k>1 인 경우
2이상의 k를 가진 경우 주어진 문자를 사용해 나열 가능한 모든 조합 나옴
→버블 소트
의 원리 생각하면 됨
→ 그냥 문자열 정렬해서 사전순으로 가장 작은 것 반환
k=1인 경우
주어진 움직임 방법으로 구한 모든 경우의 수가len(s)
개 임
이 모든 경우를 구하고 (1 <= k <= s.length <= 1000
로 모든 경우 고려 가능)
→ 그 중 사전적으로 가장 작은 문자열 반환
→브루트포스 방식
사용
from heapq import heappop, heappush
class Solution:
def orderlyQueue(self, s: str, k: int) -> str:
s = list(s)
answer = "".join(s)
if k>1:
answer = "".join(sorted(s))
else:
for i in range(len(s)):
s = s[1:]+ [s[0]]
answer = min("".join(s), answer)
return answer