Skip to content

feat: add solutions to lc problems: No.3151~3153 #2838

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
May 19, 2024
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
53 changes: 49 additions & 4 deletions solution/3100-3199/3151.Special Array I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3151.Sp

<!-- solution:start -->

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

我们从左到右遍历数组,对于每一对相邻元素,如果它们的奇偶性相同,那么数组就不是特殊数组,返回 `false`;否则,数组是特殊数组,返回 `true`。

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

<!-- tabs:start -->

#### Python3

```python

class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
return all(a % 2 != b % 2 for a, b in pairwise(nums))
```

#### Java

```java

class Solution {
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
}
```

#### C++

```cpp

class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
};
```

#### Go

```go
func isArraySpecial(nums []int) bool {
for i, x := range nums[1:] {
if x%2 == nums[i]%2 {
return false
}
}
return true
}
```

#### TypeScript

```ts
function isArraySpecial(nums: number[]): boolean {
for (let i = 1; i < nums.length; ++i) {
if (nums[i] % 2 === nums[i - 1] % 2) {
return false;
}
}
return true;
}
```

<!-- tabs:end -->
Expand Down
53 changes: 49 additions & 4 deletions solution/3100-3199/3151.Special Array I/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,77 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3151.Sp

<!-- solution:start -->

### Solution 1
### Solution 1: Single Pass

We traverse the array from left to right. For each pair of adjacent elements, if their parity is the same, then the array is not a special array, return `false`; otherwise, the array is a special array, return `true`.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)`.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
return all(a % 2 != b % 2 for a, b in pairwise(nums))
```

#### Java

```java

class Solution {
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
}
```

#### C++

```cpp

class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
};
```

#### Go

```go
func isArraySpecial(nums []int) bool {
for i, x := range nums[1:] {
if x%2 == nums[i]%2 {
return false
}
}
return true
}
```

#### TypeScript

```ts
function isArraySpecial(nums: number[]): boolean {
for (let i = 1; i < nums.length; ++i) {
if (nums[i] % 2 === nums[i - 1] % 2) {
return false;
}
}
return true;
}
```

<!-- tabs:end -->
Expand Down
11 changes: 11 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
bool isArraySpecial(vector<int>& nums) {
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
};
8 changes: 8 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func isArraySpecial(nums []int) bool {
for i, x := range nums[1:] {
if x%2 == nums[i]%2 {
return false
}
}
return true
}
10 changes: 10 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public boolean isArraySpecial(int[] nums) {
for (int i = 1; i < nums.length; ++i) {
if (nums[i] % 2 == nums[i - 1] % 2) {
return false;
}
}
return true;
}
}
3 changes: 3 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def isArraySpecial(self, nums: List[int]) -> bool:
return all(a % 2 != b % 2 for a, b in pairwise(nums))
8 changes: 8 additions & 0 deletions solution/3100-3199/3151.Special Array I/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function isArraySpecial(nums: number[]): boolean {
for (let i = 1; i < nums.length; ++i) {
if (nums[i] % 2 === nums[i - 1] % 2) {
return false;
}
}
return true;
}
86 changes: 82 additions & 4 deletions solution/3100-3199/3152.Special Array II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,110 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3152.Sp

<!-- solution:start -->

### 方法一
### 方法一:记录每个位置的最左特殊数组位置

我们可以定义一个数组 $d$ 来记录每个位置的最左特殊数组位置,初始时 $d[i] = i$。然后我们从左到右遍历数组 $nums$,如果 $nums[i]$ 和 $nums[i - 1]$ 的奇偶性不同,那么 $d[i] = d[i - 1]$。

最后我们遍历每个查询,判断 $d[to] <= from$ 是否成立即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def isArraySpecial(self, nums: List[int], queries: List[List[int]]) -> List[bool]:
n = len(nums)
d = list(range(n))
for i in range(1, n):
if nums[i] % 2 != nums[i - 1] % 2:
d[i] = d[i - 1]
return [d[t] <= f for f, t in queries]
```

#### Java

```java

class Solution {
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
int n = nums.length;
int[] d = new int[n];
for (int i = 1; i < n; ++i) {
if (nums[i] % 2 != nums[i - 1] % 2) {
d[i] = d[i - 1];
} else {
d[i] = i;
}
}
int m = queries.length;
boolean[] ans = new boolean[m];
for (int i = 0; i < m; ++i) {
ans[i] = d[queries[i][1]] <= queries[i][0];
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<bool> isArraySpecial(vector<int>& nums, vector<vector<int>>& queries) {
int n = nums.size();
vector<int> d(n);
iota(d.begin(), d.end(), 0);
for (int i = 1; i < n; ++i) {
if (nums[i] % 2 != nums[i - 1] % 2) {
d[i] = d[i - 1];
}
}
vector<bool> ans;
for (auto& q : queries) {
ans.push_back(d[q[1]] <= q[0]);
}
return ans;
}
};
```

#### Go

```go
func isArraySpecial(nums []int, queries [][]int) (ans []bool) {
n := len(nums)
d := make([]int, n)
for i := range d {
d[i] = i
}
for i := 1; i < len(nums); i++ {
if nums[i]%2 != nums[i-1]%2 {
d[i] = d[i-1]
}
}
for _, q := range queries {
ans = append(ans, d[q[1]] <= q[0])
}
return
}
```

#### TypeScript

```ts
function isArraySpecial(nums: number[], queries: number[][]): boolean[] {
const n = nums.length;
const d: number[] = Array.from({ length: n }, (_, i) => i);
for (let i = 1; i < n; ++i) {
if (nums[i] % 2 !== nums[i - 1] % 2) {
d[i] = d[i - 1];
}
}
return queries.map(([from, to]) => d[to] <= from);
}
```

<!-- tabs:end -->
Expand Down
Loading
Loading