[LeetCode] Counting Bits
https://leetcode.com/problems/counting-bits/description/?envType=study-plan-v2&envId=leetcode-75
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n),
ans[i] is the number of 1's in the binary representation of i.
Example 1:
Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10
Example 2:
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
Constraints:
0 <= n <= 105
Follow up:
- It is very easy to come up with a solution with a runtime of O(n log n). Can you do it in linear time O(n) and possibly in a single pass?
- Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
숫자 n이 주어지고, 각 i의 자리에 i를 이진법으로 나타냈을때 1의 수를 나타낸 n+1의 길이를 갖는 배열을 내보내야 한다.
Follow up에 언급된 C++의 __builtin_popcount 는 컴파일러 내장 함수로,
unsigned int를 받아 1인 bit의 개수를 리턴해준다고 한다. 이 문제에 한해서는 사실상 치트 메서드인 셈이다ㅋㅋㅋㅋ
사실상 십진법 숫자를 이진법으로 변환하는 문제나 다름이 없다.
그래서 일단은 비트연산자는 사용하지 않고 단순히 이진법으로 바꿔주는 느낌으로 진행해 봤음.
/**
* @param {number} n
* @return {number[]}
*/
var countBits = function(n) {
const res = new Array(n+1).fill(0);
res[0] = 0;
for(let i=1; i<=n; i++){
let current = i;
let pow = 16; // 2^16 = 65,536
let count = 0;
while(current > 0){
if(current < Math.pow(2,pow)){
pow--;
continue;
}
current = current - Math.pow(2, pow);
if(current >= 0){
count++;
pow--;
}
}
res[i] = count;
}
return res;
};
2의 16승부터 시작해서 그 값을 현재 숫자에서 뺐을 때 0 이상이면 지수를 낮추고, 1을 더한다.
그런 식으로 이진법으로 바꾸는 거와 다름없이 계산을 돌림.
그나마 제약조건을 참고해서 시작하는 지수를 16으로 제한하긴 했지만...
하나하나 다 일일이 while문을 돌아야 하니, 시간효율은 엄청 낮다.
이번엔 비트 연산자를 써서 풀어 보자.
i=7이라고 했을 때, 7을 이진법으로 나타내면 000..00111 이다.
그리고 7을 2로 나누고 나머지를 버리면 3이고, 3을 이진법으로 나타내면 000...00011 이다.
즉, 7>>1 이라는 비트연산자로 계산하면 맨 오른쪽을 한 칸 밀어내는 것과 같은데,
결과적으로 7 >> 1을 한 숫자 조합과 3을 이진법으로 나타냈을 때의 숫자 조합이 앞선 0을 제외하고 11로 동일해 진다. (당연함)
마찬가지로 5 = 000..101, 5>> 1 = 2 = 000..010 으로,
0을 제외한 나머지 조합이 10으로 동일해 진다!
그리고 2로 나누었기 때문에 현재 대상인 숫자가 홀수냐 짝수냐에 따라 1의 총 수가 달라지는데,
홀수냐 짝수냐는 사실상 첫번째 0의 자리의 지수가 1이냐 0이냐로 나타낼 수 있다.
그 값은 i & 1로 간단하게 계산할 수 있다.
즉, 아래처럼 간단한 코드로 효율 좋은 결과를 낼 수 있게 된다~~
var countBits = function(n) {
const ans = new Array(n+1).fill(0);
for(let i=0; i<=n; i++){
ans[i] = ans[i>>1] + (i&1);
}
return ans;
}
'코테연습' 카테고리의 다른 글
[LeetCode] Single Number (0) | 2025.06.17 |
---|---|
[LeetCode] Unique Paths (0) | 2025.06.09 |
[LeetCode] House Robber (0) | 2025.05.27 |