ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ •์ˆ˜๋ฅผ ์ด์–ด ๋ถ™์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์•Œ์•„๋‚ด ์ฃผ์„ธ์š”.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ฃผ์–ด์ง„ ์ •์ˆ˜๊ฐ€ [6, 10, 2]๋ผ๋ฉด [6102, 6210, 1062, 1026, 2610, 2106]๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ณ , ์ด์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜๋Š” 6210์ž…๋‹ˆ๋‹ค.

0 ๋˜๋Š” ์–‘์˜ ์ •์ˆ˜๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด numbers๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ˆœ์„œ๋ฅผ ์žฌ๋ฐฐ์น˜ํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • numbers์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 100,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • numbers์˜ ์›์†Œ๋Š” 0 ์ด์ƒ 1,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ •๋‹ต์ด ๋„ˆ๋ฌด ํด ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด return ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

numbersreturn

[6, 10, 2] 6210
[3, 30, 34, 5, 9]

 

 

๋‚˜์˜ ํ’€์ด

from itertools import permutations

def solution(numbers):
    for i in range(len(numbers)):
        numbers[i] = str(numbers[i])
        
    return max(list(map("".join, permutations(numbers))))

์šฐ์„  ์ด๋ ‡๊ฒŒ ํ’€์–ด๋ดค์„ ๋•Œ ์‹คํ–‰์€ ๋œ๋‹ค. ๊ทผ๋ฐ ์ฑ„์ ํ•˜๊ณ  ์ œ์ถœํ–ˆ์„ ๋•Œ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๋กœ ํ…Œ์ŠคํŠธ๊ฐ€ ์ „๋ถ€ ์‹คํŒจํ•ด์„œ 0์ ์ด ๋– ๋ฒ„๋ฆฌ๊ณ  ๋ง์•˜๋‹ค. ํ‘ํ‘ 

๋ฌด์„œ์šด ๋นจ๊ฐ„์ƒ‰

์ƒ๊ฐ์„ ํ•ด๋ณด๋‹ˆ ์ˆœ์—ด๋กœ ๋‚˜์—ด์„ ํ•˜๋ฉด ์—„์ฒญ๋‚˜๊ฒŒ ๋งŽ์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์ƒ๊ธฐ๋‹ˆ๊นŒ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ํ’€ ํ•„์š”๊ฐ€ ์žˆ์„๊ฒƒ๊ฐ™๋‹ค. ์–ด์ฐจํ”ผ ์ œ์ผ ํฐ ์ˆ˜๋ฅผ ๊ณ ๋ฅด๋Š” ๊ฑฐ๋‹ˆ๊นŒ ์•ž์ž๋ฆฌ์ˆ˜๊ฐ€ ์ œ์ผ ๋†’์€ ์ˆซ์ž๋ฅผ ๋ฝ‘์•„์„œ ์ฐพ์œผ๋ฉด ๋˜๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. ์ž๋ฆฌ ์ˆ˜๊ฐ€ ํ•˜๋‚˜๋ผ๋ฉด ์ƒ๊ด€์ด์—†๋Š”๋ฐ ๋‘์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ๊ทธ ๋ถ€๋ถ„์„ ์‹ ๊ฒฝ์จ์„œ ํ’€์–ด์–ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

๋จธ๋ฆฌ์†์œผ๋กœ๋Š” ๋Œ€์ถฉ ๋Œ์•„๊ฐ€๋Š”๊ฑฐ ๊ฐ™์€๋ฐ ์™œ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋ คํ•˜๋ฉด ๋ณ€์ˆ˜๋ฅผ ์–ด๋–ป๊ฒŒ ์„ค์ •ํ•ด์•ผํ•˜๋Š”์ง€ ์–ด๋–ป๊ฒŒ ๋Œ๋ ค์•ผํ•˜๋Š”์ง€ ์ž๊พธ ํ—ท๊ฐˆ๋ฆฐ๋‹ค. ์ง‘์—๊ฐ€์„œ ๋‹ค์‹œ ํ•ด๋ด์•ผ์ง€

def solution(numbers):
    answer = ""
    prevMok = 0
    while len(numbers) > 0:
        for i in numbers:
            
            if len(str(i)) > 1:
                nowMok = i // 10 * (len(str(i))-1)
            else:
                nowMok = i
        
            if nowMok > prevMok :
                popNum = i
            prevMok = nowMok
        
        numbers.remove(popNum)
        print("numbers : {}\n".format(numbers))
        answer += str(popNum)
        
    return answer
        
numbers=[6,12,2]
print(solution(numbers))

 

๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฝ‘์•„๋‚ด๋Š”๊ฒƒ์œผ๋กœ..

if len(str(numbers[i])) > 1:
            tempNumbers[i] = numbers[i] // 10 * (len(str(numbers[i])) - 1)
        else:
            tempNumbers[i] = numbers[i]

 

 

# ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๊ฐ€์žฅ ํฐ ์ˆ˜

def solution(numbers):
    dic = {}
    answer = ""
    
    for i in numbers:
        dic[i] = i if len(str(i)) < 2 else i // 10 ** (len(str(i))-1)
        
    
    print("dic : {}".format(dic))
    sortedDic = sorted(dic.items(), key=lambda x: x[1] , reverse=True)
    print("sortedDic : {}".format(sortedDic))

    for i in range(len(sortedDic)):
        answer += str(sortedDic[i][0])
        print("answer : {}".format(answer))

    return answer

ใ…œใ…œ์งœ์ฆ๋‚œ๋‹ค ์ด๋ ‡๊ฒŒํ•˜๋ฉด ๊ฐ™์€ ์‹ญ์˜์ž๋ฆฌ์ผ๋•Œ๊ฐ€ ๋ฌธ์ œ๋‹ค. ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด ๋˜๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๊ณ  ๋˜๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๊ณ  ์ƒ๊ธฐ๊ณ  ์ƒ๊ธฐ๊ณ  ์ƒ๊ธฐ๊ณ  ์ƒ๊ธฐ๊ณ  ์ƒ์ƒ์ƒ์ƒ 

 

def solution(numbers):
    dic = {}
    answer = ""
    
    # print("numbers: {}".format(numbers))
    numbers.sort()
    numbers.reverse()
    # print("numbers: {}".format(numbers))

    for i in numbers:
        dic[i] = i if len(str(i)) < 2 else i // 10 ** (len(str(i))-1)
        
    
    # print("dic : {}".format(dic))
    sortedDic = sorted(dic.items(), key=lambda x: x[1] , reverse=True)
    # print("sortedDic : {}".format(sortedDic))

    for i in range(len(sortedDic)):
        answer += str(sortedDic[i][0])
        # print("answer : {}".format(answer))

    return answer

ํ‰ค

 

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ

# ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ
# ์™€์šฐ ์–ด๋–ป๊ฒŒ ์ด๋ ‡๊ฒŒ ์ƒ๊ฐํ•  ์ˆ˜๊ฐ€ ์žˆ์ฃ  ์ฐจ์•”๋‚ด
def solution2(numbers):
    numbers = list(map(str, numbers))
    print("numbers: {}".format(numbers))

    # ์ด ๋ถ€๋ถ„ ๋ญ๋ƒ ๋Œ€์ฒด? 
    # number์˜ ์›์†Œ๊ฐ€ 1000 ๋ฏธ๋งŒ์ด๋‹ˆ๊นŒ x*3์œผ๋กœ,,,,
    numbers.sort(key=lambda x: x*3, reverse=True)
    print("numbers: {}".format(numbers))

    return str(int(''.join(numbers)))

 

 

 

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€