Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 그리디
- 자바스크립트
- DP
- 백준
- 코테
- 깃
- 컴퓨터과학과
- 탐욕알고리즘
- two pointers
- LeetCode
- 방송통신대학교
- java
- it
- 자바
- 이진탐색
- boj
- sliding window
- Binary Search
- 코딩
- greedy
- 리트코드
- Git
- algorithm
- 완전탐색
- 투포인터
- dynamic programming
- 방송대
- 방통대
- javascript
- 알고리즘
Archives
- Today
- Total
개발이 취미인 주니어 기획자
[DP][JavaScript][LeetCode] #338. Counting Bits 본문
728x90
반응형
#Dynamic Programming(DP) #EASY
Counting Bits - LeetCode
Can you solve this real interview question? Counting Bits - 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
leetcode.com
🌷 문제 설명
✏️ LeetCode 연습문제: Counting Bits
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.
🎃 제한 사항
0 <= n <= 10^5
입출력 예
| nums | result |
| 2 | [0, 1, 1] |
| 5 | [0, 1, 1, 2, 1, 2] |
🌷 내 코드

var countBits = function (n) {
let dp = [0];
let offset = 1;
for (let i = 1; i <= n; i++) {
if (offset * 2 === i) {
offset = i;
}
dp[i] = 1 + dp[i - offset];
}
return dp;
};
🌷 코멘트
DP가 그렇게 많이 나온다길래 제일 기본형을 한번 해보았다
하나하나 다 써서 패턴을 찾아야되는데 그게 좀 어려웠음
블로그 내용에 문제가 있다면 댓글 혹은 아래로 연락주세요!
~대가리 꽃밭인 디지털 노마드가 꿈이예요~
🧚♀️ Gyumin Lee
📧 gyumin.q.lee@gmail.com
qminlee723 - Overview
noob. qminlee723 has 8 repositories available. Follow their code on GitHub.
github.com
728x90
반응형
'문제 풀이 > 알고리즘 문제 풀이' 카테고리의 다른 글
| [이진탐색][Java][BOJ] #1654. 랜선 자르기 (0) | 2025.01.15 |
|---|---|
| [Hashset][Java][BOJ] #2776. 암기왕 (0) | 2025.01.14 |
| [투포인터][JavaScript][LeetCode] #9. Palindrome Number (0) | 2023.04.14 |
| [스택(Stack)][JavaScript][LeetCode] #735. Asteroid Collision (1) | 2023.04.13 |
| [DFS][JavaScript][LeetCode] #733. Flood Fill (0) | 2023.04.12 |