Leetcode Problem 2913. Subarrays Distinct Element Sum of Squares I

2913. Subarrays Distinct Element Sum of Squares I

Leetcode Solutions

Brute-force solution with Hash Map

  1. Initialize ans to 0 to store the final result.
  2. Iterate over the start index i of the subarray from 0 to the size of nums.
  3. For each i, initialize an empty hash map ump to store the frequency of elements.
  4. Iterate over the end index j of the subarray from i to the size of nums.
  5. Update the frequency of nums[j] in ump.
  6. If the frequency of nums[j] is 1, increment the distinct count count.
  7. Add the square of count to ans.
  8. Continue this process until all subarrays are considered.
  9. Return ans as the final result.
UML Thumbnail

Brute-force solution with Set

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...