# CSPT21 Lecture 2 ## [Num Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) ``` class Solution: """ Understand [1,2,3,1,1,3] output = 4 [1,1,1,1] output = 6 [1,2,3] output = 0 Plan [1,2,3,1,1,3] Go through all pairs, and count how many good pairs we have """ 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 [[1,2,3], [3,2,1]] output = 6 [] output = 0 [[1,2,3]] output = 6 [[1,2,3], [3,2,2]] output = 7 Plan For each customer, get the sum of all their bank accounts. Keep track of the highest sum found. Return that sum. """ def maximumWealth(self, accounts: List[List[int]]) -> int: maxWealth = 0 for customer in accounts: currCustomerWealth = 0 for bankAccount in customer: currCustomerWealth += bankAccount if currCustomerWealth > maxWealth: maxWealth = currCustomerWealth return maxWealth ``` ## [N Repeated Element](https://leetcode.com/problems/n-repeated-element-in-size-2n-array) ``` class Solution: """ Understand [1,2,3,3] output = 3 [2,1,2,5,3,2] output = 2 [5,1,5,2,5,3,5,4] output = 5 Plan Iterate through all elements and store their counts in a dictionary (elem --> numTimesOccurred). Then iterate through dictionary and output the key that occurs N times. """ def repeatedNTimes(self, A: List[int]) -> int: counts = {} for num in A: if num not in counts: counts[num] = 1 else: counts[num] += 1 target = len(A) / 2 for (num, timesAppeared) in counts.items(): if timesAppeared == target: return num return -1 ```