# CSPT23 Lecture 2 ## [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) ``` """ Understand nums = [1,2,3,1,1,3] output = 4 nums = [1,2,3] output = 0 nums = [1,1,1] output = 3 Plan Go through each possible pair, and count how many good pairs there are """ class Solution: 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 ``` ## [N Repeated Element](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/) ``` class Solution: """ nums = [1,2,3,3] len(nums) = 2n n = len(nums) / 2 output = 3 nums = [2,1,2,5,3,2] output = 2 nums = [5,1,5,2,5,3,5,4] output = 5 Plan Walk through all elements and keep track of how many times an element occurs in the list Then walk through dictionary and output the key that occurs n times [2,1,2,5,3,2] counts = {2: 3, 1: 1, 5: 1, 3: 1} target = 3 """ def repeatedNTimes(self, nums: List[int]) -> int: counts = {} for num in nums: if num not in counts: counts[num] = 1 else: counts[num] += 1 target = len(nums) / 2 for (element, numTimesAppeared) in counts.items(): if numTimesAppeared == target: return element return -1 ``` ## [Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/) ``` class Solution: """ accounts = [[1,2,3], [3,2,1]] output = 6 accounts = [[1,2,2], [3,2,1]] output = 6 accounts = [[0]] output = 0 Plan Go through each customer and each customer's bank accounts to get the sum for their wealth. Keep track and output the maximum wealth for a customer [[1,2,2], [3,2,1]] maxWealth = 6 currCustomerWealth = 6 """ 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 ```