description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
m = len(nums2)
nums1prod = [[(nums1[i] * nums1[j]) for i in range(n)] for j in range(n)]
nums2prod = [[(nums2[i] * nums2[j]) for i in range(m)] for j in range(m)]
nums1sq = [(x**2) for x in nums1]
nums2sq = [(x**2) for x in nums2]
hm1sq = Counter(nums1sq)
hm2sq = Counter(nums2sq)
trip = 0
for i in range(n):
for j in range(i + 1, n):
if nums1prod[i][j] in hm2sq:
trip += hm2sq[nums1prod[i][j]]
for i in range(m):
for j in range(i + 1, m):
if nums2prod[i][j] in hm1sq:
trip += hm1sq[nums2prod[i][j]]
return trip
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
ans = 0
n = len(nums1)
m = len(nums2)
sq1 = list(map(lambda x: x**2, nums1))
sq2 = list(map(lambda x: x**2, nums2))
d1 = dict()
d2 = dict()
for num1 in list(set(nums1)):
d1[num1] = dict()
for i in range(n):
if nums1[i] == num1:
if i == 0:
d1[num1][i] = 1
else:
d1[num1][i] = d1[num1][i - 1] + 1
elif i == 0:
d1[num1][i] = 0
else:
d1[num1][i] = d1[num1][i - 1]
for num2 in list(set(nums2)):
d2[num2] = dict()
for i in range(m):
if nums2[i] == num2:
if i == 0:
d2[num2][i] = 1
else:
d2[num2][i] = d2[num2][i - 1] + 1
elif i == 0:
d2[num2][i] = 0
else:
d2[num2][i] = d2[num2][i - 1]
for i in range(n):
for j in range(m - 1):
tmp = sq1[i] // nums2[j]
if sq1[i] % nums2[j] == 0:
if tmp not in d2:
continue
ans += d2[tmp][m - 1] - d2[tmp][j]
for j in range(m):
for i in range(n - 1):
tmp = sq2[j] // nums1[i]
if sq2[j] % nums1[i] == 0:
if tmp not in d1:
continue
ans += d1[tmp][n - 1] - d1[tmp][i]
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
def cal(nums1, nums2):
ans = 0
for a in nums1:
d = collections.defaultdict(lambda: 0)
sq = a * a
for ib in range(0, len(nums2)):
b = nums2[ib]
if b == 0:
if a == 0:
ans += ib
else:
continue
if sq % b != 0:
continue
else:
c = sq / b
if c in d:
ans += d[c]
d[b] += 1
print(ans)
return ans
return cal(nums1, nums2) + cal(nums2, nums1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def wrapper(self, toSquare, toProd):
table = {}
for integer in toSquare:
squared = integer**2
if squared not in table:
table[squared] = 1
else:
table[squared] += 1
counter = 0
i = 0
while i < len(toProd):
j = i + 1
while j < len(toProd):
product = toProd[i] * toProd[j]
if product in table:
counter += table[product]
j += 1
i += 1
return counter
def numTriplets1(self, nums1: List[int], nums2: List[int]) -> int:
counter = 0
counter += self.wrapper(nums1, nums2)
counter += self.wrapper(nums2, nums1)
return counter
def get_tables(self, nums):
squares = {}
products = {}
i = 0
while i != len(nums):
j = i
while j != len(nums):
product_or_square = nums[i] * nums[j]
if i == j:
if product_or_square not in squares:
squares[product_or_square] = 1
else:
squares[product_or_square] += 1
elif product_or_square not in products:
products[product_or_square] = 1
else:
products[product_or_square] += 1
j += 1
i += 1
return products, squares
def get_counts(self, products, squares):
counter = 0
if len(products) > len(squares):
for square in squares:
if square in products:
counter += squares[square] * products[square]
else:
for product in products:
if product in squares:
counter += products[product] * squares[product]
return counter
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
products1, squares1 = self.get_tables(nums1)
products2, squares2 = self.get_tables(nums2)
counter = 0
counter += self.get_counts(products1, squares2)
counter += self.get_counts(products2, squares1)
return counter
|
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
def fun(arr):
res = {}
for i in range(len(arr) - 1):
for j in range(i + 1, len(arr)):
if arr[i] * arr[j] in res.keys():
res[arr[i] * arr[j]] += 1
else:
res[arr[i] * arr[j]] = 1
return res
x = fun(nums1)
y = fun(nums2)
res = 0
for i in range(len(nums1)):
if nums1[i] ** 2 in y.keys():
print(nums1[i])
res += y[nums1[i] ** 2]
for i in range(len(nums2)):
if nums2[i] ** 2 in x.keys():
res += x[nums2[i] ** 2]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
def helper(nums, lookup):
res = 0
for maxNum in nums:
prod = maxNum * maxNum
for n in lookup.keys():
if n <= maxNum and not prod % n:
m = prod // n
res += (
lookup[n] * (lookup[n] - 1) // 2
if m == n
else lookup[m] * lookup[n]
)
return res
return helper(nums1, collections.Counter(nums2)) + helper(
nums2, collections.Counter(nums1)
)
def numTriplets1(self, nums1: List[int], nums2: List[int]) -> int:
lookup1, lookup2 = collections.defaultdict(int), collections.defaultdict(int)
for n in nums1:
lookup1[n] += 1
for n in nums2:
lookup2[n] += 1
res = 0
for k in [0, 1]:
nums = nums1 if not k else nums2
lookup = lookup1 if k else lookup2
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
prod = nums[i] * nums[j]
k = int(math.sqrt(prod))
if k * k == prod and k in lookup:
res += lookup[k]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
m = len(nums1)
n = len(nums2)
mat2 = [[(0) for i in range(n)] for i in range(n)]
def fillUpper(m, nums):
mat = [[(0) for i in range(m)] for i in range(m)]
mapp1 = defaultdict(int)
for i in range(m - 1):
for j in range(i + 1, len(mat)):
mapp1[nums[i] * nums[j]] += 1
return mapp1
mat1 = fillUpper(m, nums1)
mat2 = fillUpper(n, nums2)
res = 0
for i in nums1:
c = i**2
if c in mat2:
res += mat2[c]
for i in nums2:
c = i**2
if c in mat1:
res += mat1[c]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
rev, n1, n2 = 0, Counter(nums1), Counter(nums2)
for i in range(len(nums1) - 1):
for j in range(i + 1, len(nums1)):
t = (nums1[i] * nums1[j]) ** (1 / 2)
if t == int(t) and t in n2:
rev += n2[t]
for i in range(len(nums2) - 1):
for j in range(i + 1, len(nums2)):
t = (nums2[i] * nums2[j]) ** (1 / 2)
if t == int(t) and t in n1:
rev += n1[t]
return rev
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
m1, m2 = collections.Counter(), collections.Counter()
result = 0
for i in range(len(nums1) - 1):
for j in range(i + 1, len(nums1)):
n = sqrt(nums1[i] * nums1[j])
if n % 1 == 0:
m1[n] += 1
for i in range(len(nums2) - 1):
for j in range(i + 1, len(nums2)):
n = sqrt(nums2[i] * nums2[j])
if n % 1 == 0:
m2[n] += 1
for num in nums1:
result += m2[num]
for num in nums2:
result += m1[num]
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
comp1, comp2 = defaultdict(set), defaultdict(set)
if not nums1 or not nums2:
return 0
n, m = len(nums1), len(nums2)
ans = 0
c1, c2 = Counter([(num**2) for num in nums1]), Counter(
[(num**2) for num in nums2]
)
for i in range(n):
for j in range(i + 1, n):
prod = nums1[i] * nums1[j]
ans += c2[prod]
for i in range(m):
for j in range(i + 1, m):
prod = nums2[i] * nums2[j]
ans += c1[prod]
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
def count(nums1, nums2):
pairs = []
N1, N2 = len(nums1), len(nums2)
result = 0
for i in range(N2):
for j in range(i + 1, N2):
pairs.append((nums2[i], nums2[j]))
Ns = collections.Counter(nums1)
for u, v in pairs:
if (u * v) ** 0.5 in Ns:
result += Ns[(u * v) ** 0.5]
return result
return count(nums1, nums2) + count(nums2, nums1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
dic1, dic2 = collections.Counter(nums1), collections.Counter(nums2)
ans = 0
m, n = len(nums1), len(nums2)
for i in range(m):
for j in range(i + 1, m):
target = nums1[i] * nums1[j]
sq = math.sqrt(target)
if sq * sq == target:
ans += dic2[sq]
for i in range(n):
for j in range(i + 1, n):
target = nums2[i] * nums2[j]
sq = math.sqrt(target)
if sq * sq == target:
ans += dic1[sq]
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, a: List[int], b: List[int]) -> int:
n = len(a)
m = len(b)
pro1 = {}
pro2 = {}
for i in range(n):
for j in range(n):
if i != j:
pro1[a[i] * a[j]] = (
pro1[a[i] * a[j]] + 1 if a[i] * a[j] in pro1 else 1
)
for i in range(m):
for j in range(m):
if i != j:
pro2[b[i] * b[j]] = (
pro2[b[i] * b[j]] + 1 if b[i] * b[j] in pro2 else 1
)
ans = 0
for i in range(n):
if a[i] ** 2 in pro2:
ans += pro2[a[i] ** 2] // 2
for i in range(m):
if b[i] ** 2 in pro1:
ans += pro1[b[i] ** 2] // 2
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
n1 = [(x * x) for x in nums1]
n2 = [(x * x) for x in nums2]
dnum1 = collections.Counter(n1)
dnum2 = collections.Counter(n2)
ans = 0
nums1.sort()
nums2.sort()
m1 = max(dnum1.keys())
m2 = max(dnum2.keys())
for i in range(len(nums1) - 1):
for j in range(i + 1, len(nums1)):
comb = nums1[i] * nums1[j]
if comb in dnum2:
ans += dnum2[nums1[i] * nums1[j]]
if comb > m2:
break
for i in range(len(nums2) - 1):
for j in range(i + 1, len(nums2)):
comb = nums2[i] * nums2[j]
if comb in dnum1:
ans += dnum1[nums2[i] * nums2[j]]
if comb > m1:
break
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
def helper(nums1, nums2):
s = [(n * n) for n in nums1]
count = 0
d = collections.defaultdict(list)
for i in range(len(nums2)):
d[nums2[i]].append(i)
for n in s:
for i in range(len(nums2)):
if n % nums2[i] == 0 and n // nums2[i] in d:
if nums2[i] == n // nums2[i]:
count += len(d[n // nums2[i]]) - 1
else:
count += len(d[n // nums2[i]])
return count // 2
return helper(nums1, nums2) + helper(nums2, nums1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
ret = 0
m, n = len(nums1), len(nums2)
_map1, _map2 = Counter(nums1), Counter(nums2)
for i in range(m):
nums2_map = {**_map2}
for j in range(n):
nums2_map[nums2[j]] -= 1
target = nums1[i] * nums1[i] / nums2[j]
if nums2_map.get(target, 0) > 0:
ret += nums2_map.get(target, 0)
for i in range(n):
nums1_map = {**_map1}
for j in range(m):
nums1_map[nums1[j]] -= 1
target = nums2[i] * nums2[i] / nums1[j]
if nums1_map.get(target, 0) > 0:
ret += nums1_map.get(target, 0)
return ret
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
d = {}
ct = 0
M = len(nums1)
N = len(nums2)
for i in range(M):
if nums1[i] * nums1[i] in d:
d[nums1[i] * nums1[i]] += 1
else:
d[nums1[i] * nums1[i]] = 1
for i in range(N):
for j in range(i + 1, N):
if nums2[i] * nums2[j] in d:
ct += d[nums2[i] * nums2[j]]
d = {}
for i in range(N):
if nums2[i] * nums2[i] in d:
d[nums2[i] * nums2[i]] += 1
else:
d[nums2[i] * nums2[i]] = 1
for i in range(M):
for j in range(i + 1, M):
if nums1[i] * nums1[j] in d:
ct += d[nums1[i] * nums1[j]]
return ct
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
|
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
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.
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.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
Explanation: Type 1: (1,1,2), nums1[1]^2 = nums2[1] * nums2[2]. (4^2 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
Explanation: All Triplets are valid, because 1^2 = 1 * 1.
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].
Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]^2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
Explanation: There are 2 valid triplets.
Type 1: (3,0,2). nums1[3]^2 = nums2[0] * nums2[2].
Type 2: (3,0,1). nums2[3]^2 = nums1[0] * nums1[1].
Example 4:
Input: nums1 = [4,7,9,11,23], nums2 = [3,5,1024,12,18]
Output: 0
Explanation: There are no valid triplets.
Constraints:
1 <= nums1.length, nums2.length <= 1000
1 <= nums1[i], nums2[i] <= 10^5
|
class Solution:
def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
def count(n1, n2):
l2 = len(n2)
cnt = Counter()
for k in range(l2):
for j in range(k):
v = n2[k] * n2[j]
cnt[v] += 1
ans = 0
for x in n1:
ans += cnt[x * x]
return ans
return count(nums1, nums2) + count(nums2, nums1)
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
res = []
while A and B:
start1, end1 = A[0][0], A[0][1]
start2, end2 = B[0][0], B[0][1]
if end1 <= end2:
if start2 <= start1:
res.append([start1, end1])
elif start2 <= end1:
res.append([start2, end1])
A = A[1:]
else:
if start1 <= start2:
res.append([start2, end2])
elif start1 <= end2:
res.append([start1, end2])
B = B[1:]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def overlap(self, lst):
lst.sort(key=lambda x: x[0])
if lst[0][1] >= lst[1][0]:
return True
return False
def add_data(self, start_a, end_a, start_b, end_b, res):
if self.overlap([(start_a, end_a), (start_b, end_b)]):
start, end = max(start_a, start_b), min(end_a, end_b)
if (start, end) not in res:
res.append((start, end))
return res
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
if not A or not B:
return []
start_a, end_a = A.pop(0)
start_b, end_b = B.pop(0)
res = []
while A and B:
res = self.add_data(start_a, end_a, start_b, end_b, res)
if end_a < end_b:
start_a, end_a = A.pop(0)
else:
start_b, end_b = B.pop(0)
while A:
res = self.add_data(start_a, end_a, start_b, end_b, res)
start_a, end_a = A.pop(0)
while B:
res = self.add_data(start_a, end_a, start_b, end_b, res)
start_b, end_b = B.pop(0)
res = self.add_data(start_a, end_a, start_b, end_b, res)
return res
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
intervals = deque()
while A and B:
a, b = A[-1], B[-1]
x, y = max(a[0], b[0]), min(a[1], b[1])
if x <= y:
intervals.appendleft([x, y])
(A if a > b else B).pop()
return intervals
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
extremum = [item for t in A for item in t] + [item for t in B for item in t]
extremum.sort()
counter_a = 0
counter_b = 0
next_a = A[counter_a] if A else None
next_b = B[counter_b] if B else None
max_a = A[-1][1] if A else None
max_b = B[-1][1] if B else None
intersections = {}
def intersections_append(k: (int, int), v: int):
if k in intersections:
intersections[k].append(v)
if len(intersections[k]) > 2:
tmp = list(set(intersections[k]))
tmp.sort()
intersections[k] = tmp
else:
intersections[k] = [v]
self.k1 = None
self.k2 = None
self.res2 = []
def intersections_append2(k: (int, int), v: int):
k1 = self.k1
k2 = self.k2
if k1 is not None and k2 is not None and k[0] == k1 and k[1] == k2:
if self.res2:
self.res2[-1].append(v)
if len(self.res2[-1]) > 2:
self.res2[-1] = sorted(set(self.res2[-1]))
else:
self.res2.append([v])
else:
self.k1 = k[0]
self.k2 = k[1]
self.res2.append([v])
res = []
for i in extremum:
while max_a and i <= max_a and i > next_a[1]:
counter_a += 1
next_a = A[counter_a] if counter_a < len(A) else None
while max_b and i <= max_b and i > next_b[1]:
counter_b += 1
next_b = B[counter_b] if counter_b < len(B) else None
if (
next_a
and next_b
and next_a[0] <= i <= next_a[1]
and next_b[0] <= i <= next_b[1]
):
intersections_append((counter_a, counter_b), i)
intersections_append2((counter_a, counter_b), i)
return self.res2
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER NUMBER NONE ASSIGN VAR VAR VAR NUMBER NUMBER NONE ASSIGN VAR DICT FUNC_DEF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR LIST FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE VAR NONE VAR NUMBER VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NONE WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NONE IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
if not A or not B:
return []
ans = []
i = j = 0
while i < len(A) and j < len(B):
lo = max(A[i][0], B[j][0])
hi = min(A[i][1], B[j][1])
if lo <= hi:
ans.append([lo, hi])
if B[j][1] > A[i][1]:
i += 1
elif B[j][1] < A[i][1]:
j += 1
else:
i += 1
j += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
time_points = list()
for interval in A + B:
time_points.append((interval[0], +1))
time_points.append((interval[1] + 1, -1))
sorted_points = sorted(time_points, key=lambda x: x[0])
s = 0
intersect = []
for i in range(len(sorted_points)):
s += sorted_points[i][1]
if (
i != len(sorted_points) - 1
and sorted_points[i][0] == sorted_points[i + 1][0]
):
continue
if s == 2:
intersect.append((sorted_points[i][0], True))
else:
intersect.append((sorted_points[i][0], False))
i = 0
ans = list()
while i < len(intersect):
while not intersect[i][1]:
i += 1
if i == len(intersect):
return ans
ans.append([intersect[i][0], intersect[i + 1][0] - 1])
i += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
if not A or not B:
return []
MergedList = A + B
MergedList.sort()
result = []
for i in range(len(MergedList)):
if i == 0:
continue
if MergedList[i][0] <= MergedList[i - 1][1]:
result.append(
[
max(MergedList[i - 1][0], MergedList[i][0]),
min(MergedList[i - 1][1], MergedList[i][1]),
]
)
MergedList[i][1] = max(MergedList[i - 1][1], MergedList[i][1])
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
self.k1 = None
self.k2 = None
self.res2 = []
def intersections_append2(k: (int, int), v: int):
k1 = self.k1
k2 = self.k2
if k1 is not None and k2 is not None and k[0] == k1 and k[1] == k2:
if self.res2:
self.res2[-1].append(v)
if len(self.res2[-1]) > 2:
self.res2[-1] = sorted(set(self.res2[-1]))
else:
self.res2.append([v])
else:
self.k1 = k[0]
self.k2 = k[1]
self.res2.append([v])
def get_next_items(lft_idx, rgt_idx):
lft_idx_outer = int(lft_idx / 2)
lft_idx_inner = lft_idx % 2
rgt_idx_outer = int(rgt_idx / 2)
rgt_idx_inner = rgt_idx % 2
lft = A[lft_idx_outer][lft_idx_inner] if lft_idx_outer < len(A) else None
rgt = B[rgt_idx_outer][rgt_idx_inner] if rgt_idx_outer < len(B) else None
lft_full = (
A[lft_idx_outer] if lft_idx_outer < len(A) else A[lft_idx_outer - 1]
)
rgt_full = (
B[rgt_idx_outer] if rgt_idx_outer < len(B) else B[rgt_idx_outer - 1]
)
return (
lft,
rgt,
lft_full,
rgt_full,
min(lft_idx_outer, len(A) - 1),
min(rgt_idx_outer, len(B) - 1),
)
u = 0, 0
if u[0] >= len(A) or u[0] >= len(B):
return []
i_a, i_b, next_a2, next_b2, counter_a, counter_b = get_next_items(u[0], u[1])
redo_step = True
while not (i_a is None and i_b is None):
if i_a is not None and i_b is None:
i, u = i_a, (u[0] + 1, u[1])
elif i_a is None and i_b is not None:
i, u = i_b, (u[0], u[1] + 1)
elif i_a < i_b:
i, u = i_a, (u[0] + 1, u[1])
elif i_a > i_b:
i, u = i_b, (u[0], u[1] + 1)
elif i_a == i_b and redo_step:
i, u = i_a, (u[0], u[1])
redo_step = False
elif i_a == i_b and not redo_step:
i, u = i_a, (u[0] + 1, u[1] + 1)
redo_step = True
if (
next_a2 is not None
and next_b2 is not None
and next_a2[0] <= i <= next_a2[1]
and next_b2[0] <= i <= next_b2[1]
):
intersections_append2((counter_a, counter_b), i)
i_a, i_b, next_a2, next_b2, counter_a, counter_b = get_next_items(
u[0], u[1]
)
return self.res2
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR LIST FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE VAR NONE VAR NUMBER VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NONE VAR NONE IF VAR NONE VAR NONE ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NONE VAR NONE ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NONE VAR NONE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
i = 0
j = 0
ans = []
while i < len(A) and j < len(B):
if A[i][0] >= B[j][0] and A[i][1] <= B[j][1]:
ans.append([A[i][0], A[i][1]])
i += 1
elif B[j][0] >= A[i][0] and B[j][1] <= A[i][1]:
ans.append([B[j][0], B[j][1]])
j += 1
elif A[i][1] >= B[j][0] and A[i][0] <= B[j][0]:
ans.append([B[j][0], A[i][1]])
i += 1
elif B[j][1] >= A[i][0] and B[j][0] <= A[i][0]:
ans.append([A[i][0], B[j][1]])
j += 1
elif B[j][1] < A[i][0]:
j += 1
elif A[i][1] < B[j][0]:
i += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
state = [False, False]
A_index = [0, 0]
B_index = [0, 0]
result = []
def get_event(events, index):
if index[0] >= len(events):
return -1
return events[index[0]][index[1]]
while True:
event_a = get_event(A, A_index), A_index[1]
event_b = get_event(B, B_index), B_index[1]
if event_a[0] < 0 or event_b[0] < 0:
break
event = min(event_a[0], event_b[0])
is_already_in_progress = all(state)
if event_a < event_b:
if A_index[1] == 0:
state[0] = 1
A_index[1] = 1
else:
state[0] = 0
A_index[0] += 1
A_index[1] = 0
elif B_index[1] == 0:
state[1] = 1
B_index[1] = 1
else:
state[1] = 0
B_index[0] += 1
B_index[1] = 0
if is_already_in_progress:
result[-1][1] = event
elif all(state):
result.append([event, -1])
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR ASSIGN VAR NUMBER NUMBER VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
i = 0
j = 0
result = []
while i < len(A) and j < len(B):
if self.overlap(A[i], B[j]):
result.append(self.intersection(A[i], B[j]))
if A[i][1] <= B[j][1]:
i += 1
else:
j += 1
return result
def overlap(self, i1, i2):
return any(
[
i1[0] <= i2[0] <= i1[1],
i1[0] <= i2[1] <= i1[1],
i2[0] <= i1[0] <= i2[1],
i2[0] <= i1[1] <= i2[1],
]
)
def intersection(self, i1, i2):
return [max(i1[0], i2[0]), min(i1[1], i2[1])]
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_DEF RETURN LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
if not A or not B:
return []
m, n = len(A), len(B)
result = []
i = j = 0
while i < m and j < n:
start = max(A[i][0], B[j][0])
end = min(A[i][1], B[j][1])
if start <= end:
result.append([start, end])
if A[i][1] < B[j][1]:
i += 1
else:
j += 1
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
counter_a = 0
counter_b = 0
next_a = A[counter_a] if A else None
next_b = B[counter_b] if B else None
max_a = A[-1][1] if A else None
max_b = B[-1][1] if B else None
self.k1 = None
self.k2 = None
self.res2 = []
def intersections_append2(k: (int, int), v: int):
k1 = self.k1
k2 = self.k2
if k1 is not None and k2 is not None and k[0] == k1 and k[1] == k2:
if self.res2:
self.res2[-1].append(v)
if len(self.res2[-1]) > 2:
self.res2[-1] = sorted(set(self.res2[-1]))
else:
self.res2.append([v])
else:
self.k1 = k[0]
self.k2 = k[1]
self.res2.append([v])
def get_next_items(lft_idx, rgt_idx):
lft_idx_outer = int(lft_idx / 2)
lft_idx_inner = lft_idx % 2
rgt_idx_outer = int(rgt_idx / 2)
rgt_idx_inner = rgt_idx % 2
lft = A[lft_idx_outer][lft_idx_inner] if lft_idx_outer < len(A) else None
rgt = B[rgt_idx_outer][rgt_idx_inner] if rgt_idx_outer < len(B) else None
return lft, rgt
u = 0, 0
i_a, i_b = get_next_items(u[0], u[1])
while i_a is not None and i_b is not None:
i_a, i_b = get_next_items(u[0], u[1])
if i_a is not None and (i_b is None or i_a <= i_b):
i = i_a
u = u[0] + 1, u[1]
else:
i = i_b
u = u[0], u[1] + 1
while max_a and i <= max_a and i > next_a[1]:
counter_a += 1
next_a = A[counter_a] if counter_a < len(A) else None
while max_b and i <= max_b and i > next_b[1]:
counter_b += 1
next_b = B[counter_b] if counter_b < len(B) else None
if (
next_a
and next_b
and next_a[0] <= i <= next_a[1]
and next_b[0] <= i <= next_b[1]
):
intersections_append2((counter_a, counter_b), i)
return self.res2
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER NUMBER NONE ASSIGN VAR VAR VAR NUMBER NUMBER NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR LIST FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE VAR NONE VAR NUMBER VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NONE RETURN VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR NONE VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NONE VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NONE WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NONE IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
res3 = []
self.k1 = None
self.k2 = None
self.res2 = []
def intersections_append2(k: (int, int), v: int):
k1 = self.k1
k2 = self.k2
if k1 is not None and k2 is not None and k[0] == k1 and k[1] == k2:
if self.res2:
self.res2[-1].append(v)
else:
self.res2.append([v])
else:
self.k1 = k[0]
self.k2 = k[1]
self.res2.append([v])
def get_next_items(lft_idx, rgt_idx):
lft_idx_outer = int(lft_idx / 2)
lft_idx_inner = lft_idx % 2
rgt_idx_outer = int(rgt_idx / 2)
rgt_idx_inner = rgt_idx % 2
lft = A[lft_idx_outer][lft_idx_inner] if lft_idx_outer < len(A) else None
rgt = B[rgt_idx_outer][rgt_idx_inner] if rgt_idx_outer < len(B) else None
lft_full = (
A[lft_idx_outer] if lft_idx_outer < len(A) else A[lft_idx_outer - 1]
)
rgt_full = (
B[rgt_idx_outer] if rgt_idx_outer < len(B) else B[rgt_idx_outer - 1]
)
return (
lft,
rgt,
lft_full,
rgt_full,
min(lft_idx_outer, len(A) - 1),
min(rgt_idx_outer, len(B) - 1),
)
u = 0, 0
a_idx = 0
b_idx = 0
if a_idx >= len(A) or b_idx >= len(B):
return []
prev_counter_a = None
prev_counter_b = None
i_a, i_b, next_a2, next_b2, counter_a, counter_b = get_next_items(a_idx, b_idx)
redo_step = True
while not (i_a is None and i_b is None):
if i_a is not None and i_b is None:
a_idx += 1
elif i_a is None and i_b is not None:
b_idx += 1
elif i_a < i_b:
a_idx += 1
elif i_a > i_b:
b_idx += 1
elif i_a == i_b and redo_step:
redo_step = False
elif i_a == i_b and not redo_step:
a_idx += 1
b_idx += 1
redo_step = True
i = min(k for k in [i_a, i_b] if k is not None)
if (
next_a2 is not None
and next_b2 is not None
and next_a2[0] <= i <= next_a2[1]
and next_b2[0] <= i <= next_b2[1]
):
if counter_a == prev_counter_a and counter_b == prev_counter_b:
res3[-1].append(i)
else:
res3.append([i])
prev_counter_a = counter_a
prev_counter_b = counter_b
i_a, i_b, next_a2, next_b2, counter_a, counter_b = get_next_items(
a_idx, b_idx
)
res4 = []
for x in res3:
if len(x) > 2:
tmp = sorted(list(set(x)))
res4.append(tmp)
else:
res4.append(x)
return res4
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR LIST FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE VAR NONE VAR NUMBER VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE VAR NONE IF VAR NONE VAR NONE VAR NUMBER IF VAR NONE VAR NONE VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR NONE IF VAR NONE VAR NONE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
result = []
while len(A) != 0 and len(B) != 0:
a_start, a_end = A[0]
b_start, b_end = B[0]
start = max(a_start, b_start)
end = min(a_end, b_end)
if start > end:
if start == a_start:
B = B[1:]
else:
A = A[1:]
else:
result.append([start, end])
if end == a_end:
B[0] = [start, b_end]
A = A[1:]
else:
A[0] = [start, a_end]
B = B[1:]
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR ASSIGN VAR NUMBER LIST VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER LIST VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
p1, p2 = 0, 0
res = []
while p1 < len(A) and p2 < len(B):
if not (B[p2][1] < A[p1][0] or A[p1][1] < B[p2][0]):
res.append((max(A[p1][0], B[p2][0]), min(A[p1][1], B[p2][1])))
if A[p1][1] < B[p2][1]:
p1 += 1
else:
p2 += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
NA = len(A)
NB = len(B)
if NA == 0 or NB == 0:
return []
pA = 0
pB = 0
res = []
while pA < NA and pB < NB:
sA = A[pA][0]
eA = A[pA][1]
sB = B[pB][0]
eB = B[pB][1]
if sA <= sB:
if eA >= sB:
res.append([sB, min(eA, eB)])
elif eB >= sA:
res.append([sA, min(eA, eB)])
if eA < eB:
pA += 1
elif eA == eB:
pA += 1
pB += 1
else:
pB += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
ans = []
if not A or not B:
return ans
cur_a = A.pop(0)
cur_b = B.pop(0)
while cur_a and cur_b:
if cur_a[0] <= cur_b[1] and cur_a[1] >= cur_b[0]:
ans.append([max(cur_a[0], cur_b[0]), min(cur_a[1], cur_b[1])])
old_a = cur_a
if cur_a[1] <= cur_b[1]:
cur_a = A.pop(0) if A else None
if old_a[1] >= cur_b[1]:
cur_b = B.pop(0) if B else None
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER NONE IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER NONE RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
res3 = []
a_idx = 0
b_idx = 0
if a_idx >= len(A) or b_idx >= len(B):
return []
redo_step = False
while a_idx < len(A) and b_idx < len(B):
next_a2 = A[a_idx]
next_b2 = B[b_idx]
i_a = next_a2[0]
i_b = next_b2[0]
if next_a2[1] < next_b2[1]:
a_idx += 1
else:
b_idx += 1
max_lft = max(k for k in [next_a2[0], next_b2[0]])
min_rgt = min(k for k in [next_a2[1], next_b2[1]])
if max_lft <= min_rgt:
res3.append([max_lft, min_rgt])
return res3
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR LIST VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
def intersection(a: List, b: List) -> List:
l = max(a[0], b[0])
r = min(a[1], b[1])
if l <= r:
return [l, r]
else:
return None
def union(a: List, b: List) -> List:
l = min(a[0], b[0])
r = max(a[1], b[1])
i = intersection(a, b)
if i:
return [l, r]
else:
return None
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
result = []
while A and B:
a = A[0]
b = B[0]
i = intersection(a, b)
if i:
result.append(i)
if a[1] < b[1]:
A.pop(0)
elif a[1] > b[1]:
B.pop(0)
else:
A.pop(0)
i = 1
while i < len(result):
u = union(result[i - 1], result[i])
if u:
result.pop(i - 1)
result.pop(i - 1)
result.insert(i - 1, u)
else:
i += 1
return result
|
FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN LIST VAR VAR RETURN NONE VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN LIST VAR VAR RETURN NONE VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
ite1 = 0
ite2 = 0
lenA = len(A)
lenB = len(B)
ans = []
while ite1 < lenA and ite2 < lenB:
curA = A[ite1]
curB = B[ite2]
s, e = max(curA[0], curB[0]), min(curA[1], curB[1])
if s <= e:
ans.append([s, e])
if curA[1] > curB[1]:
ite2 += 1
elif curA[1] < curB[1]:
ite1 += 1
else:
ite1 += 1
ite2 += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
i, j = 0, 0
res = []
A.append([math.inf, math.inf])
B.append([math.inf, math.inf])
while i < len(A) - 1 or j < len(B) - 1:
(i1_s, i1_e), (i2_s, i2_e) = A[i], B[j]
if i1_e >= i2_s:
if i1_s <= i2_e:
res.append([max(i1_s, i2_s), min(i1_e, i2_e)])
j += 1
else:
if j > 0:
j -= 1
i += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
if len(A) == 0 or len(B) == 0:
return []
a_start, a_end = A[0]
b_start, b_end = B[0]
start = max(a_start, b_start)
end = min(a_end, b_end)
if start > end:
if start == a_start:
return self.intervalIntersection(A, B[1:])
else:
return self.intervalIntersection(A[1:], B)
elif end == a_end:
B[0] = [start, b_end]
return [[start, end]] + self.intervalIntersection(A[1:], B)
else:
A[0] = [start, a_end]
return [[start, end]] + self.intervalIntersection(A, B[1:])
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER LIST VAR VAR RETURN BIN_OP LIST LIST VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER LIST VAR VAR RETURN BIN_OP LIST LIST VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR
|
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order.
Return the intersection of these two interval lists.
(Formally, a closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b. The intersection of two closed intervals is a set of real numbers that is either empty, or can be represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].)
Example 1:
Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Note:
0 <= A.length < 1000
0 <= B.length < 1000
0 <= A[i].start, A[i].end, B[i].start, B[i].end < 10^9
|
class Solution:
def intervalIntersection(
self, A: List[List[int]], B: List[List[int]]
) -> List[List[int]]:
i, j, ans = 0, 0, []
while i < len(A) and j < len(B):
a, b = A[i], B[j]
if not (a[1] < b[0] or a[0] > b[1]):
ans.append([max(a[0], b[0]), min(a[1], b[1])])
if a[1] >= b[1]:
j += 1
elif b[1] >= a[1]:
i += 1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for _ in range(int(input())):
input()
C = list(map(int, input().split()))
W = list(map(int, input().split()))
s, i = set([C[0]]), 0
c = mx = W[0]
for j in range(1, len(C)):
while C[j] in s:
mx = max(mx, c)
s.remove(C[i])
c -= W[i]
i += 1
s.add(C[j])
c += W[j]
print(max(mx, c))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
def solution(C, W):
low = 0
k = {}
k[C[low]] = 0
maxi = W[0]
ans = W[0]
for i in range(1, len(C)):
if C[i] not in k.keys():
ans += W[i]
k[C[i]] = i
elif k[C[i]] >= low:
j = low
while j <= k[C[i]]:
ans -= W[j]
j += 1
ans += W[i]
low = k[C[i]] + 1
k[C[i]] = i
else:
ans += W[i]
k[C[i]] = i
if maxi < ans:
maxi = ans
return maxi
T = int(input())
for t in range(T):
n = int(input())
C = list(map(int, input().split()))
W = list(map(int, input().split()))
print(solution(C, W))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
while t != 0:
n = int(input())
c = input()
c = [int(x) for x in c.split(" ")]
w = input()
w = [int(x) for x in w.split(" ")]
arr = [0] * n
max = w[0]
sum = w[0]
lo = 0
hi = 1
arr[c[0]] = 1
while hi < n:
if arr[c[hi]] == 0:
arr[c[hi]] = 1
sum = sum + w[hi]
hi = hi + 1
else:
arr[c[lo]] = 0
sum = sum - w[lo]
lo = lo + 1
if sum > max:
max = sum
print(max)
t = t - 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
from itertools import accumulate
red = lambda: list(map(int, input().split()))
for i in range(int(input())):
n = int(input())
l = red()
wi = list(map(int, ("0 " + input()).split()))
wis = list(accumulate(wi))
j = m = 0
s = set()
for i in range(n):
while j < n and l[i] in s:
s.remove(l[j])
j += 1
s.add(l[i])
m = max(m, wis[i + 1] - wis[j])
print(m)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for t in range(int(input())):
n = int(input())
cost = list(map(int, input().strip().split()))
weight = list(map(int, input().strip().split()))
s = [0] * (n + 1)
for i in range(n):
s[i + 1] = s[i] + weight[i]
ans = 0
i = 0
d = {}
for j in range(n):
if cost[j] not in d:
d[cost[j]] = -1
if d[cost[j]] >= i:
i = d[cost[j]] + 1
d[cost[j]] = j
ans = max(ans, s[j + 1] - s[i])
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
cases = int(input())
for x in range(cases):
nums = int(input())
array = list(map(int, input().split()))
weights = [0] + list(map(int, input().split()))
dictionary = {}
prev = 0
score = 0
for index in range(nums):
weights[index + 1] += weights[index]
item = array[index]
dict_index = dictionary.get(item)
if dict_index is not None:
prev = max(prev, dict_index)
score = max(score, weights[index + 1] - weights[prev])
dictionary[item] = index + 1
print(score)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
ans = 0
s = []
s.append(w[0])
for i in w[1:]:
s.append(s[-1] + i)
f = [-1] * (n + 1)
cs = 0
t = 0
for i in range(n):
if f[c[i]] == -1 or f[c[i]] < t:
cs += w[i]
f[c[i]] = i
else:
cs = s[i] - s[f[c[i]]]
t = f[c[i]] + 1
f[c[i]] = i
if cs > ans:
ans = cs
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
hash = []
for i in range(1000000):
hash.append(0)
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
w = list(map(int, input().split()))
for i in range(n):
hash[i] = 0
maxx = max(w)
l = 0
r = 1
s = w[0]
hash[a[0]] += 1
if r < n:
hash[a[r]] += 1
s += w[r]
while l < n and r < n:
if l == r:
r += 1
if r < n:
hash[a[r]] += 1
s += w[r]
elif hash[a[r]] < 2 and hash[a[l]] < 2:
if maxx < s:
maxx = s
r += 1
if r < n:
hash[a[r]] += 1
s += w[r]
else:
s -= w[l]
hash[a[l]] -= 1
l += 1
print(maxx)
|
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
def maxsegm(C, W):
d, s, t, i, r = {(0): 0}, set(), 0, 0, 0
for j in range(len(C)):
t += W[j]
d[j + 1] = t
while C[j] in s:
s.remove(C[i])
i += 1
s.add(C[j])
r = max(r, t - d[i])
return r
for _ in range(int(input())):
input()
C = list(map(int, input().split()))
W = list(map(int, input().split()))
print(maxsegm(C, W))
|
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR DICT NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
def find(n, c, w):
big = 0
sum = 0
sum1 = 0
j = 0
a = [(0) for i in range(n)]
m = {}
x = -1
while j < n:
sum1 += w[j]
a[j] = sum1
if c[j] not in m:
if x >= 0:
sum = a[j] - a[x]
else:
sum = a[j]
m[c[j]] = j
else:
if m[c[j]] > x:
x = m[c[j]]
sum = a[j] - a[x]
m[c[j]] = j
if sum > big:
big = sum
j += 1
return big
t = int(input())
for i in range(t):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
print(find(n, c, w))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for _ in range(int(input())):
N = int(input())
C = list(map(int, input().split()))
W = list(map(int, input().split()))
vi = [0] * N
M = 0
Sum = 0
L = 0
for i in range(N):
if vi[C[i]] == 0:
Sum += W[i]
vi[C[i]] = 1
else:
M = max(M, Sum)
Sum += W[i]
k = C.index(C[i], L, i)
Sum -= sum(W[L : k + 1])
for j in range(L, k):
vi[C[j]] = 0
L = k + 1
print(max(M, Sum))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for _ in range(int(input())):
n = int(input())
c = [int(i) for i in input().split()]
w = [int(i) for i in input().split()]
b = [(-1) for i in range(n)]
sry = [w[0]]
for i in range(1, n):
sry.append(sry[-1] + w[i])
ans = 0
sp = 0
d = 0
i = 0
while i < n:
if b[c[i]] == -1:
b[c[i]] = i
d += w[i]
ans = max(ans, d)
else:
l = b[c[i]]
d -= sry[l]
if sp > 0:
d += sry[sp - 1]
d += w[i]
ans = max(ans, d)
while sp < l:
b[c[sp]] = -1
sp += 1
sp += 1
b[c[i]] = i
i += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = [0]
for i in b:
x.append(x[-1] + i)
ans = [0] * n
d = {}
c = 0
for i in range(n):
try:
e = d[a[i]]
c = x[i + 1] - x[e + 1]
del d
d = {}
for j in range(e + 1, i + 1):
d[a[j]] = j
ans[i] = c
except:
d[a[i]] = i
c += b[i]
ans[i] = c
print(max(ans))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for test in range(t):
n = int(input())
c_array = [i for i in map(int, input().split())]
w_array = [i for i in map(int, input().split())]
seen = [(False) for i in range(n)]
result_sum = 0
i = 0
j = 0
max_sum = 0
while j < n:
if seen[c_array[j]] == True:
while c_array[i] != c_array[j]:
result_sum -= w_array[i]
seen[c_array[i]] = False
i += 1
seen[c_array[i]] = False
result_sum -= w_array[i]
i += 1
seen[c_array[j]] = True
result_sum += w_array[j]
if max_sum < result_sum:
max_sum = result_sum
j += 1
print(max_sum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for _ in range(t):
n = int(input())
C = list(map(int, input().split()))
W = list(map(int, input().split()))
Ws = [W[0]]
for i in range(1, n):
Ws.append(Ws[-1] + W[i])
S = [(0, 0)]
R = [-1] * n
R[C[0]] = 0
ini = 0
for i in range(1, n):
a = C[i]
if R[a] == -1:
S.append((ini, i))
R[a] = i
pass
elif R[a] < ini:
S.append((ini, i))
R[a] = i
else:
ini = R[a] + 1
S.append((ini, i))
R[a] = i
Su = []
for i in range(n):
a, b = S[i]
if a == 0:
Su.append(Ws[b])
else:
Su.append(Ws[b] - Ws[a - 1])
print(max(Su))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
while t:
t -= 1
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = temp = 0
d = [0] * n
i = j = k = 0
while i < n:
while j < n and d[a[j]] == 0:
d[a[j]] = 1
temp += b[j]
j += 1
ans = max(ans, temp)
f = 0
while k < n and j < n and a[j] != a[k]:
d[a[k]] = 0
temp -= b[k]
f = 1
k += 1
if k < n:
temp -= b[k]
if j < n:
temp += b[j]
ans = max(ans, temp)
i += 1
k += 1
j += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
a = [0] * 1000001
i = j = sum = ans = 0
while i < n:
while j < n and a[c[j]] == 0:
a[c[j]] = 1
sum += w[j]
j += 1
ans = max(sum, ans)
a[c[i]] = 0
sum -= w[i]
i += 1
print(max(ans, sum))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for _ in range(t):
n = int(input())
indexes = list(map(int, input().split()))
values = list(map(int, input().split()))
leftsum = [-1] * n
lastindex = [-1] * n
startindex = 0
tempindex = 0
maxsum = 0
tempsum = 0
totalsum = 0
for i in range(n):
if lastindex[indexes[i]] >= tempindex:
tempsum = totalsum - leftsum[indexes[i]] + values[i]
if tempsum >= maxsum:
maxsum = tempsum
startindex = i
tempindex = lastindex[indexes[i]] + 1
totalsum += values[i]
leftsum[indexes[i]] = totalsum
lastindex[indexes[i]] = i
else:
tempsum += values[i]
totalsum += values[i]
leftsum[indexes[i]] = totalsum
lastindex[indexes[i]] = i
if tempsum > maxsum:
maxsum = tempsum
print(maxsum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for t in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
pres = [0]
for i in range(n):
pres.append(pres[-1] + w[i])
i, j = 0, 1
ma = 0
x = {c[0]}
while i < n:
if j < n and c[j] not in x:
x.add(c[j])
j += 1
else:
x.remove(c[i])
i += 1
diff = pres[j] - pres[i]
if ma < diff:
ma = diff
print(ma)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
def answer():
c = [0] * (n + 1)
ans, i, j, s = 0, 0, 0, 0
for i in range(n):
while j < n and c[a[j]] == 0:
c[a[j]] += 1
s += w[j]
j += 1
ans = max(ans, s)
if j == n:
break
c[a[i]] -= 1
s -= w[i]
return ans
for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
w = list(map(int, input().split()))
print(answer())
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
d = {}
j = 0
i = 0
ans = 0
mans = 0
while i < n:
if d.get(c[i], 0) == 0:
ans += w[i]
if ans > mans:
mans = ans
d[c[i]] = 1
i += 1
else:
d[c[j]] -= 1
ans -= w[j]
j += 1
print(mans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for t1 in range(t):
n = int(input())
a = []
b = []
a = list(map(int, input().strip().split(" ")))
b = list(map(int, input().strip().split(" ")))
s = 0
e = 0
max = 0
sum = 0
bucket = [0] * n
for i in range(n):
if bucket[a[i]] == 0:
e = i
bucket[a[i]] = 1
sum = sum + b[i]
else:
if sum > max:
max = sum
while s <= e:
bucket[a[s]] = 0
sum = sum - b[s]
s = s + 1
if a[s - 1] == a[i]:
bucket[a[i]] = 1
e = i
sum = sum + b[i]
break
if sum > max:
max = sum
print(max)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
T = int(input())
for eachCase in range(T):
N = int(input())
C = list(map(int, input().split()))
W = list(map(int, input().split()))
seen = [False] * N
L = R = 0
maxSum = currentSum = 0
while True:
while R < N and not seen[C[R]]:
currentSum += W[R]
seen[C[R]] = True
R += 1
maxSum = max(currentSum, maxSum)
if R == N:
break
while seen[C[R]]:
seen[C[L]] = False
currentSum -= W[L]
L += 1
print(maxSum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
def list_input():
return list(map(int, input().split()))
def map_input():
return map(int, input().split())
def map_string():
return input().split()
for _ in range(int(input())):
n = int(input())
c = list_input()
w = list_input()
last = [0] * (n + 5)
pref = [0]
for i in w:
pref.append(pref[-1] + i)
dp = [0] * (n + 1)
for i in range(1, n + 1):
dp[i] = min(dp[i - 1] + w[i - 1], pref[i] - pref[last[c[i - 1]]])
last[c[i - 1]] = i
print(max(dp))
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
while t:
t -= 1
n = int(input())
C = [int(x) for x in input().strip().split()]
W = [int(x) for x in input().strip().split()]
C_location = []
latest_location = []
for i in range(0, n):
C_location.append(False)
latest_location.append(-1)
maxm_unique_sum = -1
sum_until = []
prev_summ = 0
for val in W:
prev_summ += val
sum_until.append(prev_summ)
i = 0
start = i
while i < n:
while i < n:
if C_location[C[i]]:
if latest_location[C[i]] < start:
latest_location[C[i]] = i
i += 1
continue
break
latest_location[C[i]] = i
C_location[C[i]] = True
i += 1
end = i - 1
prev_summ = sum_until[end] - sum_until[start] + W[start]
if prev_summ > maxm_unique_sum:
maxm_unique_sum = prev_summ
if i < n:
start = latest_location[C[i]] + 1
latest_location[C[i]] = i
i += 1
print(maxm_unique_sum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR WHILE VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for u in range(t):
prefix = []
n = int(input())
c = input().split()
for val in range(len(c)):
c[val] = int(c[val])
w = input().split()
for val in range(len(w)):
w[val] = int(w[val])
prefix.append(0)
for val in range(len(w)):
prefix.append(prefix[val] + w[val])
D = {}
q = []
start = 0
end = 0
maxsum = 0
while start < n:
while start < n and c[start] not in D:
q.append(c[start])
D[c[start]] = 1
start += 1
if prefix[start] - prefix[end] > maxsum:
maxsum = prefix[start] - prefix[end]
if start == n:
break
while q[0] != c[start]:
del D[q.pop(0)]
end += 1
del D[q.pop(0)]
end += 1
print(maxsum)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR WHILE VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for i in range(t):
n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))
res = 0
d1 = {}
sum1 = 0
s1 = []
max1 = 0
k = -1
for j in range(n):
sum1 += l2[j]
s1.append(sum1)
if d1.get(l1[j], -1) == -1:
if k >= 0:
res = s1[j] - s1[k]
else:
res = s1[j]
d1[l1[j]] = j
else:
if d1[l1[j]] > k:
k = d1[l1[j]]
res = s1[j] - s1[k]
d1[l1[j]] = j
if res > max1:
max1 = res
print(max1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for x in range(t):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
u = []
s = 0
st = set()
i = 0
d = dict()
while i < n:
if c[i] not in st:
s = s + w[i]
st.add(c[i])
d[c[i]] = i
i = i + 1
else:
u.append(s)
s = 0
i = d[c[i]] + 1
st.clear()
d.clear()
u.append(s)
print(max(u))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for i in range(t):
n = int(input())
c = [int(x) for x in input().split()]
w = [int(x) for x in input().split()]
s = []
for i in range(n):
if i == 0:
s.append(w[0])
else:
s.append(s[-1] + w[i])
l = 0
r = 0
j = 0
su = 0
mas = 0
vis = []
for i in range(n):
vis.append(0)
for i in range(n):
vis[c[l]] = 0
l = i
while True:
if r == n:
break
if vis[c[r]] == 1:
break
vis[c[r]] = 1
r = r + 1
r -= 1
if l == 0:
su = s[r]
else:
su = s[r] - s[l - 1]
if mas < su:
mas = su
r += 1
print(mas)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for t in range(int(input())):
n = int(input())
ar2 = input().split()
x = {}
for i in range(n):
ar2[i] = int(ar2[i])
x[ar2[i]] = False
ar = input().split()
ar[0] = int(ar[0])
for i in range(1, n):
ar[i] = int(ar[i])
ar[i] = ar[i - 1] + ar[i]
ar.append(0)
i = j = 0
ans = tans = 0
while i < n:
while j < n and x[ar2[j]] == False:
x[ar2[j]] = True
tans = ar[j] - ar[i - 1]
ans = max(tans, ans)
j = j + 1
x[ar2[i]] = False
i = i + 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for i in range(t):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
s = set([])
start = 0
s.add(c[0])
maxi = w[0]
sumi = w[0]
for j in range(1, n):
sumi += w[j]
if c[j] not in s:
s.add(c[j])
else:
while start < j and c[j] != c[start]:
s.remove(c[start])
start += 1
sumi -= w[start - 1]
start += 1
sumi -= w[start - 1]
if sumi > maxi:
maxi = sumi
print(maxi)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
t = int(input())
for i in range(t):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
d = {}
for j in c:
d[j] = 0
s, a, ans = 0, 0, 0
for k in range(n):
d[c[k]] += 1
while d[c[k]] == 2:
d[c[a]] -= 1
s -= w[a]
a += 1
s += w[k]
ans = max(ans, s)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for _ in range(int(input())):
n = int(input())
c = list(map(int, input().split()))
w = list(map(int, input().split()))
prefix, maxi, l = [], 0, 0
unique = {}
for i in range(n):
if not i:
prefix.append(w[i])
else:
prefix.append(prefix[i - 1] + w[i])
if c[i] in unique:
if unique[c[i]] >= l:
l = unique[c[i]]
maxi = max(maxi, prefix[i] if l == 0 else prefix[i] - prefix[l - 1])
unique[c[i]] = i + 1
print(maxi)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for _ in range(int(input())):
N = int(input())
C = list(map(int, input().split()))
W = list(map(int, input().split()))
sumW = [0] * N
sumW[0] = W[0]
for i in range(1, N):
sumW[i] = W[i] + sumW[i - 1]
l = 0
r = 0
maxsum = -1
status = [None] * N
while l < N and r < N:
if status[C[r]] == None:
status[C[r]] = r
r += 1
else:
tempsum = sumW[r - 1] - sumW[l] + W[l]
if tempsum > maxsum:
maxsum = tempsum
for i in range(l, status[C[r]]):
status[C[i]] = None
l = status[C[r]] + 1
status[C[r]] = None
tempsum = sumW[r - 1] - sumW[l] + W[l]
if tempsum > maxsum:
maxsum = tempsum
print(maxsum)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NONE ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W_{1}, W_{2}, .., W_{N}) and C = (C_{1}, C_{2}, .., C_{N}) with N elements each. A range [l, r] is unique if all the elements C_{l}, C_{l+1}, .., C_{r} are unique (ie. no duplicates). The sum of the range is W_{l} + W_{l+1} + ... + W_{r}.
You want to find an unique range with the maximum sum possible, and output this sum.
------ Input ------
The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
The first line of each test case contains a single integer N, denoting the size of the arrays.
The second line contains N space-separated integers : C_{1}, C_{2}, .., C_{N}.
The third line contains N space-separated integers : W_{1}, W_{2}, .., W_{N}.
------ Output ------
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 1000000$
$0 ≤ C_{i} < N$
$0 ≤ W_{i} ≤ 1000000000
$ $1 ≤ Sum of N over all test cases ≤ 1000000$
------ Subtasks ------
$Subtask #1 (30 points): Sum of N over all test cases ≤ 10000$
$Subtask #2 (70 points): Original constraints$
----- Sample Input 1 ------
1
5
0 1 2 0 2
5 6 7 8 2
----- Sample Output 1 ------
21
----- explanation 1 ------
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|
for tt in range(int(input())):
n = int(input())
m = {}
for i in range(n):
m[i] = -1
c = [int(i) for i in input().split()]
w = [int(i) for i in input().split()]
ans = 0
j = 0
i = 0
sm = 0
while i < n:
if m[c[i]] != -1:
h = m[c[i]]
for k in range(j, h + 1):
m[c[k]] = -1
sm -= w[k]
j = h + 1
m[c[i]] = i
sm += w[i]
ans = max(sm, ans)
i += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
arr.sort()
cnt = 0
for i in range(n - 2):
j = i + 1
k = n - 1
target_sum = sumo - arr[i]
while j < k:
res = arr[i] + arr[j] + arr[k]
if res < sumo:
cnt += k - j
j += 1
else:
k -= 1
return cnt
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, N, sum):
arr.sort()
count = 0
for i in range(N - 2):
first, last = i + 1, N - 1
while first < last:
req = arr[i] + arr[first] + arr[last]
if req >= sum:
last -= 1
else:
count += last - first
first += 1
return count
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
arr.sort()
c = 0
for i in range(len(arr)):
tar = sumo - arr[i]
si = i + 1
ei = n - 1
while si <= ei:
if arr[si] + arr[ei] < tar:
c += ei - si
si += 1
else:
ei -= 1
return c
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def pair_sum(self, arr, start, end, sumo, a):
i, j = start, end
cnt = 0
while i < j:
curr_sum = a + arr[i] + arr[j]
if arr[i] == a:
i += 1
elif arr[j] == a:
j -= 1
elif curr_sum < sumo:
cnt += j - i
i += 1
else:
j -= 1
return cnt
def countTriplets(self, arr, n, sumo):
arr.sort()
cnt = 0
for i in range(n - 2):
cnt += self.pair_sum(arr, i + 1, n - 1, sumo, arr[i])
return cnt
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
n = len(arr)
if n <= 2:
return 0
arr.sort()
ctr = 0
for idx, num in enumerate(arr):
i, j = idx + 1, n - 1
while i < j:
if arr[i] + arr[j] + num >= sumo:
j -= 1
else:
ctr += j - i
i += 1
return ctr
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, s):
c = 0
arr.sort()
for i in range(n - 2):
j, k = i + 1, n - 1
while j < k:
if arr[i] + arr[j] + arr[k] >= s:
k -= 1
elif arr[i] + arr[j] + arr[k] < s:
c += k - j
j += 1
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
cnt = 0
arr.sort()
for l in range(0, n):
i, j = l + 1, n - 1
while i < j:
sum = arr[l] + arr[i] + arr[j]
if sum < sumo:
cnt += j - i
i += 1
else:
j -= 1
return cnt
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
result = 0
arr.sort()
for i in range(n - 2):
if i > 0 or arr[i] != arr[i - 1]:
start = i + 1
end = n - 1
while start < end:
if arr[i] + arr[start] + arr[end] >= sumo:
end -= 1
else:
result += end - start
start += 1
return result
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
ans = 0
arr.sort()
for i in range(n):
j = i + 1
e = n - 1
c = 0
s = 0
while j < e:
if arr[i] + arr[j] + arr[e] >= sumo:
e = e - 1
else:
c = c + (e - j)
j = j + 1
ans = ans + c
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
ans = 0
arr = sorted(arr)
for i in range(len(arr)):
l, r = i + 1, len(arr) - 1
while l < r:
curr = arr[i] + arr[l] + arr[r]
if curr >= sumo:
r -= 1
else:
ans += r - l
l += 1
return ans
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
nums = sorted(arr)
count = 0
start = 0
while start < len(nums):
sub_start, sub_end = start + 1, len(nums) - 1
while sub_start < sub_end:
if nums[sub_start] + nums[sub_end] < sumo - nums[start]:
count += sub_end - sub_start
sub_start += 1
else:
sub_end -= 1
start += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
arr = sorted(arr)
count = 0
for i in range(n):
left = i + 1
right = n - 1
while left < right:
sum1 = arr[i] + arr[left] + arr[right]
if sum1 < sumo:
count = count + right - left
left = left + 1
else:
right = right - 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, sumo):
arr.sort()
ans = 0
for i in range(n):
a = i + 1
b = n - 1
while a < b:
if arr[a] + arr[b] + arr[i] < sumo:
ans += b - a
a += 1
else:
b -= 1
return ans
|
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, nums, n, s):
c = 0
n = len(nums)
nums.sort()
for i in range(n - 2):
j, k = i + 1, n - 1
while j < k:
if nums[i] + nums[j] + nums[k] >= s:
k -= 1
else:
c += k - j
j += 1
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, a, n, s):
c = 0
a = sorted(a)
for i in range(n):
j = i + 1
k = n - 1
while j < k:
if a[i] + a[j] + a[k] >= s:
k = k - 1
else:
c += k - j
j += 1
return c
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given an array arr[] of distinct integers of size N and a value sum, the task is to find the count of triplets (i, j, k), having (i<j<k) with the sum of (arr[i] + arr[j] + arr[k]) smaller than the given value sum.
Example 1:
Input: N = 4, sum = 2
arr[] = {-2, 0, 1, 3}
Output: 2
Explanation: Below are triplets with
sum less than 2 (-2, 0, 1) and (-2, 0, 3).
Example 2:
Input: N = 5, sum = 12
arr[] = {5, 1, 3, 4, 7}
Output: 4
Explanation: Below are triplets with
sum less than 12 (1, 3, 4), (1, 3, 5),
(1, 3, 7) and (1, 4, 5).
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countTriplets() that take array arr[], integer N and integer sum as parameters and returns the count of triplets.
Expected Time Complexity: O(N^{2}).
Expected Auxiliary Space: O(1).
Constraints:
3 ≤ N ≤ 10^{3}
-10^{3} ≤ arr[i] ≤ 10^{3}
|
class Solution:
def countTriplets(self, arr, n, target):
i = 0
arr.sort()
count = 0
while i < n - 2:
j = i + 1
k = n - 1
while j < k:
if arr[i] + arr[j] + arr[k] < target:
count += k - j
j += 1
else:
k -= 1
i += 1
return count
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 ≤ i ≤ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y ≥ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤A_{i} ≤10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split(" ")))
s, e, c = 0, n - 1, 0
if n == 1:
print(c)
else:
while s < e:
if a[s] < a[e]:
a[e] = a[e] - a[s]
c += 1
s += 1
elif a[s] > a[e]:
a[s] = a[s] - a[e]
c += 1
e -= 1
else:
s += 1
e -= 1
print(c)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 ≤ i ≤ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y ≥ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤A_{i} ≤10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
count = 0
while len(arr) > 1:
if arr[0] == arr[-1]:
del arr[0]
del arr[-1]
else:
if arr[0] > arr[-1]:
arr[0] = arr[0] - arr[-1]
del arr[-1]
else:
arr[-1] = arr[-1] - arr[0]
del arr[0]
count = count + 1
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 ≤ i ≤ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y ≥ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤A_{i} ≤10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
l = 0
r = n - 1
c = 0
while l < r:
if a[l] == a[r]:
l += 1
r -= 1
elif a[l] < a[r]:
a[r] = a[r] - a[l]
l += 1
c += 1
else:
a[l] = a[l] - a[r]
r -= 1
c += 1
print(c)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 ≤ i ≤ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y ≥ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤A_{i} ≤10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
if n % 2 == 0:
size = n // 2 - 1
else:
size = n // 2
lz = 0
leftel = a[0]
rightel = a[n - 1]
ct = 0
rz = n - 1
while len(a) > 1:
if a[0] == a[-1]:
del a[0]
del a[-1]
else:
if a[0] > a[-1]:
a[0] = a[0] - a[-1]
del a[-1]
else:
a[-1] = a[-1] - a[0]
del a[0]
ct = ct + 1
print(ct)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 ≤ i ≤ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y ≥ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤A_{i} ≤10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
l = 0
r = len(a) - 1
count = 0
while l < r:
if a[l] == a[r]:
l = l + 1
r = r - 1
elif a[l] < a[r]:
a[r] = a[r] - a[l]
l = l + 1
count = count + 1
else:
a[l] = a[l] - a[r]
r = r - 1
count = count + 1
print(count)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 ≤ i ≤ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y ≥ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤A_{i} ≤10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
n = int(input())
for i in range(n):
size = int(input())
arr = input()
arr = [int(x) for x in arr.split()]
count = 0
f = 0
l = len(arr) - 1
while True:
while arr[f] == arr[l] and f < l:
f += 1
l -= 1
if f >= l:
break
if arr[f] < arr[l]:
arr[l] = arr[l] - arr[f]
f = f + 1
else:
arr[f] = arr[f] - arr[l]
l = l - 1
count += 1
print(count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
JJ has an array A initially of length N. He can perform the following operation on A:
1) Pick any index i (1 ≤ i ≤ |A|) such that A_{i} > 1 \\
2) Select any two integers X and Y such that X + Y = A_{i} and X, Y ≥ 1 \\
3) Replace A_{i} with X and Y
Note that the length of the array increases by 1 after each operation.
For example, if A = [4, 6, 7, 2], he can select i = 1 and select X = 1, Y = 3 (since X + Y = A_{1}). After the operation array becomes: [\underline{4}, 6, 7, 2] \rightarrow [\underline{1}, \underline{3}, 6, 7, 2].
JJ wants to make A palindromic. Find the minimum number of operations to do so.
It is guaranteed that A can be converted to a palindromic array by using the above operation.
Note: An array is called a palindrome if it reads the same backwards and forwards, for e.g. [1, 3, 3, 1] and [6, 2, 6] are palindromic.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first line of each test case contains an integer N — the size of the array A.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A.
------ Output Format ------
For each test case, output the minimum number of operations to make A palindromic.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$1 ≤ N ≤ 10^{5}$
$1 ≤A_{i} ≤10^{5}$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
4
3 7 6 4
5
1 4 5 4 1
5
1 2 3 4 5
----- Sample Output 1 ------
2
0
4
----- explanation 1 ------
Test Case 1: We can perform the following operations:
- $[3, \underline{7}, 6, 4] \xrightarrow{i = 2, X = 1, Y = 6} [3, \underline{1}, \underline{6}, 6, 4]$
- $[3, 1, 6, 6, \underline{4}] \xrightarrow{i = 5, X = 1, Y = 3} [3, 1, 6, 6, \underline{1}, \underline{3}]$
Test Case 2: $A$ is already palindromic.
|
n = int(input())
def pal(arr):
x = 0
i, j = 0, len(arr) - 1
while i <= j:
if arr[i] == arr[j]:
i += 1
j -= 1
elif arr[i] > arr[j]:
arr[i] -= arr[j]
j -= 1
x = x + 1
else:
arr[j] -= arr[i]
i += 1
x = x + 1
return x
for i in range(n):
length = int(input())
li = list(map(int, input().split()))
print(pal(li))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.