diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md index 56a9933b0eac9..477593cc7a52a 100644 --- a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README.md @@ -84,32 +84,60 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Mi -### 方法一 +### 方法一:求和取模 + +题目实际上是求数组元素之和对 $k$ 取模的结果。因此,我们只需要遍历数组,计算出所有元素之和,然后对 $k$ 取模,最后返回这个结果即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 #### 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& 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; +} ``` diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md index f3e3b39d3cd12..e4adb8d98a3e9 100644 --- a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/README_EN.md @@ -82,32 +82,60 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3512.Mi -### 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)$. #### 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& 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; +} ``` diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.cpp b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.cpp new file mode 100644 index 0000000000000..8ae8217649a57 --- /dev/null +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + int minOperations(vector& nums, int k) { + return reduce(nums.begin(), nums.end(), 0) % k; + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.go b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.go new file mode 100644 index 0000000000000..18ccd4fb0afe5 --- /dev/null +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.go @@ -0,0 +1,6 @@ +func minOperations(nums []int, k int) (ans int) { + for _, x := range nums { + ans = (ans + x) % k + } + return +} \ No newline at end of file diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.java b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.java new file mode 100644 index 0000000000000..5559072c4a3b0 --- /dev/null +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.java @@ -0,0 +1,5 @@ +class Solution { + public int minOperations(int[] nums, int k) { + return Arrays.stream(nums).sum() % k; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.py b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.py new file mode 100644 index 0000000000000..26904a4e47634 --- /dev/null +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def minOperations(self, nums: List[int], k: int) -> int: + return sum(nums) % k diff --git a/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.ts b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.ts new file mode 100644 index 0000000000000..fd98a8bf077e6 --- /dev/null +++ b/solution/3500-3599/3512.Minimum Operations to Make Array Sum Divisible by K/Solution.ts @@ -0,0 +1,3 @@ +function minOperations(nums: number[], k: number): number { + return nums.reduce((acc, x) => acc + x, 0) % k; +}