Skip to content

Commit b985ba5

Browse files
committed
Apr-29
1 parent dcc7f7d commit b985ba5

10 files changed

+388
-88
lines changed

README.md

+19-11
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Tag: Array, Hash Table, Math, Two Pointers
2+
// Time: O(NM)
3+
// Space: O(N + M)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
8+
//
9+
// Type 1: Triplet (i, j, k) if nums1[i]2 == nums2[j] * nums2[k] where 0 <= i < nums1.length and 0 <= j < k < nums2.length.
10+
// Type 2: Triplet (i, j, k) if nums2[i]2 == nums1[j] * nums1[k] where 0 <= i < nums2.length and 0 <= j < k < nums1.length.
11+
//
12+
//  
13+
// Example 1:
14+
//
15+
// Input: nums1 = [7,4], nums2 = [5,2,8,9]
16+
// Output: 1
17+
// Explanation: Type 1: (1, 1, 2), nums1[1]2 = nums2[1] * nums2[2]. (42 = 2 * 8).
18+
//
19+
// Example 2:
20+
//
21+
// Input: nums1 = [1,1], nums2 = [1,1,1]
22+
// Output: 9
23+
// Explanation: All Triplets are valid, because 12 = 1 * 1.
24+
// Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2). nums1[i]2 = nums2[j] * nums2[k].
25+
// Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]2 = nums1[j] * nums1[k].
26+
//
27+
// Example 3:
28+
//
29+
// Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
30+
// Output: 2
31+
// Explanation: There are 2 valid triplets.
32+
// Type 1: (3,0,2). nums1[3]2 = nums2[0] * nums2[2].
33+
// Type 2: (3,0,1). nums2[3]2 = nums1[0] * nums1[1].
34+
//
35+
//  
36+
// Constraints:
37+
//
38+
// 1 <= nums1.length, nums2.length <= 1000
39+
// 1 <= nums1[i], nums2[i] <= 105
40+
//
41+
//
42+
43+
class Solution {
44+
public:
45+
int numTriplets(vector<int>& nums1, vector<int>& nums2) {
46+
sort(nums1.begin(), nums1.end());
47+
sort(nums2.begin(), nums2.end());
48+
49+
unordered_map<int, int> counter1;
50+
unordered_map<int, int> counter2;
51+
for (auto &x : nums1) {
52+
counter1[x] += 1;
53+
}
54+
55+
for (auto &x: nums2) {
56+
counter2[x] += 1;
57+
}
58+
59+
int res = 0;
60+
for (auto &[x, c]: counter1) {
61+
res += count(nums2, counter2, 1LL * x * x) * c;
62+
}
63+
64+
for (auto &[x, c]: counter2) {
65+
res += count(nums1, counter1, 1LL * x * x) * c;
66+
}
67+
68+
return res;
69+
}
70+
71+
int count(vector<int>& nums, unordered_map<int, int> &counter, long long target) {
72+
int l = 0;
73+
int r = nums.size() - 1;
74+
int res = 0;
75+
while (l < r) {
76+
long long tmp = 1LL * nums[l] * nums[r];
77+
int left = counter[nums[l]];
78+
int right = counter[nums[r]];
79+
if (tmp == target) {
80+
if (nums[l] == nums[r]) {
81+
res += left * (left - 1) / 2;
82+
break;
83+
} else {
84+
res += left * right;
85+
l += left;
86+
r -= right;
87+
}
88+
} else if (tmp > target) {
89+
r -= right;
90+
} else {
91+
l += left;
92+
}
93+
}
94+
95+
return res;
96+
}
97+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Tag: Array, Hash Table, Math, Two Pointers
2+
# Time: O(NM)
3+
# Space: O(N + M)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
8+
#
9+
# Type 1: Triplet (i, j, k) if nums1[i]2 == nums2[j] * nums2[k] where 0 <= i < nums1.length and 0 <= j < k < nums2.length.
10+
# Type 2: Triplet (i, j, k) if nums2[i]2 == nums1[j] * nums1[k] where 0 <= i < nums2.length and 0 <= j < k < nums1.length.
11+
#
12+
#  
13+
# Example 1:
14+
#
15+
# Input: nums1 = [7,4], nums2 = [5,2,8,9]
16+
# Output: 1
17+
# Explanation: Type 1: (1, 1, 2), nums1[1]2 = nums2[1] * nums2[2]. (42 = 2 * 8).
18+
#
19+
# Example 2:
20+
#
21+
# Input: nums1 = [1,1], nums2 = [1,1,1]
22+
# Output: 9
23+
# Explanation: All Triplets are valid, because 12 = 1 * 1.
24+
# Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2). nums1[i]2 = nums2[j] * nums2[k].
25+
# Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]2 = nums1[j] * nums1[k].
26+
#
27+
# Example 3:
28+
#
29+
# Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
30+
# Output: 2
31+
# Explanation: There are 2 valid triplets.
32+
# Type 1: (3,0,2). nums1[3]2 = nums2[0] * nums2[2].
33+
# Type 2: (3,0,1). nums2[3]2 = nums1[0] * nums1[1].
34+
#
35+
#  
36+
# Constraints:
37+
#
38+
# 1 <= nums1.length, nums2.length <= 1000
39+
# 1 <= nums1[i], nums2[i] <= 105
40+
#
41+
#
42+
43+
from collections import Counter
44+
class Solution:
45+
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
46+
res = 0
47+
nums1.sort()
48+
nums2.sort()
49+
counter1 = Counter(nums1)
50+
counter2 = Counter(nums2)
51+
for x in counter1:
52+
res += self.count(nums2, counter2, x * x) * counter1[x]
53+
54+
for x in counter2:
55+
res += self.count(nums1, counter1, x * x) * counter2[x]
56+
57+
return res
58+
59+
def count(self, nums: list, counter: dict, target: int) -> int:
60+
l = 0
61+
r = len(nums) - 1
62+
res = 0
63+
while l < r:
64+
tmp = nums[l] * nums[r]
65+
left = counter[nums[l]]
66+
right = counter[nums[r]]
67+
68+
if tmp == target:
69+
if nums[l] == nums[r]:
70+
res += left * (left - 1) // 2
71+
break
72+
else:
73+
res += left * right
74+
l += left
75+
r -= right
76+
elif tmp > target:
77+
r -= right
78+
else:
79+
l += left
80+
81+
return res

leetcode/16.3sum-closest.cpp

+44-22
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,59 @@
1+
// Tag: Array, Two Pointers, Sorting
2+
// Time: O(N^2)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
8+
// Return the sum of the three integers.
9+
// You may assume that each input would have exactly one solution.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: nums = [-1,2,1,-4], target = 1
14+
// Output: 2
15+
// Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
16+
//
17+
// Example 2:
18+
//
19+
// Input: nums = [0,0,0], target = 1
20+
// Output: 0
21+
// Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
22+
//
23+
//  
24+
// Constraints:
25+
//
26+
// 3 <= nums.length <= 500
27+
// -1000 <= nums[i] <= 1000
28+
// -104 <= target <= 104
29+
//
30+
//
31+
132
class Solution {
233
public:
334
int threeSumClosest(vector<int>& nums, int target) {
4-
5-
int result = nums[0] + nums[1] + nums[3];
35+
int n = nums.size();
36+
int result = nums[0] + nums[1] + nums[2];
637
sort(nums.begin(), nums.end());
738

8-
for (auto i = 0; i < nums.size(); ++i)
9-
{
10-
int left = i + 1;
11-
int right = nums.size() - 1;
12-
while (left < right)
13-
{
14-
int tmp = nums[i] + nums[left] + nums[right];
15-
if (abs(target - result) > abs(target - tmp))
16-
{
39+
for (int k = 2; k < n; k++) {
40+
int l = 0;
41+
int r = k - 1;
42+
while (l < r) {
43+
int tmp = nums[l] + nums[r] + nums[k];
44+
if (abs(target - result) > abs(target - tmp)) {
1745
result = tmp;
1846
}
1947

20-
if (tmp < target)
21-
{
22-
left ++;
23-
}
24-
else if(tmp > target)
25-
{
26-
right --;
27-
}
28-
else
29-
{
48+
if (tmp == target) {
3049
break;
50+
} else if(tmp > target){
51+
r -= 1;
52+
} else {
53+
l += 1;
3154
}
3255
}
3356
}
34-
3557
return result;
3658
}
3759
};

leetcode/16.3sum-closest.py

+44-51
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
1-
class Solution:
2-
def threeSumClosest(self, nums: List[int], target: int) -> int:
3-
res = float('inf')
4-
nums.sort()
5-
6-
for i in range(len(nums) - 2):
7-
a = nums[i]
8-
left = i + 1
9-
right = len(nums) - 1
10-
11-
while left < right:
12-
threeSum = a + nums[left] + nums[right]
13-
if abs(threeSum - target) < abs(res - target):
14-
res = threeSum
15-
16-
if threeSum == target:
17-
break
18-
elif threeSum > target:
19-
right -= 1
20-
else:
21-
left += 1
22-
23-
return res
1+
# Tag: Array, Two Pointers, Sorting
2+
# Time: O(N^2)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
246

7+
# Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
8+
# Return the sum of the three integers.
9+
# You may assume that each input would have exactly one solution.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: nums = [-1,2,1,-4], target = 1
14+
# Output: 2
15+
# Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
16+
#
17+
# Example 2:
18+
#
19+
# Input: nums = [0,0,0], target = 1
20+
# Output: 0
21+
# Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
22+
#
23+
#  
24+
# Constraints:
25+
#
26+
# 3 <= nums.length <= 500
27+
# -1000 <= nums[i] <= 1000
28+
# -104 <= target <= 104
29+
#
30+
#
2531

26-
class Solution2:
32+
class Solution:
2733
def threeSumClosest(self, nums: List[int], target: int) -> int:
28-
closest = float('inf')
34+
n = len(nums)
2935
nums.sort()
36+
res = float('inf')
37+
for k in range(2, n):
38+
l = 0
39+
r = k - 1
40+
while l < r:
41+
tmp = nums[l] + nums[r] + nums[k]
42+
if abs(res - target) > abs(tmp - target):
43+
res = tmp
3044

31-
for i in range(len(nums) - 2):
32-
33-
if i > 0 and nums[i] == nums[i - 1]:
34-
continue
35-
36-
a = nums[i]
37-
left = i + 1
38-
right = len(nums) - 1
39-
40-
while left < right:
41-
42-
threeSum = a + nums[left] + nums[right]
43-
44-
if abs(threeSum - target) < abs(closest - target):
45-
closest = threeSum
46-
47-
if threeSum == target:
45+
if tmp == target:
4846
break
49-
elif threeSum > target:
50-
while (left < right and nums[right] == nums[right - 1]): #remove duplicate
51-
right -= 1
52-
right -= 1
47+
elif tmp > target:
48+
r -= 1
5349
else:
54-
while (left < right and nums[left] == nums[left + 1]): #remove duplicate
55-
left += 1
56-
left += 1
57-
58-
return closest
50+
l += 1
51+
return res

leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Space: O(1)
44
// Ref: -
55
// Note: -
6+
// Video: https://youtu.be/Ck_lb4dp6R4
67

78
// You are given an integer array nums and a positive integer k.
89
// Return the number of subarrays where the maximum element of nums appears at least k times in that subarray.

leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.py

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Space: O(1)
44
# Ref: -
55
# Note: -
6+
# Video: https://youtu.be/Ck_lb4dp6R4
67

78
# You are given an integer array nums and a positive integer k.
89
# Return the number of subarrays where the maximum element of nums appears at least k times in that subarray.

0 commit comments

Comments
 (0)