Skip to content

Commit 8e41e0d

Browse files
authored
feat: add solutions to lc problem: No.3512 (#4357)
No.3512.Minimum Operations to Make Array Sum Divisible by K
1 parent 6a670af commit 8e41e0d

File tree

7 files changed

+87
-8
lines changed

7 files changed

+87
-8
lines changed

solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,60 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Mi
8484

8585
<!-- solution:start -->
8686

87-
### 方法一
87+
### 方法一:求和取模
88+
89+
题目实际上是求数组元素之和对 $k$ 取模的结果。因此,我们只需要遍历数组,计算出所有元素之和,然后对 $k$ 取模,最后返回这个结果即可。
90+
91+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
8892

8993
<!-- tabs:start -->
9094

9195
#### Python3
9296

9397
```python
94-
98+
class Solution:
99+
def minOperations(self, nums: List[int], k: int) -> int:
100+
return sum(nums) % k
95101
```
96102

97103
#### Java
98104

99105
```java
100-
106+
class Solution {
107+
public int minOperations(int[] nums, int k) {
108+
return Arrays.stream(nums).sum() % k;
109+
}
110+
}
101111
```
102112

103113
#### C++
104114

105115
```cpp
106-
116+
class Solution {
117+
public:
118+
int minOperations(vector<int>& nums, int k) {
119+
return reduce(nums.begin(), nums.end(), 0) % k;
120+
}
121+
};
107122
```
108123
109124
#### Go
110125
111126
```go
127+
func minOperations(nums []int, k int) (ans int) {
128+
for _, x := range nums {
129+
ans = (ans + x) % k
130+
}
131+
return
132+
}
133+
```
134+
135+
#### TypeScript
112136

137+
```ts
138+
function minOperations(nums: number[], k: number): number {
139+
return nums.reduce((acc, x) => acc + x, 0) % k;
140+
}
113141
```
114142

115143
<!-- tabs:end -->

solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,60 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Mi
8282

8383
<!-- solution:start -->
8484

85-
### Solution 1
85+
### Solution 1: Sum and Modulo
86+
87+
The problem essentially asks for the result of the sum of the array elements modulo $k$. Therefore, we only need to iterate through the array, calculate the sum of all elements, and then take the modulo $k$. Finally, return this result.
88+
89+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
8690

8791
<!-- tabs:start -->
8892

8993
#### Python3
9094

9195
```python
92-
96+
class Solution:
97+
def minOperations(self, nums: List[int], k: int) -> int:
98+
return sum(nums) % k
9399
```
94100

95101
#### Java
96102

97103
```java
98-
104+
class Solution {
105+
public int minOperations(int[] nums, int k) {
106+
return Arrays.stream(nums).sum() % k;
107+
}
108+
}
99109
```
100110

101111
#### C++
102112

103113
```cpp
104-
114+
class Solution {
115+
public:
116+
int minOperations(vector<int>& nums, int k) {
117+
return reduce(nums.begin(), nums.end(), 0) % k;
118+
}
119+
};
105120
```
106121
107122
#### Go
108123
109124
```go
125+
func minOperations(nums []int, k int) (ans int) {
126+
for _, x := range nums {
127+
ans = (ans + x) % k
128+
}
129+
return
130+
}
131+
```
132+
133+
#### TypeScript
110134

135+
```ts
136+
function minOperations(nums: number[], k: number): number {
137+
return nums.reduce((acc, x) => acc + x, 0) % k;
138+
}
111139
```
112140

113141
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums, int k) {
4+
return reduce(nums.begin(), nums.end(), 0) % k;
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func minOperations(nums []int, k int) (ans int) {
2+
for _, x := range nums {
3+
ans = (ans + x) % k
4+
}
5+
return
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int minOperations(int[] nums, int k) {
3+
return Arrays.stream(nums).sum() % k;
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int], k: int) -> int:
3+
return sum(nums) % k
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function minOperations(nums: number[], k: number): number {
2+
return nums.reduce((acc, x) => acc + x, 0) % k;
3+
}

0 commit comments

Comments
 (0)