2026-05-06:采購的最小花費。用go語言,你有 5 個整數:cost1、cost2、costBoth、need1、need2。
現在你可以購買三種物品來湊需求:
1. 物品A:價格是 cost1,只能用于滿足“需求1”,每買一個提供 1 單位需求1。
2. 物品B:價格是 cost2,只能用于滿足“需求2”,每買一個提供 1 單位需求2。
3. 物品C:價格是 costBoth,同時滿足兩種需求:每買一個會讓需求1減少 1 且需求2減少 1(等
價于同時提供 1 單位需求1和 1 單位需求2)。
你的目標是:
? 讓總的需求1滿足數量至少達到 need1
? 讓總的需求2滿足數量至少達到 need2
在滿足這兩條的前提下,計算總花費的最小值。
1 <= cost1, cost2, costBoth <= 1000000。
0 <= need1, need2 <= 1000000000。
輸入: cost1 = 5, cost2 = 4, costBoth = 15, need1 = 2, need2 = 3。
輸出: 22。
解釋:
購買 need1 = 2 個類型 1 的物品和 need2 = 3 個類型 2 的物品,總花費為 2 * 5 + 3 * 4 = 10 + 12 = 22。
任何其他有效的購買方案都會花費更多,因此最小總花費為 22。
題目來自力扣3789。
一、整體思路
要同時滿足需求1=need1、需求2=need2,有三種購買方案:
? 只買A、B,不買C;
? 全買C,不買A、B;
? 買一部分C,剩下不足的用A或B補。
在這三種方案里選花費最小的即可。
? cost1:A的單價(只供需求1)
? cost2:B的單價(只供需求2)
? costBoth:C的單價(同時供1和2各1)
? need1:需求1至少要滿足的數量
? need2:需求2至少要滿足的數量
? 買 need1 個 A:花費 = cost1 × need1
? 買 need2 個 B:花費 = cost2 × need2
? 總花費 res1 = cost1×need1 + cost2×need2
? 特點:一定合法,但不一定最便宜。
? 如果 need1 > need2:
? 交換 need1、need2
? 交換 cost1、cost2(相當于把“需求小的”統一放到前面,邏輯不變)
? 目的:簡化后續“買C”的計算,只需要考慮need1 ≤ need2的情況。
? 示例(原題):need1=2、need2=3,無需交換。
? C一次解決1和2各1,最多只能買need2 個(因為 need2 更大)
? 買 need2 個 C:花費 res2 = costBoth × need2
? 特點:滿足需求1=need2(≥原need1)、需求2=need2,合法;但C可能很貴。
? 先買need1 個 C:解決全部需求1、同時解決 need1 個需求2
? 需求2還剩:need2 ? need1 個
? 剩下的需求2用單價更低的那個單品補(此時 cost2 已經是歸一化后對應大需求的單價)
? 總花費 res3 = costBoth×need1 + cost2×(need2?need1)
? 特點:合法,通常在C適中時最優。
? 在 res1、res2、res3 中選最小的
? 轉成 int64(防止大數溢出)返回
? 原題示例:
? res1 = 5×2 + 4×3 = 22
? res2 = 15×3 = 45
? res3 = 15×2 + 4×1 = 34
? 最小為 22,輸出 22
?時間復雜度:O(1)
? 只有幾次算術運算、比較、交換,與輸入大小無關。
?額外空間復雜度:O(1)
? 只用到有限幾個變量(res1/res2/res3、臨時交換變量),不隨輸入增長。
1. 只需要比較三種固定方案,無需循環/枚舉;
2. 歸一化(need1 ≤ need2)是簡化邏輯的核心;
3. 所有運算都是常數級,能處理 1e9 級的超大需求;
4. 本質是貪心:在“全單品、全套餐、套餐+補單品”里選最優。
要不要我再給你幾組測試用例,幫你驗證這個邏輯在不同價格和需求下是否正確?
Go完整代碼如下:
package main
import (
"fmt"
)
func minimumCost(cost1, cost2, costBoth, need1, need2 int)int64 {
res1 := cost1*need1 + cost2*need2 // 各買各的
if need1 > need2 {
need1, need2 = need2, need1
cost2 = cost1
}
res2 := costBoth * need2 // 我包了
res3 := costBoth*need1 + cost2*(need2-need1) // 混合策略
returnint64(min(res1, res2, res3))
}func main() {
cost1 := 5
cost2 := 4
costBoth := 15
need1 := 2
need2 := 3
result := minimumCost(cost1, cost2, costBoth, need1, need2)
fmt.Println(result)
}
Python完整代碼如下:
# -*-coding:utf-8-*-
def minimum_cost(cost1: int, cost2: int, cost_both: int, need1: int, need2: int) -> int:
# 各買各的
res1 = cost1 * need1 + cost2 * need2
# 確保 need1 <= need2 以便混合策略計算
if need1 > need2:
need1, need2 = need2, need1
cost2 = cost1 # 注意:交換后 cost2 需要同步更新
# 全買雙人票
res2 = cost_both * need2
# 混合策略:部分買雙人票,部分買單人票
res3 = cost_both * need1 + cost2 * (need2 - need1)
return min(res1, res2, res3)
def main():
cost1 = 5
cost2 = 4
cost_both = 15
need1 = 2
need2 = 3
result = minimum_cost(cost1, cost2, cost_both, need1, need2)
print(result)if __name__ == "__main__":
main()
C++完整代碼如下:
using namespace std;
long long minimumCost(int cost1, int cost2, int costBoth, int need1, int need2) {
// 各買各的
long long res1 = 1LL * cost1 * need1 + 1LL * cost2 * need2;
// 確保 need1 <= need2 以便混合策略計算
if (need1 > need2) {
swap(need1, need2);
cost2 = cost1;
}
// 全買雙人票
long long res2 = 1LL * costBoth * need2;
// 混合策略:部分買雙人票,部分買單人票
long long res3 = 1LL * costBoth * need1 + 1LL * cost2 * (need2 - need1);
return min({res1, res2, res3});
}
int main() {
int cost1 = 5;
int cost2 = 4;
int costBoth = 15;
int need1 = 2;
int need2 = 3;
long long result = minimumCost(cost1, cost2, costBoth, need1, need2);
cout << result << endl;return0;
}
我們相信人工智能為普通人提供了一種“增強工具”,并致力于分享全方位的AI知識。在這里,您可以找到最新的AI科普文章、工具評測、提升效率的秘籍以及行業洞察。 歡迎關注“福大大架構師每日一題”,發消息可獲得面試資料,讓AI助力您的未來發展。
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.