Merged
Conversation
[638] Shopping Offers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[638] Shopping Offers
문제 설명
In LeetCode Store, there are
nitems to sell. Each item has a price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.You are given an integer array
pricewhereprice[i]is the price of theithitem, and an integer arrayneedswhereneeds[i]is the number of pieces of theithitem you want to buy.You are also given an array
specialwherespecial[i]is of sizen + 1wherespecial[i][j]is the number of pieces of thejthitem in theithoffer andspecial[i][n](i.e., the last integer in the array) is the price of theithoffer.Return the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers. You are not allowed to buy more items than you want, even if that would lower the overall price. You could use any of the special offers as many times as you want.
Example 1:
Example 2:
코드 설명
같은 needs 상태를 여러 번 계산하면 시간 초과
그래서 이미 계산된 상태를 저장
LeetCode에서 호출하는 진입점
내부적으로 dfs 호출
현재 needs 상태에서 만들 수 있는 최소 비용을 반환
상태 문자열 변환
중복 계산 방지
→ 시간복잡도 대폭 감소
special 안 쓰고 그냥 다 정가로 사는 경우
각 special offer 시도
offer가 요구 수량보다 많으면
불가능
현재 offer 가격
남은 상품 최소 비용
최소값 갱신
이 상태는 다시 계산하지 않도록 저장