Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.2859~2861 #1644

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,24 @@

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

**方法一:模拟**

我们直接遍历每个下标 $i$,判断其二进制表示中 $1$ 的个数是否等于 $k$,如果等于则将其对应的元素累加到答案 $ans$ 中。

遍历结束后,返回答案即可。

时间复杂度 $O(n \times \log n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
return sum(x for i, x in enumerate(nums) if i.bit_count() == k)
```

### **Java**
Expand All @@ -89,13 +99,54 @@ class Solution {
### **C++**

```cpp

class Solution {
public:
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
int ans = 0;
for (int i = 0; i < nums.size(); ++i) {
if (__builtin_popcount(i) == k) {
ans += nums[i];
}
}
return ans;
}
};
```

### **Go**

```go
func sumIndicesWithKSetBits(nums []int, k int) (ans int) {
for i, x := range nums {
if bits.OnesCount(uint(i)) == k {
ans += x
}
}
return
}
```

### **TypeScript**

```ts
function sumIndicesWithKSetBits(nums: number[], k: number): number {
let ans = 0;
for (let i = 0; i < nums.length; ++i) {
if (bitCount(i) === k) {
ans += nums[i];
}
}
return ans;
}

function bitCount(n: number): number {
let count = 0;
while (n) {
n &= n - 1;
count++;
}
return count;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ Hence, the answer is nums[3] = 1.
### **Python3**

```python

class Solution:
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
return sum(x for i, x in enumerate(nums) if i.bit_count() == k)
```

### **Java**
Expand All @@ -81,13 +83,54 @@ class Solution {
### **C++**

```cpp

class Solution {
public:
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
int ans = 0;
for (int i = 0; i < nums.size(); ++i) {
if (__builtin_popcount(i) == k) {
ans += nums[i];
}
}
return ans;
}
};
```

### **Go**

```go
func sumIndicesWithKSetBits(nums []int, k int) (ans int) {
for i, x := range nums {
if bits.OnesCount(uint(i)) == k {
ans += x
}
}
return
}
```

### **TypeScript**

```ts
function sumIndicesWithKSetBits(nums: number[], k: number): number {
let ans = 0;
for (let i = 0; i < nums.length; ++i) {
if (bitCount(i) === k) {
ans += nums[i];
}
}
return ans;
}

function bitCount(n: number): number {
let count = 0;
while (n) {
n &= n - 1;
count++;
}
return count;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
int ans = 0;
for (int i = 0; i < nums.size(); ++i) {
if (__builtin_popcount(i) == k) {
ans += nums[i];
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func sumIndicesWithKSetBits(nums []int, k int) (ans int) {
for i, x := range nums {
if bits.OnesCount(uint(i)) == k {
ans += x
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
return sum(x for i, x in enumerate(nums) if i.bit_count() == k)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function sumIndicesWithKSetBits(nums: number[], k: number): number {
let ans = 0;
for (let i = 0; i < nums.length; ++i) {
if (bitCount(i) === k) {
ans += nums[i];
}
}
return ans;
}

function bitCount(n: number): number {
let count = 0;
while (n) {
n &= n - 1;
count++;
}
return count;
}
71 changes: 69 additions & 2 deletions solution/2800-2899/2860.Happy Students/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,40 @@

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

**方法一:排序 + 枚举**

假设选出了 $k$ 个学生,那么以下情况成立:

- 如果 $nums[i] = k$,那么不存在分组方法;
- 如果 $nums[i] \gt k$,那么学生 $i$ 不被选中;
- 如果 $nums[i] \lt k$,那么学生 $i$ 被选中。

因此,被选中的学生一定是排序后的 $nums$ 数组中的前 $k$ 个元素。

我们在 $[0,..n]$ 范围内枚举 $k$,对于当前选出的学生人数 $i$,我们可以得到组内最大的学生编号 $i-1$,数字为 $nums[i-1]$。如果 $i \gt 0$ 并且 $nums[i-1] \ge i$,那么不存在分组方法;如果 $i \lt n$ 并且 $nums[i] \le i$,那么不存在分组方法。否则,存在分组方法,答案加一。

枚举结束后,返回答案即可。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组长度。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def countWays(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
ans = 0
for i in range(n + 1):
if i and nums[i - 1] >= i:
continue
if i < n and nums[i] <= i:
continue
return ans
```

### **Java**
Expand All @@ -89,13 +115,54 @@ class Solution {
### **C++**

```cpp

class Solution {
public:
int countWays(vector<int>& nums) {
sort(nums.begin(), nums.end());
int ans = 0;
int n = nums.size();
for (int i = 0; i <= n; ++i) {
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
continue;
}
++ans;
}
return ans;
}
};
```

### **Go**

```go
func countWays(nums []int) (ans int) {
sort.Ints(nums)
n := len(nums)
for i := 0; i <= n; i++ {
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
continue
}
ans++
}
return
}
```

### **TypeScript**

```ts
function countWays(nums: number[]): number {
nums.sort((a, b) => a - b);
let ans = 0;
const n = nums.length;
for (let i = 0; i <= n; ++i) {
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
continue;
}
++ans;
}
return ans;
}
```

### **...**
Expand Down
55 changes: 53 additions & 2 deletions solution/2800-2899/2860.Happy Students/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ The class teacher selects all the students to form the group.
### **Python3**

```python

class Solution:
def countWays(self, nums: List[int]) -> int:
nums.sort()
n = len(nums)
ans = 0
for i in range(n + 1):
if i and nums[i - 1] >= i:
continue
if i < n and nums[i] <= i:
continue
return ans
```

### **Java**
Expand All @@ -79,13 +89,54 @@ class Solution {
### **C++**

```cpp

class Solution {
public:
int countWays(vector<int>& nums) {
sort(nums.begin(), nums.end());
int ans = 0;
int n = nums.size();
for (int i = 0; i <= n; ++i) {
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
continue;
}
++ans;
}
return ans;
}
};
```

### **Go**

```go
func countWays(nums []int) (ans int) {
sort.Ints(nums)
n := len(nums)
for i := 0; i <= n; i++ {
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
continue
}
ans++
}
return
}
```

### **TypeScript**

```ts
function countWays(nums: number[]): number {
nums.sort((a, b) => a - b);
let ans = 0;
const n = nums.length;
for (let i = 0; i <= n; ++i) {
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
continue;
}
++ans;
}
return ans;
}
```

### **...**
Expand Down
15 changes: 15 additions & 0 deletions solution/2800-2899/2860.Happy Students/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int countWays(vector<int>& nums) {
sort(nums.begin(), nums.end());
int ans = 0;
int n = nums.size();
for (int i = 0; i <= n; ++i) {
if ((i && nums[i - 1] >= i) || (i < n && nums[i] <= i)) {
continue;
}
++ans;
}
return ans;
}
};
11 changes: 11 additions & 0 deletions solution/2800-2899/2860.Happy Students/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func countWays(nums []int) (ans int) {
sort.Ints(nums)
n := len(nums)
for i := 0; i <= n; i++ {
if (i > 0 && nums[i-1] >= i) || (i < n && nums[i] <= i) {
continue
}
ans++
}
return
}
Loading