Skip to content

feat: add Java solutions to lc problem(s): No.2859~2862 #1641

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

Merged
merged 16 commits into from
Sep 17, 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 @@ -73,7 +73,17 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
int ans = 0;
for (int i = 0; i < nums.size(); i++) {
if (Integer.bitCount(i) == k) {
ans += nums.get(i);
}
}
return ans;
}
}
```

### **C++**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,17 @@ Hence, the answer is nums[3] = 1.
### **Java**

```java

class Solution {
public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
int ans = 0;
for (int i = 0; i < nums.size(); i++) {
if (Integer.bitCount(i) == k) {
ans += nums.get(i);
}
}
return ans;
}
}
```

### **C++**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
int ans = 0;
for (int i = 0; i < nums.size(); i++) {
if (Integer.bitCount(i) == k) {
ans += nums.get(i);
}
}
return ans;
}
}
14 changes: 13 additions & 1 deletion solution/2800-2899/2860.Happy Students/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,19 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int countWays(List<Integer> nums) {
Collections.sort(nums);
int n = nums.size();
int ans = 0;
for (int i = 0; i <= n; i++) {
if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) {
ans++;
}
}
return ans;
}
}
```

### **C++**
Expand Down
14 changes: 13 additions & 1 deletion solution/2800-2899/2860.Happy Students/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,19 @@ The class teacher selects all the students to form the group.
### **Java**

```java

class Solution {
public int countWays(List<Integer> nums) {
Collections.sort(nums);
int n = nums.size();
int ans = 0;
for (int i = 0; i <= n; i++) {
if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) {
ans++;
}
}
return ans;
}
}
```

### **C++**
Expand Down
13 changes: 13 additions & 0 deletions solution/2800-2899/2860.Happy Students/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int countWays(List<Integer> nums) {
Collections.sort(nums);
int n = nums.size();
int ans = 0;
for (int i = 0; i <= n; i++) {
if ((i == 0 || nums.get(i - 1) < i) && (i == n || nums.get(i) > i)) {
ans++;
}
}
return ans;
}
}
45 changes: 44 additions & 1 deletion solution/2800-2899/2861.Maximum Number of Alloys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,50 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
int n;
int k;
int budget;
List<List<Integer>> composition;
List<Integer> stock;
List<Integer> cost;

boolean isValid(long target) {
for (int i = 0; i < k; i++) {
long remain = budget;
List<Integer> currMachine = composition.get(i);
for (int j = 0; j < n && remain >= 0; j++) {
long need = Math.max(0, currMachine.get(j) * target - stock.get(j));
remain -= need * cost.get(j);
}
if (remain >= 0) {
return true;
}
}
return false;
}

public int maxNumberOfAlloys(int n, int k, int budget, List<List<Integer>> composition,
List<Integer> stock, List<Integer> cost) {
this.n = n;
this.k = k;
this.budget = budget;
this.composition = composition;
this.stock = stock;
this.cost = cost;
int l = -1;
int r = budget / cost.get(0) + stock.get(0);
while (l < r) {
int mid = (l + r + 1) >> 1;
if (isValid(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
}
```

### **C++**
Expand Down
45 changes: 44 additions & 1 deletion solution/2800-2899/2861.Maximum Number of Alloys/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,50 @@ It can be proven that we can create at most 2 alloys.
### **Java**

```java

class Solution {
int n;
int k;
int budget;
List<List<Integer>> composition;
List<Integer> stock;
List<Integer> cost;

boolean isValid(long target) {
for (int i = 0; i < k; i++) {
long remain = budget;
List<Integer> currMachine = composition.get(i);
for (int j = 0; j < n && remain >= 0; j++) {
long need = Math.max(0, currMachine.get(j) * target - stock.get(j));
remain -= need * cost.get(j);
}
if (remain >= 0) {
return true;
}
}
return false;
}

public int maxNumberOfAlloys(int n, int k, int budget, List<List<Integer>> composition,
List<Integer> stock, List<Integer> cost) {
this.n = n;
this.k = k;
this.budget = budget;
this.composition = composition;
this.stock = stock;
this.cost = cost;
int l = -1;
int r = budget / cost.get(0) + stock.get(0);
while (l < r) {
int mid = (l + r + 1) >> 1;
if (isValid(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
}
```

### **C++**
Expand Down
44 changes: 44 additions & 0 deletions solution/2800-2899/2861.Maximum Number of Alloys/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
int n;
int k;
int budget;
List<List<Integer>> composition;
List<Integer> stock;
List<Integer> cost;

boolean isValid(long target) {
for (int i = 0; i < k; i++) {
long remain = budget;
List<Integer> currMachine = composition.get(i);
for (int j = 0; j < n && remain >= 0; j++) {
long need = Math.max(0, currMachine.get(j) * target - stock.get(j));
remain -= need * cost.get(j);
}
if (remain >= 0) {
return true;
}
}
return false;
}

public int maxNumberOfAlloys(int n, int k, int budget, List<List<Integer>> composition,
List<Integer> stock, List<Integer> cost) {
this.n = n;
this.k = k;
this.budget = budget;
this.composition = composition;
this.stock = stock;
this.cost = cost;
int l = -1;
int r = budget / cost.get(0) + stock.get(0);
while (l < r) {
int mid = (l + r + 1) >> 1;
if (isValid(mid)) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public long maximumSum(List<Integer> nums) {
long ans = 0;
int n = nums.size();
boolean[] used = new boolean[n + 1];
int bound = (int) Math.floor(Math.sqrt(n));
int[] squares = new int[bound + 1];
for (int i = 1; i <= bound + 1; i++) {
squares[i - 1] = i * i;
}
for (int i = 1; i <= n; i++) {
long res = 0;
int idx = 0;
int curr = i * squares[idx];
while (curr <= n) {
res += nums.get(curr - 1);
curr = i * squares[++idx];
}
ans = Math.max(ans, res);
}
return ans;
}
}

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ Hence, the maximum element-sum of a complete subset of indices is 19.
### **Java**

```java
class Solution {
public long maximumSum(List<Integer> nums) {
long ans = 0;
int n = nums.size();
boolean[] used = new boolean[n + 1];
int bound = (int) Math.floor(Math.sqrt(n));
int[] squares = new int[bound + 1];
for (int i = 1; i <= bound + 1; i++) {
squares[i - 1] = i * i;
}
for (int i = 1; i <= n; i++) {
long res = 0;
int idx = 0;
int curr = i * squares[idx];
while (curr <= n) {
res += nums.get(curr - 1);
curr = i * squares[++idx];
}
ans = Math.max(ans, res);
}
return ans;
}
}

```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public long maximumSum(List<Integer> nums) {
long ans = 0;
int n = nums.size();
boolean[] used = new boolean[n + 1];
int bound = (int) Math.floor(Math.sqrt(n));
int[] squares = new int[bound + 1];
for (int i = 1; i <= bound + 1; i++) {
squares[i - 1] = i * i;
}
for (int i = 1; i <= n; i++) {
long res = 0;
int idx = 0;
int curr = i * squares[idx];
while (curr <= n) {
res += nums.get(curr - 1);
curr = i * squares[++idx];
}
ans = Math.max(ans, res);
}
return ans;
}
}