[
counter
hash table
]
BinarySearch 0463 Count of Sublists with Same First and Last Values
Problem statement
https://binarysearch.com/problems/Count-of-Sublists-with-Same-First-and-Last-Values/
Solution
For each value x
if we have t
places, we can get t * (t + 1)//2
segments.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, nums):
return sum(x*(x+1)//2 for x in Counter(nums).values())