---
# System prepended metadata

title: CSPT17 Lecture 2 - UPER

---

# CSPT17 Lecture 2 - UPER

## [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/)
```
"""
Understand

[1,2,3,1,1,3]
output: 4

[1,2,3]
output: 0

[1,1,1,1]
output: 6

Plan
Keep track of number of good pairs using a variable

Go through all possible pairs in the list and count
the ones that are good pairs

Return the number of good pairs
"""

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
```

## [Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/submissions/)
```
"""
Understand

accounts = [[1,2,3],[3,2,1,1]]
output: 7

accounts = [[1,2,3],
            [3,2,100]]
output: 105

accounts = [[1,2,3]]
output: 6

Plan
Keep track of highest wealth seen

Go through all accounts, using two for loops
Add up all wealth current customer has
If total wealth is higher than what we've seen, update highest wealth seen

return highest wealth seen
"""

class Solution:
    def maximumWealth(self, accounts: List[List[int]]) -> int:
        maxWealth = 0
        for customer in accounts:
            currCustomerWealth = sum(customer)
            if currCustomerWealth > maxWealth:
                maxWealth = currCustomerWealth
        return maxWealth
```

## [Uncommon Words](https://leetcode.com/problems/uncommon-words-from-two-sentences/submissions/)
```
class Solution:
    """
    Understand
    A = "this apple is sweet", B = "this apple is sour"
    output: ["sweet", "sour"]
    
    A = "apple apple" B = "apple"
    output: []
    
    A = "orange pear" B = "strawberry pineapple"
    output = ["orange", "pear", "strawberry", "pineapple"]
    
    Plan
    Split the string into a list
    
    Keep track of word occurences in both lists
    sentenceA will have a dictionary [word --> num occurences]
    sentenceB will have a dictionary [word --> num occurences]
    
    To know if a word is uncommon, check if number of occurences in original sentence is 1
    AND it doesn't occur in sentenceB
    """
    def uncommonFromSentences(self, A: str, B: str) -> List[str]:
        wordCountInA = self.countWords(A)
        wordCountInB = self.countWords(B)
        res = []
        for (word, numOccurence) in wordCountInA.items():
            if numOccurence == 1 and word not in wordCountInB:
                res.append(word)
        for (word, numOccurence) in wordCountInB.items():
            if numOccurence == 1 and word not in wordCountInA:
                res.append(word)
        return res
        
    def countWords(self, sentence):
        wordCount = {}
        sentenceList = sentence.split(" ")
        for word in sentenceList:
            if word not in wordCount:
                wordCount[word] = 1
            else:
                wordCount[word] += 1
        return wordCount
```