[파이썬/알고리즘] Leetcode - 1689. Partitioning Into Minimum Number Of Deci-Binary Numb
728x90

[문제]

A decimal number is called deci-binary if each of its digits is either 0 or 1 without any leading zeros. For example, 101 and 1100 are deci-binary, while 112 and 3001 are not.
Given a string n that represents a positive decimal integer, return the minimum number of positive deci-binary numbers needed so that they sum up to n.

[예시]

Example 1:
Input: n = "32"
Output: 3
Explanation: 10 + 11 + 11 = 32

Example 2:
Input: n = "82734"
Output: 8

Example 3:
Input: n = "27346209830709182346"
Output: 9

[코드]

class Solution: def minPartitions(self, n: str) ->
	int: answer = list(n)
    	answer.sort(reverse=True) # answer을 리스트로 변환시키고 내림차순 정렬한다. return answer[0] # 가장 큰수가 답 

# 더 간단한 풀이 
class Solution: def minPartitions(self, n: str) -> int: 
	return max(n) # 가장 큰수가 답 


- deci-binary number는 자리수로 0 또는 1만 가질 수 있다.
- 2진수를 더해서 n을 만들때 몇번 더했는지를 리턴한다.


생각하는게 까다로워서 난이도가 medium인 것 같다. 코드는 정말 간결......

자세한 풀이는 참고.

 

Solution: Partitioning Into Minimum Number Of Deci-Binary Numbers

This is part of a series of Leetcode solution explanations (index). If you liked this solution or fou...

dev.to

 

320x100