Skip to content

Commit 1b92196

Browse files
committed
leetcode
1 parent a0d3c01 commit 1b92196

File tree

3 files changed

+327
-0
lines changed

3 files changed

+327
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
3+
-* 1630. Arithmetic Subarrays *-
4+
5+
6+
A sequence of numbers is called arithmetic if it consists of at least two elements, and the difference between every two consecutive elements is the same. More formally, a sequence s is arithmetic if and only if s[i+1] - s[i] == s[1] - s[0] for all valid i.
7+
8+
For example, these are arithmetic sequences:
9+
10+
1, 3, 5, 7, 9
11+
7, 7, 7, 7
12+
3, -1, -5, -9
13+
The following sequence is not arithmetic:
14+
15+
1, 1, 2, 5, 7
16+
You are given an array of n integers, nums, and two arrays of m integers each, l and r, representing the m range queries, where the ith query is the range [l[i], r[i]]. All the arrays are 0-indexed.
17+
18+
Return a list of boolean elements answer, where answer[i] is true if the subarray nums[l[i]], nums[l[i]+1], ... , nums[r[i]] can be rearranged to form an arithmetic sequence, and false otherwise.
19+
20+
21+
22+
Example 1:
23+
24+
Input: nums = [4,6,5,9,3,7], l = [0,0,2], r = [2,3,5]
25+
Output: [true,false,true]
26+
Explanation:
27+
In the 0th query, the subarray is [4,6,5]. This can be rearranged as [6,5,4], which is an arithmetic sequence.
28+
In the 1st query, the subarray is [4,6,5,9]. This cannot be rearranged as an arithmetic sequence.
29+
In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is an arithmetic sequence.
30+
Example 2:
31+
32+
Input: nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10]
33+
Output: [false,true,false,false,true,true]
34+
35+
36+
Constraints:
37+
38+
n == nums.length
39+
m == l.length
40+
m == r.length
41+
2 <= n <= 500
42+
1 <= m <= 500
43+
0 <= l[i] < r[i] < n
44+
-105 <= nums[i] <= 105
45+
46+
*/
47+
48+
class Solution {
49+
bool isArithmetic(List<int> nums, int ll, int rr) {
50+
final int minN = nums.sublist(ll, rr + 1).reduce((a, b) => a < b ? a : b);
51+
final int maxN = nums.sublist(ll, rr + 1).reduce((a, b) => a > b ? a : b);
52+
final int diff = maxN - minN;
53+
if (diff == 0) return true;
54+
final int d = diff ~/ (rr - ll);
55+
if (diff % (rr - ll) != 0) return false;
56+
final List<bool> term = List.filled(rr - ll + 1, false);
57+
for (int i = ll; i <= rr; i++) {
58+
final int x = nums[i] - minN;
59+
if (x % d != 0) return false;
60+
final int j = x ~/ d;
61+
if (term[j])
62+
return false; // exact once
63+
else
64+
term[j] = true;
65+
}
66+
return true;
67+
}
68+
69+
List<bool> checkArithmeticSubarrays(
70+
final List<int> nums, List<int> l, List<int> r) {
71+
// int n = nums.length;
72+
final int m = l.length;
73+
final List<bool> ans = List.filled(m, false);
74+
for (int i = 0; i < m; i++) {
75+
ans[i] = isArithmetic(nums, l[i], r[i]);
76+
}
77+
return ans;
78+
}
79+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package main
2+
3+
import "math"
4+
5+
func checkArithmeticSubarrays(nums []int, l []int, r []int) []bool {
6+
var ans []bool
7+
8+
for i := 0; i < len(l); i++ {
9+
min := math.MaxInt32
10+
max := math.MinInt32
11+
length := r[i] - l[i] + 1
12+
13+
for j := l[i]; j <= r[i]; j++ {
14+
min = int(math.Min(float64(min), float64(nums[j])))
15+
max = int(math.Max(float64(max), float64(nums[j])))
16+
}
17+
18+
if min == max {
19+
ans = append(ans, true)
20+
} else if (max-min)%(length-1) != 0 {
21+
ans = append(ans, false)
22+
} else {
23+
diff := make([]bool, length)
24+
25+
step := (max - min) / (length - 1)
26+
j := l[i]
27+
28+
for j <= r[i] {
29+
if (nums[j]-min)%step != 0 || diff[(nums[j]-min)/step] {
30+
break
31+
}
32+
33+
diff[(nums[j]-min)/step] = true
34+
j++
35+
}
36+
37+
ans = append(ans, j > r[i])
38+
}
39+
}
40+
41+
return ans
42+
}
43+
44+
45+
/*
46+
47+
48+
func isArithmetic(nums []int, ll, rr int) bool {
49+
minN := nums[ll]
50+
maxN := nums[ll]
51+
for i := ll; i <= rr; i++ {
52+
if nums[i] < minN {
53+
minN = nums[i]
54+
}
55+
if nums[i] > maxN {
56+
maxN = nums[i]
57+
}
58+
}
59+
diff := maxN - minN
60+
if diff == 0 {
61+
return true
62+
}
63+
d := diff / (rr - ll)
64+
if diff%(rr-ll) != 0 {
65+
return false
66+
}
67+
term := make([]bool, rr-ll+1)
68+
for i := ll; i <= rr; i++ {
69+
x := nums[i] - minN
70+
if x%d != 0 {
71+
return false
72+
}
73+
j := x / d
74+
if term[j] {
75+
return false // exact once
76+
} else {
77+
term[j] = true
78+
}
79+
}
80+
return true
81+
}
82+
83+
func checkArithmeticSubarrays(nums []int, l, r []int) []bool {
84+
m := len(l)
85+
ans := make([]bool, m)
86+
for i := 0; i < m; i++ {
87+
ans[i] = isArithmetic(nums, l[i], r[i])
88+
}
89+
return ans
90+
}
91+
92+
93+
94+
*/
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# ✅ 🔥 Arithmetic Subarrays 🔥 || Simple Fast and Easy || with Explanation 😈
2+
3+
### Intuition
4+
5+
The code aims to determine whether each subarray specified by the ranges in arrays `l` and `r` forms an arithmetic sequence. An arithmetic sequence is a sequence of numbers in which the difference between consecutive elements is constant.
6+
7+
### Approach
8+
9+
1. **isArithmetic Function:**
10+
- Find the minimum and maximum values in the given subarray (`ll` to `rr` inclusive).
11+
- Calculate the difference (`diff`) between the maximum and minimum values.
12+
- If `diff` is zero, all elements in the subarray are the same, and it's considered an arithmetic sequence.
13+
- Calculate the common difference `d` and check if it evenly divides the difference between max and min.
14+
- Create a boolean array (`term`) to keep track of whether each potential element in the arithmetic sequence has been encountered.
15+
- Iterate through the subarray, calculate the position of each element relative to the minimum, and check if it forms a valid position in the arithmetic sequence. If not, return false.
16+
- If the loop completes, return true, indicating the subarray is an arithmetic sequence.
17+
18+
2. **checkArithmeticSubarrays Function:**
19+
- Iterate through the ranges specified by arrays `l` and `r`.
20+
- For each range, call the `isArithmetic` function to determine if the corresponding subarray is an arithmetic sequence.
21+
- Store the result in the `ans` array.
22+
23+
### Time Complexity
24+
25+
- The time complexity of the `isArithmetic` function is O(n), where n is the length of the subarray (`rr - ll + 1`).
26+
- The `checkArithmeticSubarrays` function iterates through m ranges, and for each range, it calls `isArithmetic`, resulting in a total time complexity of O(m * n), where m is the number of ranges.
27+
28+
### Space Complexity
29+
30+
- The space complexity is O(n) for the `term` array in the `isArithmetic` function, where n is the length of the subarray.
31+
32+
### Note
33+
34+
- The given code assumes that the input arrays `l` and `r` are valid ranges (0 <= ll <= rr < len(nums)). If this assumption doesn't hold, additional boundary checks may be needed.
35+
36+
## Code
37+
38+
```go
39+
func isArithmetic(nums []int, ll, rr int) bool {
40+
minN := nums[ll]
41+
maxN := nums[ll]
42+
for i := ll; i <= rr; i++ {
43+
if nums[i] < minN {
44+
minN = nums[i]
45+
}
46+
if nums[i] > maxN {
47+
maxN = nums[i]
48+
}
49+
}
50+
diff := maxN - minN
51+
if diff == 0 {
52+
return true
53+
}
54+
d := diff / (rr - ll)
55+
if diff%(rr-ll) != 0 {
56+
return false
57+
}
58+
term := make([]bool, rr-ll+1)
59+
for i := ll; i <= rr; i++ {
60+
x := nums[i] - minN
61+
if x%d != 0 {
62+
return false
63+
}
64+
j := x / d
65+
if term[j] {
66+
return false // exact once
67+
} else {
68+
term[j] = true
69+
}
70+
}
71+
return true
72+
}
73+
74+
func checkArithmeticSubarrays(nums []int, l, r []int) []bool {
75+
m := len(l)
76+
ans := make([]bool, m)
77+
for i := 0; i < m; i++ {
78+
ans[i] = isArithmetic(nums, l[i], r[i])
79+
}
80+
return ans
81+
}
82+
```
83+
84+
## Solution - 2
85+
86+
### Intuition
87+
88+
The code still aims to determine whether each subarray specified by the ranges in arrays `l` and `r` forms an arithmetic sequence.
89+
90+
### Approach
91+
92+
1. **checkArithmeticSubarrays Function:**
93+
- Iterate through the ranges specified by arrays `l` and `r`.
94+
- For each range, find the minimum and maximum values in the subarray.
95+
- If the minimum and maximum are equal, the subarray is considered an arithmetic sequence.
96+
- If the difference between the maximum and minimum is not divisible evenly by the length of the subarray minus one, it's not an arithmetic sequence.
97+
- Otherwise, iterate through the subarray, calculate the step size, and check if each element fits into a valid position in the arithmetic sequence. If yes, the subarray is considered an arithmetic sequence.
98+
99+
### Time Complexity
100+
101+
- The time complexity of this implementation is O(m * n), where m is the number of ranges, and n is the length of the subarray (`r[i] - l[i] + 1`).
102+
103+
### Space Complexity
104+
105+
- The space complexity is O(n) for the `diff` array, where n is the length of the subarray.
106+
107+
### Note
108+
109+
- This implementation uses a similar logic to the previous one but with some modifications in the details of implementation. The choice between the two implementations may depend on factors such as readability and personal coding style.
110+
111+
## Code
112+
113+
```go
114+
func checkArithmeticSubarrays(nums []int, l []int, r []int) []bool {
115+
var ans []bool
116+
117+
for i := 0; i < len(l); i++ {
118+
min := math.MaxInt32
119+
max := math.MinInt32
120+
length := r[i] - l[i] + 1
121+
122+
for j := l[i]; j <= r[i]; j++ {
123+
min = int(math.Min(float64(min), float64(nums[j])))
124+
max = int(math.Max(float64(max), float64(nums[j])))
125+
}
126+
127+
if min == max {
128+
ans = append(ans, true)
129+
} else if (max-min)%(length-1) != 0 {
130+
ans = append(ans, false)
131+
} else {
132+
diff := make([]bool, length)
133+
134+
step := (max - min) / (length - 1)
135+
j := l[i]
136+
137+
for j <= r[i] {
138+
if (nums[j]-min)%step != 0 || diff[(nums[j]-min)/step] {
139+
break
140+
}
141+
142+
diff[(nums[j]-min)/step] = true
143+
j++
144+
}
145+
146+
ans = append(ans, j > r[i])
147+
}
148+
}
149+
150+
return ans
151+
}
152+
```
153+
154+
## [GitHub](https://github.com/ayoubzulfiqar/leetcode)

0 commit comments

Comments
 (0)