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 a9b1719

Browse files
feat: add solutions to lc problem: No.2862 (#1645)
No.2862.Maximum Element-Sum of a Complete Subset of Indices
1 parent 17c2822 commit a9b1719

File tree

7 files changed

+226
-18
lines changed

7 files changed

+226
-18
lines changed

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

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

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

59+
**方法一:枚举**
60+
61+
我们注意到,如果一个数字可以表示成 $k \times j^2$ 的形式,那么所有该形式的数字的 $k$ 是相同的。
62+
63+
因此,我们可以在 $[1,..n]$ 范围内枚举 $k,ドル然后从 1ドル$ 开始枚举 $j,ドル每一次累加 $nums[k \times j^2 - 1]$ 的值到 $t$ 中,直到 $k \times j^2 > n$。此时更新答案为 $ans = \max(ans, t)$。
64+
65+
最后返回答案 $ans$ 即可。
66+
67+
时间复杂度 $O(n),ドル其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
68+
5969
<!-- tabs:start -->
6070

6171
### **Python3**
6272

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

6575
```python
66-
76+
class Solution:
77+
def maximumSum(self, nums: List[int]) -> int:
78+
n = len(nums)
79+
ans = 0
80+
for k in range(1, n + 1):
81+
t = 0
82+
j = 1
83+
while k * j * j <= n:
84+
t += nums[k * j * j - 1]
85+
j += 1
86+
ans = max(ans, t)
87+
return ans
6788
```
6889

6990
### **Java**
@@ -94,19 +115,83 @@ class Solution {
94115
return ans;
95116
}
96117
}
118+
```
97119

120+
```java
121+
class Solution {
122+
public long maximumSum(List<Integer> nums) {
123+
long ans = 0;
124+
int n = nums.size();
125+
for (int k = 1; k <= n; ++k) {
126+
long t = 0;
127+
for (int j = 1; k * j * j <= n; ++j) {
128+
t += nums.get(k * j * j - 1);
129+
}
130+
ans = Math.max(ans, t);
131+
}
132+
return ans;
133+
}
134+
}
98135
```
99136

100137
### **C++**
101138

102139
```cpp
103-
140+
class Solution {
141+
public:
142+
long long maximumSum(vector<int>& nums) {
143+
long long ans = 0;
144+
int n = nums.size();
145+
for (int k = 1; k <= n; ++k) {
146+
long long t = 0;
147+
for (int j = 1; k * j * j <= n; ++j) {
148+
t += nums[k * j * j - 1];
149+
}
150+
ans = max(ans, t);
151+
}
152+
return ans;
153+
}
154+
};
104155
```
105156
106157
### **Go**
107158
108159
```go
160+
func maximumSum(nums []int) (ans int64) {
161+
n := len(nums)
162+
for k := 1; k <= n; k++ {
163+
var t int64
164+
for j := 1; k*j*j <= n; j++ {
165+
t += int64(nums[k*j*j-1])
166+
}
167+
ans = max(ans, t)
168+
}
169+
return
170+
}
171+
172+
func max(a, b int64) int64 {
173+
if a > b {
174+
return a
175+
}
176+
return b
177+
}
178+
```
109179

180+
### **TypeScript**
181+
182+
```ts
183+
function maximumSum(nums: number[]): number {
184+
let ans = 0;
185+
const n = nums.length;
186+
for (let k = 1; k <= n; ++k) {
187+
let t = 0;
188+
for (let j = 1; k * j * j <= n; ++j) {
189+
t += nums[k * j * j - 1];
190+
}
191+
ans = Math.max(ans, t);
192+
}
193+
return ans;
194+
}
110195
```
111196

112197
### **...**

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

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ Hence, the maximum element-sum of a complete subset of indices is 19.
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def maximumSum(self, nums: List[int]) -> int:
60+
n = len(nums)
61+
ans = 0
62+
for k in range(1, n + 1):
63+
t = 0
64+
j = 1
65+
while k * j * j <= n:
66+
t += nums[k * j * j - 1]
67+
j += 1
68+
ans = max(ans, t)
69+
return ans
5970
```
6071

6172
### **Java**
@@ -84,19 +95,83 @@ class Solution {
8495
return ans;
8596
}
8697
}
98+
```
8799

100+
```java
101+
class Solution {
102+
public long maximumSum(List<Integer> nums) {
103+
long ans = 0;
104+
int n = nums.size();
105+
for (int k = 1; k <= n; ++k) {
106+
long t = 0;
107+
for (int j = 1; k * j * j <= n; ++j) {
108+
t += nums.get(k * j * j - 1);
109+
}
110+
ans = Math.max(ans, t);
111+
}
112+
return ans;
113+
}
114+
}
88115
```
89116

90117
### **C++**
91118

92119
```cpp
93-
120+
class Solution {
121+
public:
122+
long long maximumSum(vector<int>& nums) {
123+
long long ans = 0;
124+
int n = nums.size();
125+
for (int k = 1; k <= n; ++k) {
126+
long long t = 0;
127+
for (int j = 1; k * j * j <= n; ++j) {
128+
t += nums[k * j * j - 1];
129+
}
130+
ans = max(ans, t);
131+
}
132+
return ans;
133+
}
134+
};
94135
```
95136
96137
### **Go**
97138
98139
```go
140+
func maximumSum(nums []int) (ans int64) {
141+
n := len(nums)
142+
for k := 1; k <= n; k++ {
143+
var t int64
144+
for j := 1; k*j*j <= n; j++ {
145+
t += int64(nums[k*j*j-1])
146+
}
147+
ans = max(ans, t)
148+
}
149+
return
150+
}
99151
152+
func max(a, b int64) int64 {
153+
if a > b {
154+
return a
155+
}
156+
return b
157+
}
158+
```
159+
160+
### **TypeScript**
161+
162+
```ts
163+
function maximumSum(nums: number[]): number {
164+
let ans = 0;
165+
const n = nums.length;
166+
for (let k = 1; k <= n; ++k) {
167+
let t = 0;
168+
for (let j = 1; k * j * j <= n; ++j) {
169+
t += nums[k * j * j - 1];
170+
}
171+
ans = Math.max(ans, t);
172+
}
173+
return ans;
174+
}
100175
```
101176

102177
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
long long maximumSum(vector<int>& nums) {
4+
long long ans = 0;
5+
int n = nums.size();
6+
for (int k = 1; k <= n; ++k) {
7+
long long t = 0;
8+
for (int j = 1; k * j * j <= n; ++j) {
9+
t += nums[k * j * j - 1];
10+
}
11+
ans = max(ans, t);
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func maximumSum(nums []int) (ans int64) {
2+
n := len(nums)
3+
for k := 1; k <= n; k++ {
4+
var t int64
5+
for j := 1; k*j*j <= n; j++ {
6+
t += int64(nums[k*j*j-1])
7+
}
8+
ans = max(ans, t)
9+
}
10+
return
11+
}
12+
13+
func max(a, b int64) int64 {
14+
if a > b {
15+
return a
16+
}
17+
return b
18+
}

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@ class Solution {
22
public long maximumSum(List<Integer> nums) {
33
long ans = 0;
44
int n = nums.size();
5-
boolean[] used = new boolean[n + 1];
6-
int bound = (int) Math.floor(Math.sqrt(n));
7-
int[] squares = new int[bound + 1];
8-
for (int i = 1; i <= bound + 1; i++) {
9-
squares[i - 1] = i * i;
10-
}
11-
for (int i = 1; i <= n; i++) {
12-
long res = 0;
13-
int idx = 0;
14-
int curr = i * squares[idx];
15-
while (curr <= n) {
16-
res += nums.get(curr - 1);
17-
curr = i * squares[++idx];
5+
for (int k = 1; k <= n; ++k) {
6+
long t = 0;
7+
for (int j = 1; k * j * j <= n; ++j) {
8+
t += nums.get(k * j * j - 1);
189
}
19-
ans = Math.max(ans, res);
10+
ans = Math.max(ans, t);
2011
}
2112
return ans;
2213
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maximumSum(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
ans = 0
5+
for k in range(1, n + 1):
6+
t = 0
7+
j = 1
8+
while k * j * j <= n:
9+
t += nums[k * j * j - 1]
10+
j += 1
11+
ans = max(ans, t)
12+
return ans
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function maximumSum(nums: number[]): number {
2+
let ans = 0;
3+
const n = nums.length;
4+
for (let k = 1; k <= n; ++k) {
5+
let t = 0;
6+
for (let j = 1; k * j * j <= n; ++j) {
7+
t += nums[k * j * j - 1];
8+
}
9+
ans = Math.max(ans, t);
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
(0)

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