코딩 테스트 풀이 🛠

[LeetCode - mideum] 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers

엄성준 2023. 1. 8. 17:25

ㅋ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

 

나의 풀이

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * @param {string} n
 * @return {number}
 */
var minPartitions = function(n) {
 
    const arr = n.split('')
    const numArr = arr.map((item)=>{
       return Number(item)
    }).sort((a,b)=>{
        return b-a
    })
 
    return (numArr[0])
};
cs

 

문제를 풀며 느낀 점

- forEach()와 map()에 대한 개념이 부족한 것 같습니다.

문제 풀이를 해보자면 먼저 매개 변수 n의 type이 string이기 때문에 바로 split('') 함수를 이용해서 arr에 배열로 담았습니다. 그 후 map() 함수를 이용해서 arr의 담긴 요소를 item으로 순회하면서 Number()를 통해서 형변환을 해준 값이 담긴 새로운 배열을 sort() 함수를 통해서 내림 차순 정렬한 배열을 numArr에 할당해주었고 numArr에서 가장 큰 값인 numArr [0] 담긴 값을 return 했습니다.