Skip to content

Commit dfdf8d0

Browse files
committed
Apr-15
1 parent 67eb035 commit dfdf8d0

6 files changed

+331
-13
lines changed

README.md

Lines changed: 22 additions & 12 deletions
Large diffs are not rendered by default.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Tag: Array, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
8+
//  
9+
// Example 1:
10+
//
11+
// Input: nums = [8,2,4,7], limit = 4
12+
// Output: 2
13+
// Explanation: All subarrays are:
14+
// [8] with maximum absolute diff |8-8| = 0 <= 4.
15+
// [8,2] with maximum absolute diff |8-2| = 6 > 4.
16+
// [8,2,4] with maximum absolute diff |8-2| = 6 > 4.
17+
// [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
18+
// [2] with maximum absolute diff |2-2| = 0 <= 4.
19+
// [2,4] with maximum absolute diff |2-4| = 2 <= 4.
20+
// [2,4,7] with maximum absolute diff |2-7| = 5 > 4.
21+
// [4] with maximum absolute diff |4-4| = 0 <= 4.
22+
// [4,7] with maximum absolute diff |4-7| = 3 <= 4.
23+
// [7] with maximum absolute diff |7-7| = 0 <= 4.
24+
// Therefore, the size of the longest subarray is 2.
25+
//
26+
// Example 2:
27+
//
28+
// Input: nums = [10,1,2,4,7,2], limit = 5
29+
// Output: 4
30+
// Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
31+
//
32+
// Example 3:
33+
//
34+
// Input: nums = [4,2,2,2,4,4,2,2], limit = 0
35+
// Output: 3
36+
//
37+
//  
38+
// Constraints:
39+
//
40+
// 1 <= nums.length <= 105
41+
// 1 <= nums[i] <= 109
42+
// 0 <= limit <= 109
43+
//
44+
//
45+
46+
class Solution {
47+
public:
48+
int longestSubarray(vector<int>& nums, int limit) {
49+
int n = nums.size();
50+
deque<int> smallq;
51+
deque<int> bigq;
52+
int res = 0;
53+
int i = 0;
54+
for (int j = 0; j < n; j++) {
55+
while (!bigq.empty() && nums[bigq.back()] < nums[j]) {
56+
bigq.pop_back();
57+
}
58+
bigq.push_back(j);
59+
60+
while (!smallq.empty() && nums[smallq.back()] > nums[j]) {
61+
smallq.pop_back();
62+
}
63+
smallq.push_back(j);
64+
while (nums[bigq.front()] - nums[smallq.front()] > limit) {
65+
if (i == bigq.front()) {
66+
bigq.pop_front();
67+
}
68+
69+
if (i == smallq.front()) {
70+
smallq.pop_front();
71+
}
72+
i += 1;
73+
}
74+
75+
res = max(res, j - i + 1);
76+
}
77+
78+
return res;
79+
}
80+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Tag: Array, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.
8+
#  
9+
# Example 1:
10+
#
11+
# Input: nums = [8,2,4,7], limit = 4
12+
# Output: 2
13+
# Explanation: All subarrays are:
14+
# [8] with maximum absolute diff |8-8| = 0 <= 4.
15+
# [8,2] with maximum absolute diff |8-2| = 6 > 4.
16+
# [8,2,4] with maximum absolute diff |8-2| = 6 > 4.
17+
# [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
18+
# [2] with maximum absolute diff |2-2| = 0 <= 4.
19+
# [2,4] with maximum absolute diff |2-4| = 2 <= 4.
20+
# [2,4,7] with maximum absolute diff |2-7| = 5 > 4.
21+
# [4] with maximum absolute diff |4-4| = 0 <= 4.
22+
# [4,7] with maximum absolute diff |4-7| = 3 <= 4.
23+
# [7] with maximum absolute diff |7-7| = 0 <= 4.
24+
# Therefore, the size of the longest subarray is 2.
25+
#
26+
# Example 2:
27+
#
28+
# Input: nums = [10,1,2,4,7,2], limit = 5
29+
# Output: 4
30+
# Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
31+
#
32+
# Example 3:
33+
#
34+
# Input: nums = [4,2,2,2,4,4,2,2], limit = 0
35+
# Output: 3
36+
#
37+
#  
38+
# Constraints:
39+
#
40+
# 1 <= nums.length <= 105
41+
# 1 <= nums[i] <= 109
42+
# 0 <= limit <= 109
43+
#
44+
#
45+
46+
from collections import deque
47+
class Solution:
48+
def longestSubarray(self, nums: List[int], limit: int) -> int:
49+
n = len(nums)
50+
res = 0
51+
bigq = deque()
52+
smallq = deque()
53+
i = 0
54+
for j in range(n):
55+
while len(bigq) > 0 and nums[bigq[-1]] < nums[j]:
56+
bigq.pop()
57+
bigq.append(j)
58+
59+
while len(smallq) > 0 and nums[smallq[-1]] > nums[j]:
60+
smallq.pop()
61+
smallq.append(j)
62+
63+
while nums[bigq[0]] - nums[smallq[0]] > limit:
64+
if i == bigq[0]:
65+
bigq.popleft()
66+
67+
if i == smallq[0]:
68+
smallq.popleft()
69+
70+
i += 1
71+
72+
res = max(res, j - i + 1)
73+
74+
return res
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Tag: Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
2+
// Time: O(NlogN)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/xWhiSWNeLtk
7+
8+
// You are given two 0-indexed arrays nums1 and nums2 of length n, both of which are permutations of [0, 1, ..., n - 1].
9+
// A good triplet is a set of 3 distinct values which are present in increasing order by position both in nums1 and nums2. In other words, if we consider pos1v as the index of the value v in nums1 and pos2v as the index of the value v in nums2, then a good triplet will be a set (x, y, z) where 0 <= x, y, z <= n - 1, such that pos1x < pos1y < pos1z and pos2x < pos2y < pos2z.
10+
// Return the total number of good triplets.
11+
//  
12+
// Example 1:
13+
//
14+
// Input: nums1 = [2,0,1,3], nums2 = [0,1,2,3]
15+
// Output: 1
16+
// Explanation:
17+
// There are 4 triplets (x,y,z) such that pos1x < pos1y < pos1z. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3).
18+
// Out of those triplets, only the triplet (0,1,3) satisfies pos2x < pos2y < pos2z. Hence, there is only 1 good triplet.
19+
//
20+
// Example 2:
21+
//
22+
// Input: nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3]
23+
// Output: 4
24+
// Explanation: The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
25+
//
26+
//  
27+
// Constraints:
28+
//
29+
// n == nums1.length == nums2.length
30+
// 3 <= n <= 105
31+
// 0 <= nums1[i], nums2[i] <= n - 1
32+
// nums1 and nums2 are permutations of [0, 1, ..., n - 1].
33+
//
34+
//
35+
36+
class BitTree {
37+
public:
38+
int n;
39+
vector<int> arr;
40+
BitTree(int size) {
41+
n = size;
42+
arr.resize(size + 1);
43+
}
44+
45+
void update(int i, int delta) {
46+
i = i + 1;
47+
while (i <= n) {
48+
arr[i] += delta;
49+
i += i & -i;
50+
}
51+
}
52+
53+
int query(int i) {
54+
int res = 0;
55+
i = i + 1;
56+
while (i > 0) {
57+
res += arr[i];
58+
i -= i & -i;
59+
}
60+
return res;
61+
}
62+
};
63+
64+
class Solution {
65+
public:
66+
long long goodTriplets(vector<int>& nums1, vector<int>& nums2) {
67+
int n = nums1.size();
68+
vector<int> indexes(n, 0);
69+
for (int i = 0; i < n; i++) {
70+
indexes[nums2[i]] = i;
71+
}
72+
73+
BitTree bit(n);
74+
long long res = 0;
75+
for (int i = 0; i < n; i++) {
76+
int mid = indexes[nums1[i]];
77+
int small = bit.query(mid);
78+
int big = n - 1 - mid - (i - small);
79+
res += 1LL * big * small;
80+
bit.update(mid, 1);
81+
}
82+
return res;
83+
}
84+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Tag: Array, Binary Search, Divide and Conquer, Binary Indexed Tree, Segment Tree, Merge Sort, Ordered Set
2+
# Time: O(NlogN)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/xWhiSWNeLtk
7+
8+
# You are given two 0-indexed arrays nums1 and nums2 of length n, both of which are permutations of [0, 1, ..., n - 1].
9+
# A good triplet is a set of 3 distinct values which are present in increasing order by position both in nums1 and nums2. In other words, if we consider pos1v as the index of the value v in nums1 and pos2v as the index of the value v in nums2, then a good triplet will be a set (x, y, z) where 0 <= x, y, z <= n - 1, such that pos1x < pos1y < pos1z and pos2x < pos2y < pos2z.
10+
# Return the total number of good triplets.
11+
#  
12+
# Example 1:
13+
#
14+
# Input: nums1 = [2,0,1,3], nums2 = [0,1,2,3]
15+
# Output: 1
16+
# Explanation:
17+
# There are 4 triplets (x,y,z) such that pos1x < pos1y < pos1z. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3).
18+
# Out of those triplets, only the triplet (0,1,3) satisfies pos2x < pos2y < pos2z. Hence, there is only 1 good triplet.
19+
#
20+
# Example 2:
21+
#
22+
# Input: nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3]
23+
# Output: 4
24+
# Explanation: The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
25+
#
26+
#  
27+
# Constraints:
28+
#
29+
# n == nums1.length == nums2.length
30+
# 3 <= n <= 105
31+
# 0 <= nums1[i], nums2[i] <= n - 1
32+
# nums1 and nums2 are permutations of [0, 1, ..., n - 1].
33+
#
34+
#
35+
36+
class BitTree:
37+
def __init__(self, size):
38+
self.n = size
39+
self.arr = [0] * (size + 1)
40+
41+
def update(self, i: int, delta: int):
42+
i = i + 1
43+
while i <= self.n:
44+
self.arr[i] += delta
45+
i += i & -i
46+
47+
def prefix_sum(self, i: int) -> int:
48+
i = i + 1
49+
res = 0
50+
while i > 0:
51+
res += self.arr[i]
52+
i -= i & -i
53+
return res
54+
55+
class Solution:
56+
def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int:
57+
n = len(nums1)
58+
indexes = [0 for i in range(n)]
59+
for i in range(n):
60+
indexes[nums2[i]] = i
61+
62+
bit = BitTree(n)
63+
res = 0
64+
for i in range(n):
65+
mid = indexes[nums1[i]]
66+
small = bit.prefix_sum(mid)
67+
big = n - 1 - mid - (i - small)
68+
res += small * big
69+
bit.update(mid, 1)
70+
return res

list/endlesscheng.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
86. https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/ +Mark
102102
87. https://leetcode.com/problems/subarrays-with-k-different-integers/
103103

104-
- https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/
104+
88. https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/
105105
- https://leetcode.com/problems/friends-of-appropriate-ages/
106106
- https://leetcode.com/problems/longest-nice-subarray/
107107
- https://leetcode.com/problems/swap-for-longest-repeated-character-substring/

0 commit comments

Comments
 (0)