Skip to content

feat: add solutions to lc problem: No.3512 #4357

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 1 commit into from
Apr 16, 2025
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 @@ -84,32 +84,60 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Mi

<!-- solution:start -->

### 方法一
### 方法一:求和取模

题目实际上是求数组元素之和对 $k$ 取模的结果。因此,我们只需要遍历数组,计算出所有元素之和,然后对 $k$ 取模,最后返回这个结果即可。

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

<!-- tabs:start -->

#### Python3

```python

class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
return sum(nums) % k
```

#### Java

```java

class Solution {
public int minOperations(int[] nums, int k) {
return Arrays.stream(nums).sum() % k;
}
}
```

#### C++

```cpp

class Solution {
public:
int minOperations(vector<int>& nums, int k) {
return reduce(nums.begin(), nums.end(), 0) % k;
}
};
```

#### Go

```go
func minOperations(nums []int, k int) (ans int) {
for _, x := range nums {
ans = (ans + x) % k
}
return
}
```

#### TypeScript

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

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,32 +82,60 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Mi

<!-- solution:start -->

### Solution 1
### Solution 1: Sum and Modulo

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.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def minOperations(self, nums: List[int], k: int) -> int:
return sum(nums) % k
```

#### Java

```java

class Solution {
public int minOperations(int[] nums, int k) {
return Arrays.stream(nums).sum() % k;
}
}
```

#### C++

```cpp

class Solution {
public:
int minOperations(vector<int>& nums, int k) {
return reduce(nums.begin(), nums.end(), 0) % k;
}
};
```

#### Go

```go
func minOperations(nums []int, k int) (ans int) {
for _, x := range nums {
ans = (ans + x) % k
}
return
}
```

#### TypeScript

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

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