638. 大禮包

638. 大禮包

在 LeetCode 商店中, 有 n 件在售的物品。每件物品都有對應的價格。然而,也有一些大禮包,每個大禮包以優惠的價格捆綁銷售一組物品。

給你一個整數數組 price 表示物品價格,其中 price[i] 是第 i 件物品的價格。另有一個整數數組 needs 表示購物清單,其中 needs[i] 是需要購買第 i 件物品的數量。

還有一個數組 special 表示大禮包,special[i] 的長度為 n + 1 ,其中 special[i][j] 表示第 i 個大禮包中內含第 j 件物品的數量,且 special[i][n] (也就是數組中的最后一個整數)為第 i 個大禮包的價格。

返回 確切 滿足購物清單所需花費的最低價格,你可以充分利用大禮包的優惠活動。你不能購買超出購物清單指定數量的物品,即使那樣會降低整體價格。任意大禮包可無限次購買。

示例 1:輸入:price = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2]
輸出:14
解釋:有 A 和 B 兩種物品,價格分別為 ¥2 和 ¥5 。 
大禮包 1 ,你可以以 ¥5 的價格購買 3A 和 0B 。 
大禮包 2 ,你可以以 ¥10 的價格購買 1A 和 2B 。 
需要購買 3 個 A 和 2 個 B , 所以付 ¥10 購買 1A 和 2B(大禮包 2),以及 ¥4 購買 2A 。
示例 2:輸入:price = [2,3,4], special = [[1,1,0,4],[2,2,1,9]], needs = [1,2,1]
輸出:11
解釋:A ,B ,C 的價格分別為 ¥2 ,¥3 ,¥4 。
可以用 ¥4 購買 1A 和 1B ,也可以用 ¥9 購買 2A ,2B 和 1C 。 
需要買 1A ,2B 和 1C ,所以付 ¥4 買 1A 和 1B(大禮包 1),以及 ¥3 購買 1B , ¥4 購買 1C 。 
不可以購買超出待購清單的物品,盡管購買大禮包 2 更加便宜。

提示:

  • n == price.length
  • n == needs.length
  • 1 <= n <= 6
  • 0 <= price[i] <= 10
  • 0 <= needs[i] <= 10
  • 1 <= special.length <= 100
  • special[i].length == n + 1
  • 0 <= special[i][j] <= 50

解題思路

  1. 預處理special數組,如果大禮包內所有的物品數量都為0或者大禮包的單價小于所有物品的總價,則將其剔除出去。
  2. 使用深度優先搜索,每一次遞歸就是選擇任意一份大禮包,或者不購買大禮包,全部按所需物品的單價購買,計算購買該大禮包以后,仍每種物品仍然需要的數量大小,進行下一次遞歸,如果超出購物清單指定數量的物品,則不進行遞歸,返回最小花費的組合方式。
  3. 使用記憶化搜索,使用map記錄下,所需物品清單和其花費最低價格的映射,避免重復計算某種所需物品清單的最低價格。

代碼

class Solution {Map<List<Integer>, Integer> map = new HashMap<>();public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {List<List<Integer>> filter = new ArrayList<>();for (List<Integer> integerList : special) {int sum = 0, c = 0;for (int i = 0; i < integerList.size() - 1; i++) {sum += price.get(i) * integerList.get(i);c += integerList.get(i);}if (integerList.get(integerList.size() - 1) < sum && c > 0)filter.add(integerList);}return dfs(price, needs, filter);}public int dfs(List<Integer> price, List<Integer> needs, List<List<Integer>> special) {if (map.containsKey(needs)) {return map.get(needs);}int p = 0, n =  needs.size();for (int i = 0; i < needs.size(); i++) {p += needs.get(i) * price.get(i);}for (List<Integer> spec : special) {ArrayList<Integer> next = new ArrayList<>();for (int i = 0; i < n; i++) {if (spec.get(i) > needs.get(i)) {break;}next.add(needs.get(i) - spec.get(i));}if (next.size() == n)p = Math.min(p, spec.get(n) + dfs(price, next, special));}map.put(needs, p);return p;}}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389917.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389917.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389917.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

記錄一次spark連接mysql遇到的問題

在使用spark連接mysql的過程中報錯了&#xff0c;錯誤如下 08:51:32.495 [main] ERROR - Error loading factory org.apache.calcite.jdbc.CalciteJdbc41Factory java.lang.NoClassDefFoundError: org/apache/calcite/linq4j/QueryProviderat java.lang.ClassLoader.defineCla…

什么事數據科學_如果您想進入數據科學,則必須知道的7件事

什么事數據科學No way. No freaking way to enter data science any time soon…That is exactly what I thought a year back.沒門。 很快就不會出現進入數據科學的怪異方式 ……這正是我一年前的想法。 A little bit about my data science story: I am a complete beginner…

python基礎03——數據類型string

1. 字符串介紹 在python中&#xff0c;引號中加了引號的字符都被認為是字符串。 1 namejim 2 address"beijing" 3 msg My name is Jim, I am 22 years old! 那單引號、雙引號、多引號有什么區別呢&#xff1f; 1) 單雙引號木有任何區別&#xff0c;部分情況 需要考慮…

Java基礎-基本數據類型

Java中常見的轉義字符: 某些字符前面加上\代表了一些特殊含義: \r :return 表示把光標定位到本行行首. \n :next 表示把光標定位到下一行同樣的位置. 單獨使用在某些平臺上會產生不同的效果.通常這兩個一起使用,即:\r\n. 表示換行. \t :tab鍵,長度上相當于四個或者是八個空格 …

季節性時間序列數據分析_如何指導時間序列數據的探索性數據分析

季節性時間序列數據分析為什么要進行探索性數據分析&#xff1f; (Why Exploratory Data Analysis?) You might have heard that before proceeding with a machine learning problem it is good to do en end-to-end analysis of the data by carrying a proper exploratory …

TortoiseGit上傳項目到GitHub

1. 簡介 gitHub是一個面向開源及私有軟件項目的托管平臺&#xff0c;因為只支持git 作為唯一的版本庫格式進行托管&#xff0c;故名gitHub。 2. 準備 2.1 安裝git&#xff1a;https://git-scm.com/downloads。無腦安裝 2.2 安裝TortoiseGit(小烏龜)&#xff1a;https://torto…

496. 下一個更大元素 I

496. 下一個更大元素 I 給你兩個 沒有重復元素 的數組 nums1 和 nums2 &#xff0c;其中nums1 是 nums2 的子集。 請你找出 nums1 中每個元素在 nums2 中的下一個比其大的值。 nums1 中數字 x 的下一個更大元素是指 x 在 nums2 中對應位置的右邊的第一個比 x 大的元素。如果…

利用PHP擴展Taint找出網站的潛在安全漏洞實踐

一、背景 筆者從接觸計算機后就對網絡安全一直比較感興趣&#xff0c;在做PHP開發后對WEB安全一直比較關注&#xff0c;2016時無意中發現Taint這個擴展&#xff0c;體驗之后發現確實好用&#xff1b;不過當時在查詢相關資料時候發現關注此擴展的人數并不多&#xff1b;最近因為…

美團騎手檢測出虛假定位_在虛假信息活動中檢測協調

美團騎手檢測出虛假定位Coordination is one of the central features of information operations and disinformation campaigns, which can be defined as concerted efforts to target people with false or misleading information, often with some strategic objective (…

869. 重新排序得到 2 的冪

869. 重新排序得到 2 的冪 給定正整數 N &#xff0c;我們按任何順序&#xff08;包括原始順序&#xff09;將數字重新排序&#xff0c;注意其前導數字不能為零。 如果我們可以通過上述方式得到 2 的冪&#xff0c;返回 true&#xff1b;否則&#xff0c;返回 false。 示例 …

org.apache.maven.archiver.MavenArchiver.getManifest

eclipse導入新的maven項目時&#xff0c;pom.xml第一行報錯&#xff1a; org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration) 解決辦法&#xff1a; help -> Install New…

殺進程常用命令

殺進程命令pkill 進程名killall 進程名 # 平緩kill -HUP pid # 平緩kill -USR2 pidkill pid &#xff08;-9 不要使用&#xff09;轉載于:https://www.cnblogs.com/jmaly/p/9492406.html

CertUtil.exe被利用來下載惡意軟件

1、前言 經過國外文章信息&#xff0c;CertUtil.exe下載惡意軟件的樣本。 2、實現原理 Windows有一個名為CertUtil的內置程序&#xff0c;可用于在Windows中管理證書。使用此程序可以在Windows中安裝&#xff0c;備份&#xff0c;刪除&#xff0c;管理和執行與證書和證書存儲相…

335. 路徑交叉

335. 路徑交叉 給你一個整數數組 distance 。 從 X-Y 平面上的點 (0,0) 開始&#xff0c;先向北移動 distance[0] 米&#xff0c;然后向西移動 distance[1] 米&#xff0c;向南移動 distance[2] 米&#xff0c;向東移動 distance[3] 米&#xff0c;持續移動。也就是說&#x…

回歸分析假設_回歸分析假設的最簡單指南

回歸分析假設The Linear Regression is the simplest non-trivial relationship. The biggest mistake one can make is to perform a regression analysis that violates one of its assumptions! So, it is important to consider these assumptions before applying regress…

Spring Aop之Advisor解析

2019獨角獸企業重金招聘Python工程師標準>>> 在上文Spring Aop之Target Source詳解中&#xff0c;我們講解了Spring是如何通過封裝Target Source來達到對最終獲取的目標bean進行封裝的目的。其中我們講解到&#xff0c;Spring Aop對目標bean進行代理是通過Annotatio…

react事件處理函數中綁定this的bind()函數

問題引入 import React, { Component } from react; import {Text,View } from react-native;export default class App extends Component<Props> {constructor(props){super(props)this.state{times:0}this.timePlusthis.timePlus.bind(this);}timePlus(){let timethis…

301. 刪除無效的括號

301. 刪除無效的括號 給你一個由若干括號和字母組成的字符串 s &#xff0c;刪除最小數量的無效括號&#xff0c;使得輸入的字符串有效。 返回所有可能的結果。答案可以按 任意順序 返回。 示例 1&#xff1a; 輸入&#xff1a;s “()())()” 輸出&#xff1a;["(())…

為什么隨機性是信息

用位思考 (Thinking in terms of Bits) Imagine you want to send outcomes of 3 coin flips to your friends house. Your friend knows that you want to send him those messages but all he can do is get the answer of Yes/No questions arranged by him. Lets assume th…

Chrome無法播放m3u8格式的直播視頻流的問題解決

出國&#xff0c;然后安裝這個插件即可&#xff1a;Native HLS Playback https://chrome.google.com/webstore/detail/native-hls-playback/emnphkkblegpebimobpbekeedfgemhof?hlzh-CN轉載于:https://www.cnblogs.com/EasonJim/p/8737001.html