You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|[Leetcode-318. Maximum Product Of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/description/)|[c++](./leetcode/318.maximum-product-of-word-lengths.cpp), [python](./leetcode/318.maximum-product-of-word-lengths.py)| O\(N^2\)| O\(N\)|\-| - |
|[Leetcode-342. Power Of Four](https://leetcode.com/problems/power-of-four/description/)|[python](./leetcode/342.power-of-four.py), [c++](./leetcode/342.power-of-four.cpp)| O\(1\)| O\(1\)|\-| - |
|[Leetcode-3048. Earliest Second To Mark Indices I](https://leetcode.com/problems/earliest-second-to-mark-indices-i/description/)|[python](./leetcode/3048.earliest-second-to-mark-indices-i.py), [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp)| O\(M\*logM\)| O\(M\)|\-| - |
235
236
236
237
## Dynamic Programming
237
-
| Problem(39) | Solution | Time | Space | Note | Ref |
238
+
| Problem(40) | Solution | Time | Space | Note | Ref |
|[Leetcode-309. Best Time To Buy And Sell Stock With Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)|[c++](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.cpp), [python](./leetcode/309.best-time-to-buy-and-sell-stock-with-cooldown.py)| O\(N\)| O\(N\)|\-| - |
|[Leetcode-345. Reverse Vowels Of A String](https://leetcode.com/problems/reverse-vowels-of-a-string/description/)|[c++](./leetcode/345.reverse-vowels-of-a-string.cpp), [python](./leetcode/345.reverse-vowels-of-a-string.py)|\-|\-|\-| - |
* Given a non negative integer number num. For every numbers i in the range 0
15
-
* ≤ i ≤ num calculate the number of 1's in their binary representation and
16
-
* return them as an array.
17
-
*
18
-
* Example 1:
19
-
*
20
-
*
21
-
* Input: 2
22
-
* Output: [0,1,1]
23
-
*
24
-
* Example 2:
25
-
*
26
-
*
27
-
* Input: 5
28
-
* Output: [0,1,1,2,1,2]
29
-
*
30
-
*
31
-
* Follow up:
32
-
*
33
-
*
34
-
* It is very easy to come up with a solution with run time
35
-
* O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a
36
-
* single pass?
37
-
* Space complexity should be O(n).
38
-
* Can you do it like a boss? Do it without using any builtin function like
39
-
* __builtin_popcount in c++ or in any other language.
40
-
*
41
-
*/
1
+
// Tag: Dynamic Programming, Bit Manipulation
2
+
// Time: O(N)
3
+
// Space: O(1)
4
+
// Ref: -
5
+
// Note: -
6
+
7
+
// 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.
8
+
//
9
+
// Example 1:
10
+
//
11
+
// Input: n = 2
12
+
// Output: [0,1,1]
13
+
// Explanation:
14
+
// 0 --> 0
15
+
// 1 --> 1
16
+
// 2 --> 10
17
+
//
18
+
// Example 2:
19
+
//
20
+
// Input: n = 5
21
+
// Output: [0,1,1,2,1,2]
22
+
// Explanation:
23
+
// 0 --> 0
24
+
// 1 --> 1
25
+
// 2 --> 10
26
+
// 3 --> 11
27
+
// 4 --> 100
28
+
// 5 --> 101
29
+
//
30
+
//
31
+
// Constraints:
32
+
//
33
+
// 0 <= n <= 105
34
+
//
35
+
//
36
+
// Follow up:
37
+
//
38
+
// 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?
39
+
// Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
# Given a non negative integer number num. For every numbers i in the range 0 ≤
15
-
# i ≤ num calculate the number of 1's in their binary representation and return
16
-
# them as an array.
17
-
#
18
-
# Example 1:
19
-
#
20
-
#
21
-
# Input: 2
22
-
# Output: [0,1,1]
23
-
#
24
-
# Example 2:
25
-
#
26
-
#
27
-
# Input: 5
28
-
# Output: [0,1,1,2,1,2]
29
-
#
30
-
#
31
-
# Follow up:
32
-
#
33
-
#
34
-
# It is very easy to come up with a solution with run time
35
-
# O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a
36
-
# single pass?
37
-
# Space complexity should be O(n).
38
-
# Can you do it like a boss? Do it without using any builtin function like
39
-
# __builtin_popcount in c++ or in any other language.
40
-
#
41
-
#
42
-
classSolution(object):
43
-
defcountBits(self, num):
44
-
"""
45
-
:type num: int
46
-
:rtype: List[int]
47
-
"""
48
-
49
-
res= [0foriinrange(num+1)]
1
+
# Tag: Dynamic Programming, Bit Manipulation
2
+
# Time: O(N)
3
+
# Space: O(1)
4
+
# Ref: -
5
+
# Note: -
50
6
51
-
foriinrange(1, num+1):
52
-
res[i] =res[i& (i-1)] +1
7
+
# 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.
8
+
#
9
+
# Example 1:
10
+
#
11
+
# Input: n = 2
12
+
# Output: [0,1,1]
13
+
# Explanation:
14
+
# 0 --> 0
15
+
# 1 --> 1
16
+
# 2 --> 10
17
+
#
18
+
# Example 2:
19
+
#
20
+
# Input: n = 5
21
+
# Output: [0,1,1,2,1,2]
22
+
# Explanation:
23
+
# 0 --> 0
24
+
# 1 --> 1
25
+
# 2 --> 10
26
+
# 3 --> 11
27
+
# 4 --> 100
28
+
# 5 --> 101
29
+
#
30
+
#
31
+
# Constraints:
32
+
#
33
+
# 0 <= n <= 105
34
+
#
35
+
#
36
+
# Follow up:
37
+
#
38
+
# 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?
39
+
# Can you do it without using any built-in function (i.e., like __builtin_popcount in C++)?
0 commit comments