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 91b76bf

Browse files
authored
feat: add Java solutions to lc problems: No.2859~2862 (#1641)
1 parent 374482e commit 91b76bf

File tree

12 files changed

+273
-6
lines changed

12 files changed

+273
-6
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,17 @@
7373
<!-- 这里可写当前语言的特殊实现逻辑 -->
7474

7575
```java
76-
76+
class Solution {
77+
public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
78+
int ans = 0;
79+
for (int i = 0; i < nums.size(); i++) {
80+
if (Integer.bitCount(i) == k) {
81+
ans += nums.get(i);
82+
}
83+
}
84+
return ans;
85+
}
86+
}
7787
```
7888

7989
### **C++**

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,17 @@ Hence, the answer is nums[3] = 1.
6565
### **Java**
6666

6767
```java
68-
68+
class Solution {
69+
public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
70+
int ans = 0;
71+
for (int i = 0; i < nums.size(); i++) {
72+
if (Integer.bitCount(i) == k) {
73+
ans += nums.get(i);
74+
}
75+
}
76+
return ans;
77+
}
78+
}
6979
```
7080

7181
### **C++**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
3+
int ans = 0;
4+
for (int i = 0; i < nums.size(); i++) {
5+
if (Integer.bitCount(i) == k) {
6+
ans += nums.get(i);
7+
}
8+
}
9+
return ans;
10+
}
11+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,19 @@
7171
<!-- 这里可写当前语言的特殊实现逻辑 -->
7272

7373
```java
74-
74+
class Solution {
75+
public int countWays(List<Integer> nums) {
76+
Collections.sort(nums);
77+
int n = nums.size();
78+
int ans = 0;
79+
for (int i = 0; i <= n; i++) {
80+
if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) {
81+
ans++;
82+
}
83+
}
84+
return ans;
85+
}
86+
}
7587
```
7688

7789
### **C++**

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ The class teacher selects all the students to form the group.
6161
### **Java**
6262

6363
```java
64-
64+
class Solution {
65+
public int countWays(List<Integer> nums) {
66+
Collections.sort(nums);
67+
int n = nums.size();
68+
int ans = 0;
69+
for (int i = 0; i <= n; i++) {
70+
if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) {
71+
ans++;
72+
}
73+
}
74+
return ans;
75+
}
76+
}
6577
```
6678

6779
### **C++**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int countWays(List<Integer> nums) {
3+
Collections.sort(nums);
4+
int n = nums.size();
5+
int ans = 0;
6+
for (int i = 0; i <= n; i++) {
7+
if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) {
8+
ans++;
9+
}
10+
}
11+
return ans;
12+
}
13+
}

‎solution/2800-2899/2861.Maximum Number of Alloys/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,50 @@
9494
<!-- 这里可写当前语言的特殊实现逻辑 -->
9595

9696
```java
97-
97+
class Solution {
98+
int n;
99+
int k;
100+
int budget;
101+
List<List<Integer>> composition;
102+
List<Integer> stock;
103+
List<Integer> cost;
104+
105+
boolean isValid(long target) {
106+
for (int i = 0; i < k; i++) {
107+
long remain = budget;
108+
List<Integer> currMachine = composition.get(i);
109+
for (int j = 0; j < n && remain >= 0; j++) {
110+
long need = Math.max(0, currMachine.get(j) * target - stock.get(j));
111+
remain -= need * cost.get(j);
112+
}
113+
if (remain >= 0) {
114+
return true;
115+
}
116+
}
117+
return false;
118+
}
119+
120+
public int maxNumberOfAlloys(int n, int k, int budget, List<List<Integer>> composition,
121+
List<Integer> stock, List<Integer> cost) {
122+
this.n = n;
123+
this.k = k;
124+
this.budget = budget;
125+
this.composition = composition;
126+
this.stock = stock;
127+
this.cost = cost;
128+
int l = -1;
129+
int r = budget / cost.get(0) + stock.get(0);
130+
while (l < r) {
131+
int mid = (l + r + 1) >> 1;
132+
if (isValid(mid)) {
133+
l = mid;
134+
} else {
135+
r = mid - 1;
136+
}
137+
}
138+
return l;
139+
}
140+
}
98141
```
99142

100143
### **C++**

‎solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,50 @@ It can be proven that we can create at most 2 alloys.
8484
### **Java**
8585

8686
```java
87-
87+
class Solution {
88+
int n;
89+
int k;
90+
int budget;
91+
List<List<Integer>> composition;
92+
List<Integer> stock;
93+
List<Integer> cost;
94+
95+
boolean isValid(long target) {
96+
for (int i = 0; i < k; i++) {
97+
long remain = budget;
98+
List<Integer> currMachine = composition.get(i);
99+
for (int j = 0; j < n && remain >= 0; j++) {
100+
long need = Math.max(0, currMachine.get(j) * target - stock.get(j));
101+
remain -= need * cost.get(j);
102+
}
103+
if (remain >= 0) {
104+
return true;
105+
}
106+
}
107+
return false;
108+
}
109+
110+
public int maxNumberOfAlloys(int n, int k, int budget, List<List<Integer>> composition,
111+
List<Integer> stock, List<Integer> cost) {
112+
this.n = n;
113+
this.k = k;
114+
this.budget = budget;
115+
this.composition = composition;
116+
this.stock = stock;
117+
this.cost = cost;
118+
int l = -1;
119+
int r = budget / cost.get(0) + stock.get(0);
120+
while (l < r) {
121+
int mid = (l + r + 1) >> 1;
122+
if (isValid(mid)) {
123+
l = mid;
124+
} else {
125+
r = mid - 1;
126+
}
127+
}
128+
return l;
129+
}
130+
}
88131
```
89132

90133
### **C++**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
int n;
3+
int k;
4+
int budget;
5+
List<List<Integer>> composition;
6+
List<Integer> stock;
7+
List<Integer> cost;
8+
9+
boolean isValid(long target) {
10+
for (int i = 0; i < k; i++) {
11+
long remain = budget;
12+
List<Integer> currMachine = composition.get(i);
13+
for (int j = 0; j < n && remain >= 0; j++) {
14+
long need = Math.max(0, currMachine.get(j) * target - stock.get(j));
15+
remain -= need * cost.get(j);
16+
}
17+
if (remain >= 0) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
24+
public int maxNumberOfAlloys(int n, int k, int budget, List<List<Integer>> composition,
25+
List<Integer> stock, List<Integer> cost) {
26+
this.n = n;
27+
this.k = k;
28+
this.budget = budget;
29+
this.composition = composition;
30+
this.stock = stock;
31+
this.cost = cost;
32+
int l = -1;
33+
int r = budget / cost.get(0) + stock.get(0);
34+
while (l < r) {
35+
int mid = (l + r + 1) >> 1;
36+
if (isValid(mid)) {
37+
l = mid;
38+
} else {
39+
r = mid - 1;
40+
}
41+
}
42+
return l;
43+
}
44+
}

‎solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@
7171
<!-- 这里可写当前语言的特殊实现逻辑 -->
7272

7373
```java
74+
class Solution {
75+
public long maximumSum(List<Integer> nums) {
76+
long ans = 0;
77+
int n = nums.size();
78+
boolean[] used = new boolean[n + 1];
79+
int bound = (int) Math.floor(Math.sqrt(n));
80+
int[] squares = new int[bound + 1];
81+
for (int i = 1; i <= bound + 1; i++) {
82+
squares[i - 1] = i * i;
83+
}
84+
for (int i = 1; i <= n; i++) {
85+
long res = 0;
86+
int idx = 0;
87+
int curr = i * squares[idx];
88+
while (curr <= n) {
89+
res += nums.get(curr - 1);
90+
curr = i * squares[++idx];
91+
}
92+
ans = Math.max(ans, res);
93+
}
94+
return ans;
95+
}
96+
}
7497

7598
```
7699

0 commit comments

Comments
(0)

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