# CSPT25 Lecture 2 ## [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) ``` class Solution: """ Understand nums = [1,2,3,1,1,3] output = 4 nums = [1,1,1,1] output = 6 nums = [1,2,3] output = 0 nums = [1] output = 0 Plan Generate all possible pairs. Count the ones that are good pairs. Output the count in the end. """ def numIdenticalPairs(self, nums: List[int]) -> int: numGoodPairs = 0 for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] == nums[j]: numGoodPairs += 1 return numGoodPairs ``` ## [Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/) ``` class Solution: """ Understand accounts = [[1,2,3], [3,2,1]] output = 6 accounts = [[1,2,3], [3,2,1], [100,20,1]] output = 121 accounts = [[1,2,3]] output = 6 Plan Go through all accounts and sum up a customer's wealth. Keep track of the highest wealth that you found. Return that value. maxWealthFound = 121 customerWealth = 121 [[1,2,3], [3,2,1], [100,20,1]**] """ def maximumWealth(self, accounts: List[List[int]]) -> int: maxWealthFound = 0 for account in accounts: customerWealth = sum(account) if customerWealth > maxWealthFound: maxWealthFound = customerWealth return maxWealthFound ``` ## [N Repeated Element](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) ``` class Solution: """ Understand nums = [1,2,3,3] 4 == 2 * n n = 2 output = 3 nums = [2,1,2,5,3,2] 6 == 2 * n n = 3 output = 2 nums = [5,1,5,2,5,3,5,4] 8 == 2 * n n = 4 output = 5 Plan Use a dictionary to keep track of how many times an element occurs. Traverse that dictionary and output the key that occurs arrayLength / 2 """ def repeatedNTimes(self, nums: List[int]) -> int: counts = {} for num in nums: if num not in counts: counts[num] = 1 else: counts[num] += 1 n = len(nums) / 2 for element, numTimesOccurred in counts.items(): if numTimesOccurred == n: return element ```