145. 1로 만들기

김해준's avatar
Dec 28, 2025
145. 1로 만들기

사용언어 : Java

문제
notion image
풀이
/* 2로 나누는데 짝수면 그냥 나누고 홀수면 -1해서 나눔 최종 1이 나올떄 까지 걸린 연산 횟수 1 => 0 2 => 1 3 => 1 4 => 2 5 => 2 6 => 2 7 => 2 8 => 3 9 => 3 10 => 3 11 => 3 12 => 3 13 => 3 14 => 3 15 => 3 16 => 4 ... 30 => 4 31 => 4 32 => 5 33 => 5 2의 제곱 수 마다 연산 횟수가 1씩 증가한다 1/0 . 2~3/1 . 4~7/2 . 8~15/3 . 16~31/4 num_list의 크기만큼 반복 num_list[i]가 최초로 2의n승 보다 작을 때 return n - 1 answer += return 7 / 2 => 3 3 / 2 => 1 2회 8 / 2 => 4 4 / 2 => 2 2 / 2 => 1 3회 15 / 2 => 7 7 / 2 => 3 3 / 2 => 1 3회 19 / 2 => 9 9 / 2 => 4 4 / 2 => 2 2 / 2 => 1 4회 1. num_list만큼 반복 for(int i = 0 ; i < num_list.length; i++) 2. num_list[i]를 1이 될떄까지 2로 계속 나눔 int result = num_list[i]; while(result > 1) { result /= 2; answer++; } 3. answer 반환 return answer */ class Solution { public int solution(int[] num_list) { int answer = 0; for(int i = 0 ; i < num_list.length; i++) { int result = num_list[i]; while(result > 1) { result /= 2; answer++; } } return answer; } }
notion image
Share article