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

Python - Algorithm #43

hkl22 2024. 8. 31. 17:01

Python Algorithm

LeetCode

1317. Convert Integer to the Sum of Two No-Zero Integers

  • No-Zero integer is a positive integer that does not contain any 0 in its decimal representation.
  • Given an integer n, return a list of two integers [a, b] where:
    • a and b are No-Zero integers.
    • a + b = n
  • The test cases are generated so that there is at least one valid solution. If there are many valid solutions, you can return any of them.
  • Constraints
    • 2 n 10⁴
class Solution:
    def getNoZeroIntegers(self, n: int) -> List[int]:
        # for
        # 숫자를 문자열로 바꿔주기
        for i in range(1, n):
            a = i
            b = n - a
            # a와 b에 0이 포함되어 있지 않으면 
            # [a, b]를 answer로 지정하고 break
            if "0" not in str(a) and "0" not in str(n):
                return [a, b]
class Solution:
    def getNoZeroIntegers(self, n: int) -> List[int]:
        for i in range(1, n):
            if "0" not in str(i) and "0" not in str(n-i):
                return [i, n-i]

3065. Minimum Operations to Exceed Threshold Value I

  • You are given a 0-indexed integer array [nums], and an integer k.
  • In one operation, you can remove one occurrence of the smallest element of [nums].
  • Return the minimum number of operations needed so that all elements of the array are greater than or equal to k.
  • Constraints
    • 1 nums.length 50
    • 1 nums[i] 10⁹
    • 1 k 10
    • The input is generated such that there is at least one index i such that nums[i] ≥ k.
class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
        # sorted
        answer = 0
        nums = sorted(nums)
        for i in range(len(nums)):
            if nums[i] >= k:
                return i
class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
        return len([num for num in nums if num < k])
class Solution:
    def minOperations(self, nums: List[int], k: int) -> int:
        answer = 0
        for num in nums:
            if num < k:
                answer += 1
        return answer

https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/

https://leetcode.com/problems/minimum-operations-to-exceed-threshold-value-i/

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

Python - Algorithm #45  (0) 2024.09.03
Python - Algorithm #44  (0) 2024.09.02
Python - Algorithm #42  (0) 2024.08.29
Python - Algorithm #41  (0) 2024.08.28
Python - Algorithm #40  (0) 2024.08.27