2357. Make Array Zero by Subtracting Equal Amounts
LeetCode
https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts/
문제
- 양의 정수 배열 nums가 주어졌습니다.
- 한 번의 작업으로 nums에서 0이 아닌 가장 작은 값을 선택하여 nums의 모든 요소를 해당 값으로 빼줍니다.
- nums의 모든 요소를 0으로 만드는 데 필요한 최소 작업 횟수를 반환하는 코드를 작성하세요.
예제
- Input: nums = [1, 5, 0, 3, 5]
- Output: 3
풀이 과정 #1
- 집합 컴프리헨션을 사용하여 주어진 배열 nums에서 0이 아닌 모든 요소를 필터링합니다.
- 필터링된 요소들의 개수를 반환합니다.
코드(Python) #1
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
return len({num for num in nums if num})
풀이 과정 #2
- 주어진 배열 nums에서 0보다 큰 요소를 필터링하여 집합으로 생성합니다.
- 집합의 요소 개수를 반환합니다.
코드(Python) #2
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
return len(set(filter(lambda x: x > 0, nums)))
참고
- filter(함수, 객체) : 주어진 함수의 조건을 만족하는 요소만 포함하는 객체 반환
'알고리즘 스터디 > Python' 카테고리의 다른 글
| [LeetCode] 2682. Find the Losers of the Circular Game (0) | 2024.09.22 |
|---|---|
| [LeetCode] 2124. Check if All A's Appears Before All B's (0) | 2024.09.20 |
| [LeetCode] 2351. First Letter to Appear Twice (0) | 2024.09.16 |
| [LeetCode] 1385. Find the Distance Value Between Two Arrays (0) | 2024.09.12 |
| [LeetCode] 1331. Rank Transform of an Array (0) | 2024.09.12 |