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

Python - Algorithm #39

hkl22 2024. 8. 26. 10:07

Python Algorithm

LeetCode

2363. Merge Similar Items

  • You are given two 2D integer arrays, [items1] and [items2], representing two sets of items. Each array [items] has the following properties:
    • items[i] = [valueᵢ, weightᵢ] where valueᵢ represents the value and weightᵢ represents the weight of the iᵗʰ item.
    • The value of each item in items is unique.
  • Return a 2D integer array ret where ret[i] = [value, weight], with weight being the sum of weights of all items with value value.
  • Note: ret should be returned in ascending order by value.
  • Constraints
    • 1 items1.length, items2.length 1000
    • items1[i].length == items2[i].length == 2
    • 1 value, weight 1000
    • Each value in [items1] is unique.
    • Each value in [items2] is unique.
class Solution:
    def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
        # dictionary, for
        count_dict = defaultdict(int)
        for value, weight in items1 + items2:
            count_dict[value] += weight
        sorted_keys = sorted(count_dict.keys())
        answer = []
        for key in sorted_keys:
            answer.append([key, count_dict[key]])
        return answer
from collections import defaultdict

class Solution:
    def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
        items = items1 + items2
        items_dict = defaultdict(int)
        answer = []
        for value, weight in items:
            items_dict[value] += weight
        return sorted(items_dict.items())
from collections import defaultdict

class Solution:
    def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
        items = items1 + items2
        items_dict = defaultdict(int)
        answer = []
        for value, weight in items:
            items_dict[value] += weight
        for key, value in items_dict.items():
            answer.append([key, value])
        return sorted(answer)

2367. Number of Arithmetic Triplets

  • You are given a 0-indexed, strictly increasing integer array [nums] and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met:
    • i < j < k,
    • nums[j] - nums[i] == diff, and
    • nums[k] - nums[j] == diff.
  • Return the number of unique arithmetic triplets.
  • Constraints
    • 3 nums.length 200
    • 0 nums[i] 200
    • 1 diff 50
    • [nums] is strictly increasing.
class Solution:
    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
        # 3중 for문 사용
        answer = 0
        for i in range(len(nums)):
            for j in range(i+1, len(nums)):
                for k in range(j+1, len(nums)):
                    # nums[j] - nums[i] == diff, and
                    # nums[k] - nums[j] == diff일 때 answer에 1 더해주기
                    if (nums[j] - nums[i] == diff) and (nums[k] - nums[j] == diff):
                        answer += 1
        return answer
class Solution:
    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
        answer = 0
        for a, b, c in combinations(nums, 3):
            if (b - a == diff) and (c - b == diff):
                answer += 1
        return answer
class Solution:
    def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
        answer = 0
        for i in range(len(nums)):
            for j in range(i+1, len(nums)):
                for k in range(j+1, len(nums)):
                    if (nums[j] - nums[i] == diff) and (nums[k] - nums[j] == diff):
                        answer += 1
        return answer

https://leetcode.com/problems/merge-similar-items/

https://leetcode.com/problems/number-of-arithmetic-triplets/

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

Python - Algorithm #41  (0) 2024.08.28
Python - Algorithm #40  (0) 2024.08.27
Python - Algorithm #38  (0) 2024.08.23
Python - Algorithm #37  (0) 2024.08.12
Python - Algorithm #36  (0) 2024.08.09