返回
LeetCode 2333. Cartesian Sum of Squared Pairs
后端
2024-01-13 23:34:50
### Introduction
The Cartesian sum of a pair of integers (a, b) is defined as (a + b) ^ 2.
This LeetCode problem is about finding the sum of the Cartesian sum of all the distinct ordered pair of integers in a given array.
### Problem Details
* [LeetCode 2333. Cartesian Sum of Squared Pairs](https://github.com/Bfbi92/LeetCode-Solutions/blob/main/solutions/2333-Cartesian-Sum-of-Squared-Pairs/Cartesian-Sum-of-Squared-Pairs.py)
* Difficulty: **[MEDIUM](https://github.com/Bfbi92/LeetCode-Solutions/blob/main/solutions/2333-Cartesian-Sum-of-Squared-Pairs/Cartesian-Sum-of-Squared-Pairs.py)**
* Submissions: 18.2K
* Positive Ratings: 77.4%
### Solution
The core of the problem is to calculate all the Cartesian sum of the paired integers in the array. To do this we can use 2-nested loops. The first loop can select the first number of a pair and the second loop can select the second number of that pair. However, this will lead to count some Cartesian sum of the same pair twice. For example, if the array is [1, 2, 3], then the pair (1,2) will be counted twice, once when 1 is the first number and 2 is the second number, and again when 2 is the first number and 1 is the second number. To avoid this, we can only count the Cartesian sum of a pair when the first number is no larger than the second number.
```python
def cartesianSum(arr):
n = len(arr)
arr.sort()
result = 0
for i in range(n):
for j in range(i, n):
result += (arr[i] + arr[j])**2
return result
Summary
In this problem, we learned how to use the Cartesian product to solve a problem. The key to this problem is to avoid the double-counting of the Cartesian product by only considering the Cartesian product of two elements when the first element is not greater than the second element.
## LeetCode 2333. Cartesian Sum of Squared Pairs
The Cartesian sum of a pair of integers (a, b) is defined as (a + b) ^ 2.
This LeetCode problem is about finding the sum of the Cartesian sum of all the distinct ordered pair of integers in a given array.
### Problem Details
* [LeetCode 2333. Cartesian Sum of Squared Pairs](https://github.com/Bfbi92/LeetCode-Solutions/blob/main/solutions/2333-Cartesian-Sum-of-Squared-Pairs/Cartesian-Sum-of-Squared-Pairs.py)
* Difficulty: **[MEDIUM](https://github.com/Bfbi92/LeetCode-Solutions/blob/main/solutions/2333-Cartesian-Sum-of-Squared-Pairs/Cartesian-Sum-of-Squared-Pairs.py)**
* Submissions: 18.2K
* Positive Ratings: 77.4%
### Solution
The core of the problem is to calculate all the Cartesian sum of the paired integers in the array. To do this we can use 2-nested loops. The first loop can select the first number of a pair and the second loop can select the second number of that pair. However, this will lead to count some Cartesian sum of the same pair twice. For example, if the array is [1, 2, 3], then the pair (1,2) will be counted twice, once when 1 is the first number and 2 is the second number, and again when 2 is the first number and 1 is the second number. To avoid this, we can only count the Cartesian sum of a pair when the first number is no larger than the second number.
```python
def cartesianSum(arr):
n = len(arr)
arr.sort()
result = 0
for i in range(n):
for j in range(i, n):
result += (arr[i] + arr[j])**2
return result
Summary
In this problem, we learned how to use the Cartesian product to solve a problem. The key to this problem is to avoid the double-counting of the Cartesian product by only considering the Cartesian product of two elements when the first element is not greater than the second element.