본문 바로가기
python-algorithm

leetcode 2913. Subarrays Distinct Element Sum of Squares I

by 무적김두칠 2024. 1. 15.

https://leetcode.com/problems/subarrays-distinct-element-sum-of-squares-i/description/

 

Subarrays Distinct Element Sum of Squares I - LeetCode

Can you solve this real interview question? Subarrays Distinct Element Sum of Squares I - 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 ind

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def sumCounts(self, nums: List[int]) -> int:
        answer = 0
        cnt = 1
        for i in range(len(nums), 0-1):
            for j in range(i):
                distinct_nums = list(set(nums[j: j + cnt]))
                answer += (len(distinct_nums) ** 2)
            cnt += 1
        
        return answer
 
        
cs
반응형

댓글