Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 17c2822

Browse files
feat: add solutions to lc problems: No.2859~2861 (#1644)
* No.2859.Sum of Values at Indices With K Set Bits * No.2860.Happy Students * No.2861.Maximum Number of Alloys
1 parent 57da030 commit 17c2822

File tree

19 files changed

+680
-24
lines changed

19 files changed

+680
-24
lines changed

‎solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README.md‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,24 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:模拟**
62+
63+
我们直接遍历每个下标 $i,ドル判断其二进制表示中 1ドル$ 的个数是否等于 $k,ドル如果等于则将其对应的元素累加到答案 $ans$ 中。
64+
65+
遍历结束后,返回答案即可。
66+
67+
时间复杂度 $O(n \times \log n),ドル其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
68+
6169
<!-- tabs:start -->
6270

6371
### **Python3**
6472

6573
<!-- 这里可写当前语言的特殊实现逻辑 -->
6674

6775
```python
68-
76+
class Solution:
77+
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
78+
return sum(x for i, x in enumerate(nums) if i.bit_count() == k)
6979
```
7080

7181
### **Java**
@@ -89,13 +99,54 @@ class Solution {
8999
### **C++**
90100

91101
```cpp
92-
102+
class Solution {
103+
public:
104+
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
105+
int ans = 0;
106+
for (int i = 0; i < nums.size(); ++i) {
107+
if (__builtin_popcount(i) == k) {
108+
ans += nums[i];
109+
}
110+
}
111+
return ans;
112+
}
113+
};
93114
```
94115
95116
### **Go**
96117
97118
```go
119+
func sumIndicesWithKSetBits(nums []int, k int) (ans int) {
120+
for i, x := range nums {
121+
if bits.OnesCount(uint(i)) == k {
122+
ans += x
123+
}
124+
}
125+
return
126+
}
127+
```
128+
129+
### **TypeScript**
130+
131+
```ts
132+
function sumIndicesWithKSetBits(nums: number[], k: number): number {
133+
let ans = 0;
134+
for (let i = 0; i < nums.length; ++i) {
135+
if (bitCount(i) === k) {
136+
ans += nums[i];
137+
}
138+
}
139+
return ans;
140+
}
98141

142+
function bitCount(n: number): number {
143+
let count = 0;
144+
while (n) {
145+
n &= n - 1;
146+
count++;
147+
}
148+
return count;
149+
}
99150
```
100151

101152
### **...**

‎solution/2800-2899/2859.Sum of Values at Indices With K Set Bits/README_EN.md‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ Hence, the answer is nums[3] = 1.
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
64+
return sum(x for i, x in enumerate(nums) if i.bit_count() == k)
6365
```
6466

6567
### **Java**
@@ -81,13 +83,54 @@ class Solution {
8183
### **C++**
8284

8385
```cpp
84-
86+
class Solution {
87+
public:
88+
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
89+
int ans = 0;
90+
for (int i = 0; i < nums.size(); ++i) {
91+
if (__builtin_popcount(i) == k) {
92+
ans += nums[i];
93+
}
94+
}
95+
return ans;
96+
}
97+
};
8598
```
8699
87100
### **Go**
88101
89102
```go
103+
func sumIndicesWithKSetBits(nums []int, k int) (ans int) {
104+
for i, x := range nums {
105+
if bits.OnesCount(uint(i)) == k {
106+
ans += x
107+
}
108+
}
109+
return
110+
}
111+
```
112+
113+
### **TypeScript**
114+
115+
```ts
116+
function sumIndicesWithKSetBits(nums: number[], k: number): number {
117+
let ans = 0;
118+
for (let i = 0; i < nums.length; ++i) {
119+
if (bitCount(i) === k) {
120+
ans += nums[i];
121+
}
122+
}
123+
return ans;
124+
}
90125

126+
function bitCount(n: number): number {
127+
let count = 0;
128+
while (n) {
129+
n &= n - 1;
130+
count++;
131+
}
132+
return count;
133+
}
91134
```
92135

93136
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
4+
int ans = 0;
5+
for (int i = 0; i < nums.size(); ++i) {
6+
if (__builtin_popcount(i) == k) {
7+
ans += nums[i];
8+
}
9+
}
10+
return ans;
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func sumIndicesWithKSetBits(nums []int, k int) (ans int) {
2+
for i, x := range nums {
3+
if bits.OnesCount(uint(i)) == k {
4+
ans += x
5+
}
6+
}
7+
return
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
3+
return sum(x for i, x in enumerate(nums) if i.bit_count() == k)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function sumIndicesWithKSetBits(nums: number[], k: number): number {
2+
let ans = 0;
3+
for (let i = 0; i < nums.length; ++i) {
4+
if (bitCount(i) === k) {
5+
ans += nums[i];
6+
}
7+
}
8+
return ans;
9+
}
10+
11+
function bitCount(n: number): number {
12+
let count = 0;
13+
while (n) {
14+
n &= n - 1;
15+
count++;
16+
}
17+
return count;
18+
}

‎solution/2800-2899/2860.Happy Students/README.md‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,40 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:排序 + 枚举**
60+
61+
假设选出了 $k$ 个学生,那么以下情况成立:
62+
63+
- 如果 $nums[i] = k,ドル那么不存在分组方法;
64+
- 如果 $nums[i] \gt k,ドル那么学生 $i$ 不被选中;
65+
- 如果 $nums[i] \lt k,ドル那么学生 $i$ 被选中。
66+
67+
因此,被选中的学生一定是排序后的 $nums$ 数组中的前 $k$ 个元素。
68+
69+
我们在 $[0,..n]$ 范围内枚举 $k,ドル对于当前选出的学生人数 $i,ドル我们可以得到组内最大的学生编号 $i-1,ドル数字为 $nums[i-1]$。如果 $i \gt 0$ 并且 $nums[i-1] \ge i,ドル那么不存在分组方法;如果 $i \lt n$ 并且 $nums[i] \le i,ドル那么不存在分组方法。否则,存在分组方法,答案加一。
70+
71+
枚举结束后,返回答案即可。
72+
73+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。
74+
5975
<!-- tabs:start -->
6076

6177
### **Python3**
6278

6379
<!-- 这里可写当前语言的特殊实现逻辑 -->
6480

6581
```python
66-
82+
class Solution:
83+
def countWays(self, nums: List[int]) -> int:
84+
nums.sort()
85+
n = len(nums)
86+
ans = 0
87+
for i in range(n + 1):
88+
if i and nums[i - 1] >= i:
89+
continue
90+
if i < n and nums[i] <= i:
91+
continue
92+
return ans
6793
```
6894

6995
### **Java**
@@ -89,13 +115,54 @@ class Solution {
89115
### **C++**
90116

91117
```cpp
92-
118+
class Solution {
119+
public:
120+
int countWays(vector<int>& nums) {
121+
sort(nums.begin(), nums.end());
122+
int ans = 0;
123+
int n = nums.size();
124+
for (int i = 0; i <= n; ++i) {
125+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
126+
continue;
127+
}
128+
++ans;
129+
}
130+
return ans;
131+
}
132+
};
93133
```
94134
95135
### **Go**
96136
97137
```go
138+
func countWays(nums []int) (ans int) {
139+
sort.Ints(nums)
140+
n := len(nums)
141+
for i := 0; i <= n; i++ {
142+
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
143+
continue
144+
}
145+
ans++
146+
}
147+
return
148+
}
149+
```
150+
151+
### **TypeScript**
98152

153+
```ts
154+
function countWays(nums: number[]): number {
155+
nums.sort((a, b) => a - b);
156+
let ans = 0;
157+
const n = nums.length;
158+
for (let i = 0; i <= n; ++i) {
159+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
160+
continue;
161+
}
162+
++ans;
163+
}
164+
return ans;
165+
}
99166
```
100167

101168
### **...**

‎solution/2800-2899/2860.Happy Students/README_EN.md‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,17 @@ The class teacher selects all the students to form the group.
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def countWays(self, nums: List[int]) -> int:
60+
nums.sort()
61+
n = len(nums)
62+
ans = 0
63+
for i in range(n + 1):
64+
if i and nums[i - 1] >= i:
65+
continue
66+
if i < n and nums[i] <= i:
67+
continue
68+
return ans
5969
```
6070

6171
### **Java**
@@ -79,13 +89,54 @@ class Solution {
7989
### **C++**
8090

8191
```cpp
82-
92+
class Solution {
93+
public:
94+
int countWays(vector<int>& nums) {
95+
sort(nums.begin(), nums.end());
96+
int ans = 0;
97+
int n = nums.size();
98+
for (int i = 0; i <= n; ++i) {
99+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
100+
continue;
101+
}
102+
++ans;
103+
}
104+
return ans;
105+
}
106+
};
83107
```
84108
85109
### **Go**
86110
87111
```go
112+
func countWays(nums []int) (ans int) {
113+
sort.Ints(nums)
114+
n := len(nums)
115+
for i := 0; i <= n; i++ {
116+
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
117+
continue
118+
}
119+
ans++
120+
}
121+
return
122+
}
123+
```
124+
125+
### **TypeScript**
88126

127+
```ts
128+
function countWays(nums: number[]): number {
129+
nums.sort((a, b) => a - b);
130+
let ans = 0;
131+
const n = nums.length;
132+
for (let i = 0; i <= n; ++i) {
133+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
134+
continue;
135+
}
136+
++ans;
137+
}
138+
return ans;
139+
}
89140
```
90141

91142
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int countWays(vector<int>& nums) {
4+
sort(nums.begin(), nums.end());
5+
int ans = 0;
6+
int n = nums.size();
7+
for (int i = 0; i <= n; ++i) {
8+
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
9+
continue;
10+
}
11+
++ans;
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func countWays(nums []int) (ans int) {
2+
sort.Ints(nums)
3+
n := len(nums)
4+
for i := 0; i <= n; i++ {
5+
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
6+
continue
7+
}
8+
ans++
9+
}
10+
return
11+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /