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

Python - Algorithm #28

hkl22 2024. 7. 30. 10:19

Python Algorithm

LeetCode

2535. Difference Between Element Sum and Digit Sum of an Array

  • You are given a positive integer array [nums].
    • The element sum is the sum of all the elements in [nums].
    • The digit sum is the sum of all the digits (not necessarily distinct) that appear in [nums].
  • Return the absolute difference between the element sum and digit sum of [nums].
  • Note that the absolute difference between two integers x and y is defined as |x - y|.
  • Constraints
    • 1 nums.length 2000
    • 1 nums[i] 2000
class Solution:
    def differenceOfSum(self, nums: List[int]) -> int:
        # for문으로 반복
        # num을 문자열로 만들고, 다시 for문을 돌면 각 자릿수를 가져올 수 있음
        element_sum = 0
        digit_sum = 0
        for num in nums:
            element_sum += num
            for c in str(num):
                digit_sum += int(c)
        # 둘의 차이의 절대값을 리턴
        return abs(element_sum - digit_sum)
class Solution:
    def differenceOfSum(self, nums: List[int]) -> int:
        x = sum(nums)
        y = 0
        for num in nums:
            for n in str(num):
                y += int(n)
        return abs(x - y)

2644. Find the Maximum Divisibility Score

  • You are given two integer arrays [nums] and [divisors].
  • The divisibility score of divisors[i] is the number of indices j such that nums[j] is divisible by divisors[i].
  • Return the integer divisors[i] with the maximum divisibility score. If multiple integers have the maximum score, return the smallest one.
  • Constraints
    • 1 nums.length, divisors.length 1000
    • 1 nums[i], divisors[i] 10⁹
class Solution:
    def maxDivScore(self, nums: List[int], divisors: List[int]) -> int:
        # 각 divisor 별 divisibility score 저장
        d = {}
        for divisor in divisors:
            score = 0
            for num in nums:
                if num % divisor == 0:
                    score += 1
            d[divisor] = score
        max_score = max(d.values())
        candidates = []
        for divisor, score in d.items():
            if score == max_score:
                candidates.append(divisor)
        answer = sorted(candidates)[0]
        return answer
class Solution:
    def maxDivScore(self, nums: List[int], divisors: List[int]) -> int:
        # 각 divisor 별 divisibility score 저장
        d = {}
        for divisor in divisors:
            score = 0
            for num in nums:
                if num % divisor == 0:
                    score += 1
            d[divisor] = score
        answer = sorted(d.items(), key=lambda x: (-x[1], x[0]))[0][0]
        return answer
class Solution:
    def maxDivScore(self, nums: List[int], divisors: List[int]) -> int:
        answer = 0
        d = {}
        for divisor in divisors:
            score = 0
            for num in nums:
                if num % divisor == 0:
                    score += 1
            d[divisor] = score
        answer = float("inf")
        for key, value in d.items():
            if value == max(d.values()):
                if key < answer:
                    answer = key
        return answer
class Solution:
    def maxDivScore(self, nums: List[int], divisors: List[int]) -> int:
        divisors.sort()
        div = []
        for d in range(len(divisors)):
            cnt = 0
            for j in range(len(nums)):
                if nums[j] % divisors[d] == 0:
                    cnt += 1
            div.append(cnt)
        i = div.index(max(div))
        return divisors[i]

https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/

https://leetcode.com/problems/find-the-maximum-divisibility-score/

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

Python - Algorithm #30  (0) 2024.08.01
Python - Algorithm #29  (0) 2024.07.31
Python - Algorithm #27  (0) 2024.07.29
Python - Algorithm #26  (0) 2024.07.26
Python - Algorithm #25  (0) 2024.07.25