[LeetCode] 3079. Find the Sum of Encrypted Integers

Chobby·2025년 12월 19일

LeetCode

목록 보기
850/989

😎풀이

  1. 암호화 헬퍼 함수 정의
    1-1. 각 자릿수 분리
    1-2. 최댓값을 갖는 자릿수로 모든 자릿수 변환
  2. nums의 모든 요소를 암호화 과정을 거쳐 합산
  3. 합산한 결괏값 반환
function sumOfEncryptedInt(nums: number[]): number {
    return nums.reduce((acc, cur) => acc + encrypt(cur), 0)
};

function encrypt(num: number) {
    const digit = []
    while(num) {
        digit.push(num % 10)
        num = Math.floor(num / 10)
    }
    const max = Math.max(...digit)
    const len = digit.length
    const strEncrypt = String(max).repeat(len)
    return Number(strEncrypt)
}
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글