-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2913.py
More file actions
52 lines (47 loc) · 1.59 KB
/
Copy path2913.py
File metadata and controls
52 lines (47 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'''
2913. Subarrays Distinct Element Sum of Squares I
Difficulty:Easy
You are given a 0-indexed integer array nums.
The distinct count of a subarray of nums is defined as:
Let nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length.
Then the number of distinct values in nums[i..j] is called the distinct count of nums[i..j].
Return the sum of the squares of distinct counts of all subarrays of nums.
A subarray is a contiguous non-empty sequence of elements within an array.
Hints:
1.Use a set/heap to keep track of distinct element counts.
----
Example 1:
Input: nums = [1,2,1]
Output: 15
Explanation: Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 + 22 + 22 + 22 = 15.
Example 2:
Input: nums = [1,1]
Output: 3
Explanation: Three possible subarrays are:
[1]: 1 distinct value
[1]: 1 distinct value
[1,1]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 12 + 12 + 12 = 3.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
@author mavis
'''
class Solution:
def sumCounts(self, nums: list[int]) -> int:
res = []
for i in range(len(nums)):
for j in range(i,len(nums)+1):
res.append(len(set(nums[i:j])) ** 2)
return sum(res)
nums = [1,2,1]
# 15
ans = Solution()
print(ans.sumCounts(nums))