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 5e3d4a0

Browse files
feat: add solutions to lc problems: No.3674,3675 (#4702)
1 parent 29183b6 commit 5e3d4a0

File tree

14 files changed

+285
-16
lines changed

14 files changed

+285
-16
lines changed

‎solution/3600-3699/3674.Minimum Operations to Equalize Array/README.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3674.Mi
6363

6464
<!-- solution:start -->
6565

66-
### 方法一
66+
### 方法一:一次遍历
67+
68+
如果 $\textit{nums}$ 中所有元素都相等,则不需要任何操作;否则,选择整个数组作为子数组进行一次操作即可。
69+
70+
时间复杂度 $O(n),ドル其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
6771

6872
<!-- tabs:start -->
6973

7074
#### Python3
7175

7276
```python
73-
77+
class Solution:
78+
def minOperations(self, nums: List[int]) -> int:
79+
return int(any(x != nums[0] for x in nums))
7480
```
7581

7682
#### Java
7783

7884
```java
79-
85+
class Solution {
86+
public int minOperations(int[] nums) {
87+
for (int x : nums) {
88+
if (x != nums[0]) {
89+
return 1;
90+
}
91+
}
92+
return 0;
93+
}
94+
}
8095
```
8196

8297
#### C++
8398

8499
```cpp
85-
100+
class Solution {
101+
public:
102+
int minOperations(vector<int>& nums) {
103+
for (int x : nums) {
104+
if (x != nums[0]) {
105+
return 1;
106+
}
107+
}
108+
return 0;
109+
}
110+
};
86111
```
87112
88113
#### Go
89114
90115
```go
116+
func minOperations(nums []int) int {
117+
for _, x := range nums {
118+
if x != nums[0] {
119+
return 1
120+
}
121+
}
122+
return 0
123+
}
124+
```
91125

126+
#### TypeScript
127+
128+
```ts
129+
function minOperations(nums: number[]): number {
130+
for (const x of nums) {
131+
if (x !== nums[0]) {
132+
return 1;
133+
}
134+
}
135+
return 0;
136+
}
92137
```
93138

94139
<!-- tabs:end -->

‎solution/3600-3699/3674.Minimum Operations to Equalize Array/README_EN.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,32 +59,77 @@ A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of element
5959

6060
<!-- solution:start -->
6161

62-
### Solution 1
62+
### Solution 1: Single Pass
63+
64+
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.
65+
66+
The time complexity is $O(n),ドル where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
6367

6468
<!-- tabs:start -->
6569

6670
#### Python3
6771

6872
```python
69-
73+
class Solution:
74+
def minOperations(self, nums: List[int]) -> int:
75+
return int(any(x != nums[0] for x in nums))
7076
```
7177

7278
#### Java
7379

7480
```java
75-
81+
class Solution {
82+
public int minOperations(int[] nums) {
83+
for (int x : nums) {
84+
if (x != nums[0]) {
85+
return 1;
86+
}
87+
}
88+
return 0;
89+
}
90+
}
7691
```
7792

7893
#### C++
7994

8095
```cpp
81-
96+
class Solution {
97+
public:
98+
int minOperations(vector<int>& nums) {
99+
for (int x : nums) {
100+
if (x != nums[0]) {
101+
return 1;
102+
}
103+
}
104+
return 0;
105+
}
106+
};
82107
```
83108
84109
#### Go
85110
86111
```go
112+
func minOperations(nums []int) int {
113+
for _, x := range nums {
114+
if x != nums[0] {
115+
return 1
116+
}
117+
}
118+
return 0
119+
}
120+
```
87121

122+
#### TypeScript
123+
124+
```ts
125+
function minOperations(nums: number[]): number {
126+
for (const x of nums) {
127+
if (x !== nums[0]) {
128+
return 1;
129+
}
130+
}
131+
return 0;
132+
}
88133
```
89134

90135
<!-- tabs:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums) {
4+
for (int x : nums) {
5+
if (x != nums[0]) {
6+
return 1;
7+
}
8+
}
9+
return 0;
10+
}
11+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func minOperations(nums []int) int {
2+
for _, x := range nums {
3+
if x != nums[0] {
4+
return 1
5+
}
6+
}
7+
return 0
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int minOperations(int[] nums) {
3+
for (int x : nums) {
4+
if (x != nums[0]) {
5+
return 1;
6+
}
7+
}
8+
return 0;
9+
}
10+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int]) -> int:
3+
return int(any(x != nums[0] for x in nums))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function minOperations(nums: number[]): number {
2+
for (const x of nums) {
3+
if (x !== nums[0]) {
4+
return 1;
5+
}
6+
}
7+
return 0;
8+
}

‎solution/3600-3699/3675.Minimum Operations to Transform String/README.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3675.Mi
7676

7777
<!-- solution:start -->
7878

79-
### 方法一
79+
### 方法一:一次遍历
80+
81+
根据题目描述,我们一定是先从字符 'b' 开始,依次将每个字符变为下一个字符,直到变为 'a'。因此,我们只需要统计字符串中距离 'a' 最远的字符与 'a' 的距离,即可得到答案。
82+
83+
时间复杂度 $O(n),ドル其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
8084

8185
<!-- tabs:start -->
8286

8387
#### Python3
8488

8589
```python
86-
90+
class Solution:
91+
def minOperations(self, s: str) -> int:
92+
return max((26 - (ord(c) - 97) for c in s if c != "a"), default=0)
8793
```
8894

8995
#### Java
9096

9197
```java
92-
98+
class Solution {
99+
public int minOperations(String s) {
100+
int ans = 0;
101+
for (char c : s.toCharArray()) {
102+
if (c != 'a') {
103+
ans = Math.max(ans, 26 - (c - 'a'));
104+
}
105+
}
106+
return ans;
107+
}
108+
}
93109
```
94110

95111
#### C++
96112

97113
```cpp
98-
114+
class Solution {
115+
public:
116+
int minOperations(string s) {
117+
int ans = 0;
118+
for (char c : s) {
119+
if (c != 'a') {
120+
ans = max(ans, 26 - (c - 'a'));
121+
}
122+
}
123+
return ans;
124+
}
125+
};
99126
```
100127
101128
#### Go
102129
103130
```go
131+
func minOperations(s string) (ans int) {
132+
for _, c := range s {
133+
if c != 'a' {
134+
ans = max(ans, 26-int(c-'a'))
135+
}
136+
}
137+
return
138+
}
139+
```
104140

141+
#### TypeScript
142+
143+
```ts
144+
function minOperations(s: string): number {
145+
let ans = 0;
146+
for (const c of s) {
147+
if (c !== 'a') {
148+
ans = Math.max(ans, 26 - (c.charCodeAt(0) - 97));
149+
}
150+
}
151+
return ans;
152+
}
105153
```
106154

107155
<!-- tabs:end -->

‎solution/3600-3699/3675.Minimum Operations to Transform String/README_EN.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3675.Mi
7474

7575
<!-- solution:start -->
7676

77-
### Solution 1
77+
### Solution 1: Single Pass
78+
79+
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.
80+
81+
The time complexity is $O(n),ドル where $n$ is the length of the string $s$. The space complexity is $O(1)$.
7882

7983
<!-- tabs:start -->
8084

8185
#### Python3
8286

8387
```python
84-
88+
class Solution:
89+
def minOperations(self, s: str) -> int:
90+
return max((26 - (ord(c) - 97) for c in s if c != "a"), default=0)
8591
```
8692

8793
#### Java
8894

8995
```java
90-
96+
class Solution {
97+
public int minOperations(String s) {
98+
int ans = 0;
99+
for (char c : s.toCharArray()) {
100+
if (c != 'a') {
101+
ans = Math.max(ans, 26 - (c - 'a'));
102+
}
103+
}
104+
return ans;
105+
}
106+
}
91107
```
92108

93109
#### C++
94110

95111
```cpp
96-
112+
class Solution {
113+
public:
114+
int minOperations(string s) {
115+
int ans = 0;
116+
for (char c : s) {
117+
if (c != 'a') {
118+
ans = max(ans, 26 - (c - 'a'));
119+
}
120+
}
121+
return ans;
122+
}
123+
};
97124
```
98125
99126
#### Go
100127
101128
```go
129+
func minOperations(s string) (ans int) {
130+
for _, c := range s {
131+
if c != 'a' {
132+
ans = max(ans, 26-int(c-'a'))
133+
}
134+
}
135+
return
136+
}
137+
```
102138

139+
#### TypeScript
140+
141+
```ts
142+
function minOperations(s: string): number {
143+
let ans = 0;
144+
for (const c of s) {
145+
if (c !== 'a') {
146+
ans = Math.max(ans, 26 - (c.charCodeAt(0) - 97));
147+
}
148+
}
149+
return ans;
150+
}
103151
```
104152

105153
<!-- tabs:end -->
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int minOperations(string s) {
4+
int ans = 0;
5+
for (char c : s) {
6+
if (c != 'a') {
7+
ans = max(ans, 26 - (c - 'a'));
8+
}
9+
}
10+
return ans;
11+
}
12+
};

0 commit comments

Comments
(0)

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