diff --git a/solution/3600-3699/3674.Minimum Operations to Equalize Array/README.md b/solution/3600-3699/3674.Minimum Operations to Equalize Array/README.md index 1efa9853ce587..0db7fdf4e82c2 100644 --- a/solution/3600-3699/3674.Minimum Operations to Equalize Array/README.md +++ b/solution/3600-3699/3674.Minimum Operations to Equalize Array/README.md @@ -63,32 +63,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3674.Mi -### 方法一 +### 方法一:一次遍历 + +如果 $\textit{nums}$ 中所有元素都相等,则不需要任何操作;否则,选择整个数组作为子数组进行一次操作即可。 + +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def minOperations(self, nums: List[int]) -> int: + return int(any(x != nums[0] for x in nums)) ``` #### Java ```java - +class Solution { + public int minOperations(int[] nums) { + for (int x : nums) { + if (x != nums[0]) { + return 1; + } + } + return 0; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minOperations(vector& nums) { + for (int x : nums) { + if (x != nums[0]) { + return 1; + } + } + return 0; + } +}; ``` #### Go ```go +func minOperations(nums []int) int { + for _, x := range nums { + if x != nums[0] { + return 1 + } + } + return 0 +} +``` +#### TypeScript + +```ts +function minOperations(nums: number[]): number { + for (const x of nums) { + if (x !== nums[0]) { + return 1; + } + } + return 0; +} ``` diff --git a/solution/3600-3699/3674.Minimum Operations to Equalize Array/README_EN.md b/solution/3600-3699/3674.Minimum Operations to Equalize Array/README_EN.md index d96d77835508b..61f8fe14fb3e4 100644 --- a/solution/3600-3699/3674.Minimum Operations to Equalize Array/README_EN.md +++ b/solution/3600-3699/3674.Minimum Operations to Equalize Array/README_EN.md @@ -59,32 +59,77 @@ A subarray is a contiguous non-empty sequence of element -### Solution 1 +### Solution 1: Single Pass + +If all elements in $\textit{nums}$ are equal, no operations are needed; otherwise, we can select the entire array as a subarray and perform one operation. + +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]) -> int: + return int(any(x != nums[0] for x in nums)) ``` #### Java ```java - +class Solution { + public int minOperations(int[] nums) { + for (int x : nums) { + if (x != nums[0]) { + return 1; + } + } + return 0; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minOperations(vector& nums) { + for (int x : nums) { + if (x != nums[0]) { + return 1; + } + } + return 0; + } +}; ``` #### Go ```go +func minOperations(nums []int) int { + for _, x := range nums { + if x != nums[0] { + return 1 + } + } + return 0 +} +``` +#### TypeScript + +```ts +function minOperations(nums: number[]): number { + for (const x of nums) { + if (x !== nums[0]) { + return 1; + } + } + return 0; +} ``` diff --git a/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.cpp b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.cpp new file mode 100644 index 0000000000000..2483bd929e642 --- /dev/null +++ b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int minOperations(vector& nums) { + for (int x : nums) { + if (x != nums[0]) { + return 1; + } + } + return 0; + } +}; diff --git a/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.go b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.go new file mode 100644 index 0000000000000..a0728a231daf1 --- /dev/null +++ b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.go @@ -0,0 +1,8 @@ +func minOperations(nums []int) int { + for _, x := range nums { + if x != nums[0] { + return 1 + } + } + return 0 +} diff --git a/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.java b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.java new file mode 100644 index 0000000000000..e7f1a24df2c8a --- /dev/null +++ b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.java @@ -0,0 +1,10 @@ +class Solution { + public int minOperations(int[] nums) { + for (int x : nums) { + if (x != nums[0]) { + return 1; + } + } + return 0; + } +} diff --git a/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.py b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.py new file mode 100644 index 0000000000000..4ffa2c0d17c48 --- /dev/null +++ b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def minOperations(self, nums: List[int]) -> int: + return int(any(x != nums[0] for x in nums)) diff --git a/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.ts b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.ts new file mode 100644 index 0000000000000..08f202621d8b2 --- /dev/null +++ b/solution/3600-3699/3674.Minimum Operations to Equalize Array/Solution.ts @@ -0,0 +1,8 @@ +function minOperations(nums: number[]): number { + for (const x of nums) { + if (x !== nums[0]) { + return 1; + } + } + return 0; +} diff --git a/solution/3600-3699/3675.Minimum Operations to Transform String/README.md b/solution/3600-3699/3675.Minimum Operations to Transform String/README.md index 18f12658e5c95..83b51f38bf986 100644 --- a/solution/3600-3699/3675.Minimum Operations to Transform String/README.md +++ b/solution/3600-3699/3675.Minimum Operations to Transform String/README.md @@ -76,32 +76,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3675.Mi -### 方法一 +### 方法一:一次遍历 + +根据题目描述,我们一定是先从字符 'b' 开始,依次将每个字符变为下一个字符,直到变为 'a'。因此,我们只需要统计字符串中距离 'a' 最远的字符与 'a' 的距离,即可得到答案。 + +时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def minOperations(self, s: str) -> int: + return max((26 - (ord(c) - 97) for c in s if c != "a"), default=0) ``` #### Java ```java - +class Solution { + public int minOperations(String s) { + int ans = 0; + for (char c : s.toCharArray()) { + if (c != 'a') { + ans = Math.max(ans, 26 - (c - 'a')); + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minOperations(string s) { + int ans = 0; + for (char c : s) { + if (c != 'a') { + ans = max(ans, 26 - (c - 'a')); + } + } + return ans; + } +}; ``` #### Go ```go +func minOperations(s string) (ans int) { + for _, c := range s { + if c != 'a' { + ans = max(ans, 26-int(c-'a')) + } + } + return +} +``` +#### TypeScript + +```ts +function minOperations(s: string): number { + let ans = 0; + for (const c of s) { + if (c !== 'a') { + ans = Math.max(ans, 26 - (c.charCodeAt(0) - 97)); + } + } + return ans; +} ``` diff --git a/solution/3600-3699/3675.Minimum Operations to Transform String/README_EN.md b/solution/3600-3699/3675.Minimum Operations to Transform String/README_EN.md index 55993a1fa7d7c..b1530e6ca90e8 100644 --- a/solution/3600-3699/3675.Minimum Operations to Transform String/README_EN.md +++ b/solution/3600-3699/3675.Minimum Operations to Transform String/README_EN.md @@ -74,32 +74,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3675.Mi -### Solution 1 +### Solution 1: Single Pass + +According to the problem description, we always start from the character 'b' and successively change each character to the next one until it becomes 'a'. Therefore, we only need to find the character in the string that is farthest from 'a' and calculate its distance to 'a' to get the answer. + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def minOperations(self, s: str) -> int: + return max((26 - (ord(c) - 97) for c in s if c != "a"), default=0) ``` #### Java ```java - +class Solution { + public int minOperations(String s) { + int ans = 0; + for (char c : s.toCharArray()) { + if (c != 'a') { + ans = Math.max(ans, 26 - (c - 'a')); + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minOperations(string s) { + int ans = 0; + for (char c : s) { + if (c != 'a') { + ans = max(ans, 26 - (c - 'a')); + } + } + return ans; + } +}; ``` #### Go ```go +func minOperations(s string) (ans int) { + for _, c := range s { + if c != 'a' { + ans = max(ans, 26-int(c-'a')) + } + } + return +} +``` +#### TypeScript + +```ts +function minOperations(s: string): number { + let ans = 0; + for (const c of s) { + if (c !== 'a') { + ans = Math.max(ans, 26 - (c.charCodeAt(0) - 97)); + } + } + return ans; +} ``` diff --git a/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.cpp b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.cpp new file mode 100644 index 0000000000000..587c2c58822cb --- /dev/null +++ b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int minOperations(string s) { + int ans = 0; + for (char c : s) { + if (c != 'a') { + ans = max(ans, 26 - (c - 'a')); + } + } + return ans; + } +}; diff --git a/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.go b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.go new file mode 100644 index 0000000000000..b86abcba009f2 --- /dev/null +++ b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.go @@ -0,0 +1,8 @@ +func minOperations(s string) (ans int) { + for _, c := range s { + if c != 'a' { + ans = max(ans, 26-int(c-'a')) + } + } + return +} diff --git a/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.java b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.java new file mode 100644 index 0000000000000..6704c1da7c350 --- /dev/null +++ b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.java @@ -0,0 +1,11 @@ +class Solution { + public int minOperations(String s) { + int ans = 0; + for (char c : s.toCharArray()) { + if (c != 'a') { + ans = Math.max(ans, 26 - (c - 'a')); + } + } + return ans; + } +} diff --git a/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.py b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.py new file mode 100644 index 0000000000000..f91d55b87763b --- /dev/null +++ b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def minOperations(self, s: str) -> int: + return max((26 - (ord(c) - 97) for c in s if c != "a"), default=0) diff --git a/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.ts b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.ts new file mode 100644 index 0000000000000..989a1a11123ef --- /dev/null +++ b/solution/3600-3699/3675.Minimum Operations to Transform String/Solution.ts @@ -0,0 +1,9 @@ +function minOperations(s: string): number { + let ans = 0; + for (const c of s) { + if (c !== 'a') { + ans = Math.max(ans, 26 - (c.charCodeAt(0) - 97)); + } + } + return ans; +}