# CSPT29 Lecture 2 ## Number of Good Pairs ``` class Solution: """ nums = [1,2,3,1,1,3] output = 4 nums = [1,1,1] output = 3 nums = [1,2,3] output = 0 Plan Brute-force - generate all possible pairs and count which ones are good nums = [1,2,3,1,1,3] numGoodPairs = 4 """ 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 ``` ## Max Wealth ``` class Solution: """ accounts = [[1,2,3], [3,2,1]] output = 6 accounts = [[1,2,3], [3,2,10]] output = 15 accounts = [[1,2,3]] output = 6 Plan Look at every single customer While doing that, keep track of max wealth found from a customer Return highest wealth found """ 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 ``` class Solution: """ nums.length = 2 * n n = nums.length / 2 nums = [1,2,3,3] output = 3 nums = [2,1,2,5,3,2] output = 2 Plan You need to go through all elements at least once Use a dictionary that maps from element --> num times occuered n = nums.length / 2 Go through the dictionary one more time and return the key that occurred n times """ 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 num, numTimesOccurred in counts.items(): if numTimesOccurred == n: return num ```