Skip to content
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 @@ -63,32 +63,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3674.Mi

<!-- solution:start -->

### 方法一
### 方法一:一次遍历

如果 $\textit{nums}$ 中所有元素都相等,则不需要任何操作;否则,选择整个数组作为子数组进行一次操作即可。

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

<!-- tabs:start -->

#### 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<int>& 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;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,77 @@ A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of element

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

#### 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<int>& 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;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
int minOperations(vector<int>& nums) {
for (int x : nums) {
if (x != nums[0]) {
return 1;
}
}
return 0;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func minOperations(nums []int) int {
for _, x := range nums {
if x != nums[0] {
return 1
}
}
return 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public int minOperations(int[] nums) {
for (int x : nums) {
if (x != nums[0]) {
return 1;
}
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def minOperations(self, nums: List[int]) -> int:
return int(any(x != nums[0] for x in nums))
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function minOperations(nums: number[]): number {
for (const x of nums) {
if (x !== nums[0]) {
return 1;
}
}
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3675.Mi

<!-- solution:start -->

### 方法一
### 方法一:一次遍历

根据题目描述,我们一定是先从字符 'b' 开始,依次将每个字符变为下一个字符,直到变为 'a'。因此,我们只需要统计字符串中距离 'a' 最远的字符与 'a' 的距离,即可得到答案。

时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3675.Mi

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
};
Loading