Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 잇츠디모
- python 알고리즘
- 파이썬알고리즘풀기
- leetcode 풀기
- 릿코드풀기
- python xor
- 파이썬 알고리즘
- python zip_longest
- 릿코드 풀기
- python priority queue
- 코틀린기초
- binary search
- 파이썬알고리즘
- 파이썬릿코드풀기
- leetcode풀기
- python 릿코드
- 파이썬릿코드
- 릿코드
- 알고리즘풀기
- 상가수익률계산기
- 파이썬 릿코드
- 릿코드풀이
- python sorted
- leetcode풀이
- 릿코드 파이썬
- 파이썬 알고리즘 풀기
- 파이썬 프로그래머스
- 알고리즘풀이
- LeetCode
- python Leetcode
Archives
- Today
- Total
소프트웨어에 대한 모든 것
2389. Longest Subsequence With Limited Sum 본문
반응형
문제)
2389. Longest Subsequence With Limited Sum
You are given an integer array nums of length n, and an integer array queries of length m.
Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements is less than or equal to queries[i].
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [4,5,2,1], queries = [3,10,21]
Output: [2,3,4]
Explanation: We answer the queries as follows:
- The subsequence [2,1] has a sum less than or equal to 3. It can be proven that 2 is the maximum size of such a subsequence, so answer[0] = 2.
- The subsequence [4,5,1] has a sum less than or equal to 10. It can be proven that 3 is the maximum size of such a subsequence, so answer[1] = 3.
- The subsequence [4,5,2,1] has a sum less than or equal to 21. It can be proven that 4 is the maximum size of such a subsequence, so answer[2] = 4.
Example 2:
Input: nums = [2,3,4,5], queries = [1]
Output: [0]
Explanation: The empty subsequence is the only subsequence that has a sum less than or equal to 1, so answer[0] = 0.
Constraints:
- n == nums.length
- m == queries.length
- 1 <= n, m <= 1000
- 1 <= nums[i], queries[i] <= 106
솔루션1)
- 재귀로 풀었더니 TLE 발생
class Solution:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
nums_len = len(nums)
def recur(idx, subsequence_sum, subsequence_len, target):
if subsequence_sum > target:
return subsequence_len - 1
if idx >= nums_len:
return subsequence_len
return max(recur(idx + 1, subsequence_sum + nums[idx], subsequence_len + 1, target),
recur(idx + 1, subsequence_sum, subsequence_len, target))
ret = []
for target in queries:
ret.append(recur(0, 0, 0, target))
return ret
솔루션2)
- nums를 정렬하고 accumulate() 사용해서 누적합 리스트를 생성
- 이진 탐색을 이용해서 문제 해결
문제를 풀고나면 이렇게도 풀리는구나.. 너무 허탈...
문제가 막혀있을 때 심플한 방법을 계속 고민하자!
class Solution:
def answerQueries(self, nums: List[int], queries: List[int]) -> List[int]:
# 오름차순 정렬
nums = sorted(nums)
# 누적합
nums = list(accumulate(nums))
# 이진탐색. 실패 시 오른쪽 인덱스 반환
return [bisect.bisect_right(nums, target) for target in queries]
반응형
'알고리즘 > LeetCode' 카테고리의 다른 글
2103. Rings and Rods (0) | 2022.12.27 |
---|---|
1365. How Many Numbers Are Smaller Than the Current Number (0) | 2022.12.25 |
2418. Sort the People (0) | 2022.12.24 |
1318. Minimum Flips to Make a OR b Equal to c (0) | 2022.12.24 |
2367. Number of Arithmetic Triplets (0) | 2022.12.24 |
Comments