/**
* Problem: LeetCode 2913 - Subarrays Distinct Element Sum of Squares I
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
import java.util.List;
import java.util.HashSet;
import java.util.Set;
/**
* Problem: LeetCode 2913 - Subarrays Distinct Element Sum of Squares I
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int sumCounts(List<Integer> nums) {
int n = nums.size();
int total = 0;
for (int i = 0; i < n; i++) {
Set<Integer> distinct = new HashSet<>();
for (int j = i; j < n; j++) {
distinct.add(nums.get(j));
int count = distinct.size();
total += count * count;
}
}
return total;
}
}
"""
Problem: LeetCode 2913 - Subarrays Distinct Element Sum of Squares I
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def sumCounts(self, nums: List[int]) -> int:
n = len(nums)
total = 0
for i in range(n):
distinct = set()
for j in range(i, n):
distinct.add(nums[j])
total += len(distinct) ** 2
return total
/**
* Problem: LeetCode 2913 - Subarrays Distinct Element Sum of Squares I
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <unordered_set>
using namespace std;
/**
* Problem: LeetCode 2913 - Subarrays Distinct Element Sum of Squares I
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int sumCounts(vector<int>& nums) {
int n = nums.size();
int total = 0;
for (int i = 0; i < n; i++) {
unordered_set<int> distinct;
for (int j = i; j < n; j++) {
distinct.insert(nums[j]);
int count = distinct.size();
total += count * count;
}
}
return total;
}
};
/**
* Problem: LeetCode 2913 - Subarrays Distinct Element Sum of Squares I
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var sumCounts = function(nums) {
const n = nums.length;
let total = 0;
for (let i = 0; i < n; i++) {
const distinct = new Set();
for (let j = i; j < n; j++) {
distinct.add(nums[j]);
const count = distinct.size;
total += count * count;
}
}
return total;
};