지난 공부기록/알고리즘 풀이

Python - Algorithm #36

hkl22 2024. 8. 9. 10:06

Python Algorithm

LeetCode

1710. Maximum Units on a Truck

  • You are assigned to put some amount of boxes onto one truck. You are given a 2D array [boxTypes], where boxTypes[i] = [numberOfBoxesᵢ, numberOfUnitsPerBox]:
    • numberOfBoxes is the number of boxes of type
    • i.numberOfUnitsPerBox is the number of units in each box of the type i.
  • You are also given an integer truckSize, which is the maximum number of boxes that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed truckSize.
  • Return the maximum total number of units that can be put on the truck.
  • Constraints
    • 1 boxTypes.length 1000
    • 1 numberOfBoxesi, numberOfUnitsPerBoxi 1000
    • 1 truckSize 10⁶
class Solution:
    def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
        # sorting, for
        sortedBoxTypes = sorted(boxTypes, key=lambda x: x[1], reverse=True)
        answer = 0
        for numBoxes, unit in sortedBoxTypes:
            # 트럭에 실을 수 있는 수량만큼 싣고
            # 실을 수량 * 유닛만큼 answer에 더해주기
            if truckSize > numBoxes:
                answer += numBoxes * unit
                truckSize -= numBoxes
            else:
                answer += truckSize * unit
                break
        return answer
class Solution:
    def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
        answer = 0
        boxTypes.sort(key=lambda x: -x[1])
        for i in range(len(boxTypes)):
            if truckSize >= boxTypes[i][0]:
                answer += boxTypes[i][0] * boxTypes[i][1]
                truckSize -= boxTypes[i][0]
            else:
                answer += truckSize * boxTypes[i][1]
                break
        return answer
class Solution:
    def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int:
        answer = 0
        boxTypes.sort(key=lambda x: -x[1])
        for i, [num, unit] in enumerate(boxTypes):
            if truckSize >= num:
                answer += num * unit
                truckSize -= num
            else:
                answer += truckSize * unit
                break
        return answer

1773. Count Items Matching a Rule

  • You are given an array [items], where each items[i] = [type, color, name] describes the type, color, and name of the iᵗʰ item. You are also given a rule represented by two strings, ruleKey and ruleValue.
  • The iᵗʰ item is said to match the rule if one of the following is true:
    • ruleKey == "type" and ruleValue == type.
    • ruleKey == "color" and ruleValue == color.
    • ruleKey == "name" and ruleValue == name.
  • Return the number of items that match the given rule.
  • Constraints
    • 1 items.length 10⁴
    • 1 type.length, color.length, name.length, ruleValue.length 10
    • ruleKey is equal to either "type", "color", or "name".
    • All strings consist only of lowercase letters.
class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        answer = 0
        for type_, color, name in items:
            # ruleKey가 type이고, type이 ruleValue와 같으면 answer += 1
            # ruleKey가 color이고, color가 ruleValue와 같으면 answer += 1
            # ruleKey가 name이고, name이 ruleValue와 같으면 answer += 1
            if ruleKey == "type" and type_ == ruleValue:
                answer += 1
            elif ruleKey == "color" and color == ruleValue:
                answer += 1
            elif ruleKey == "name" and name == ruleValue:
                answer += 1
        return answer
class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        answer = 0
        for i in range(len(items)):
            if ruleKey == "type" and items[i][0] == ruleValue:
                    answer += 1
            elif ruleKey == "color" and items[i][1] == ruleValue:
                    answer += 1
            elif ruleKey == "name" and items[i][2] == ruleValue:
                    answer += 1
        return answer
class Solution:
    def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
        answer = 0
        if ruleKey == "type":
           n = 0
        elif ruleKey == "color":
            n = 1
        else:
            n = 2
        for i in range(len(items)):
            if items[i][n] == ruleValue:
                answer += 1
        return answer

https://leetcode.com/problems/maximum-units-on-a-truck/

https://leetcode.com/problems/count-items-matching-a-rule/

'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글

Python - Algorithm #38  (0) 2024.08.23
Python - Algorithm #37  (0) 2024.08.12
Python - Algorithm #35  (0) 2024.08.08
Python - Algorithm #34  (0) 2024.08.07
Python - Algorithm #33  (0) 2024.08.06