You are given a positive integer num consisting of exactly four digits. Split num into two new integers new1 and new2 by using the digits found in num. Leading zeros are allowed in new1 and new2, and all the digits found in num must be used.
- For example, given num = 2932, you have the following digits: two 2's, one 9 and one 3. Some of the possible pairs [new1, new2] are [22, 93], [23, 92], [223, 9] and [2, 329].
Return the minimum possible sum of new1 and new2.
Example 1:
Input: num = 2932
Output: 52
Explanation: Some possible pairs [new1, new2] are [29, 23], [223, 9], etc.
The minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52.
Example 2:
Input: num = 4009
Output: 13
Explanation: Some possible pairs [new1, new2] are [0, 49], [490, 0], etc.
The minimum sum can be obtained by the pair [4, 9]: 4 + 9 = 13.
Constraints:
- 1000 <= num <= 9999
나의 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/**
* @param {number} num
* @return {number}
*/
var minimumSum = function(num) {
const nums = String(num).split('').sort()
let answerArr=[]
let answer =0
for(let i=0; i<nums.length/2; i++){
answerArr.push(nums[i]+nums[i+2])
}
console.log(answerArr)
for(let num of answerArr){
answer += Number(num)
}
return answer
};
|
cs |
문제를 풀며 느낀 점
- 숫자 4자리가 주어지는데 가장 작은 수의 합을 return 해 주는 문제라서 split('')을 하고 sort()를 이용해서 오름 차순 정렬뒤 index 1,3의 조합과 2,4의 조합을 합한 값을 return 해주었습니다.
'코딩 테스트 풀이 🛠' 카테고리의 다른 글
[프로그래머스] 직각삼각형 출력하기 (0) | 2023.02.09 |
---|---|
[프로그래머스] 합성수 찾기 (0) | 2023.02.06 |
[Leet Code - eazy] 2114. Maximum Number of Words Found in Sentences (0) | 2023.01.29 |
[Leet Code - eazy] 2413. Smallest Even Multiple (0) | 2023.01.29 |
[Leet Code - eazy] 1672. Richest Customer Wealth (0) | 2023.01.28 |