[프로그래머스] 가장 큰 수 (Python)

yuuforest·2023년 9월 21일

정렬

목록 보기
2/3
post-thumbnail

프로그래머스 문제 풀이 - 정렬

📰 문제


문제 확인 🏃


💡 입출력 예제


[6, 10, 2]

>> "6210"
[3, 30, 34, 5, 9]

>> "9534330"
[0, 0, 0, 0]

>> "0"

💬 풀이


🎵 첫번째 풀이

참고.https://school.programmers.co.kr/questions/24627

def solution(numbers):

    # 자릿수를 동일하게 만들어서 비교하기
    sorted_numbers = sorted(list(map(str, numbers)), reverse=True, key=lambda x : (x*4)[:4])
    answer = "".join(sorted_numbers)
    
    idx = 0
    while answer[idx] == '0' and idx < len(answer)-1:
        idx+=1

    return answer[idx:] 


✒️ 생각


이러저리 생각해보고 풀어보다가 결국 다른 분의 아이디어를 참고한 문제..😣

0000을 0으로 바꾸는 경우는 str -> int -> str로 바꿔서 할 수도 있었지만,
정답이 너무 클 수 있으니 문자열로 바꾸어 return 합니다. 가 신경쓰여서....

profile
🐥 Backend Developer 🐥

0개의 댓글