आसान 2 हल किए गए 25.0% स्वीकृति

Cart Total With Discount

Total a shopping cart and apply a discount above a threshold.

आप क्या सीखेंगे

  • मात्रा के साथ कीमतें जोड़ना
  • सीमा से ऊपर छूट लागू करना
  • कोड को सहायक फ़ंक्शन में व्यवस्थित करना

समस्या

Add up the prices in the cart. If the total is at least discount_threshold, take discount_percent percent off the whole total.

Implement cart_total(prices, discount_threshold, discount_percent).

Input

prices: The item prices. discount_threshold: The total that unlocks the discount. discount_percent: The discount in percent.

Output

Return the amount to pay.

सीमाएँ

0 ≤ length ≤ 1,000; 0 ≤ each price ≤ 100,000; 0 ≤ discount_percent ≤ 100

उदाहरण

उदाहरण 1
इनपुट:
prices = [40, 35, 25]
discount_threshold = 100
discount_percent = 10

अपेक्षित आउटपुट:
expected = 90.0
इनपुट:
std::vector<long long> prices = {40, 35, 25};
long long discount_threshold = 100;
long long discount_percent = 10;

अपेक्षित आउटपुट:
double expected = 90.0;
इनपुट:
long[] prices = new long[] {40, 35, 25};
long discount_threshold = 100;
long discount_percent = 10;

अपेक्षित आउटपुट:
double expected = 90.0;

व्याख्या

The total is 100, which reaches the threshold, so 10% off: 90.

उदाहरण 2
इनपुट:
prices = [20, 30]
discount_threshold = 100
discount_percent = 10

अपेक्षित आउटपुट:
expected = 50.0
इनपुट:
std::vector<long long> prices = {20, 30};
long long discount_threshold = 100;
long long discount_percent = 10;

अपेक्षित आउटपुट:
double expected = 50.0;
इनपुट:
long[] prices = new long[] {20, 30};
long discount_threshold = 100;
long discount_percent = 10;

अपेक्षित आउटपुट:
double expected = 50.0;

व्याख्या

The total 50 is below 100, so no discount: 50.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Discount से पहले cart total निकालो।

संकेत 2

Percentage reduction सिर्फ तभी लगाओ जब total threshold तक पहुँचे।

संकेत 3

ध्यान रहे discount पूरे order पर लगे, item by item नहीं।

अपेक्षित जटिलता

समय O(n), अतिरिक्त स्थान O(1)

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

# कोडर भाषा समय मेमोरी AI रिव्यू समाधान
1 LMLaura Mahlow Python 823 ms - 3/5 कोड देखें
2 HJhettory Jurash Python 828 ms - 3/5 कोड देखें

दूसरों से सीखें

समुदाय समाधान

स्पॉइलर अलर्ट: नीचे 2 पूरे समाधान पहले खुद हल करने की कोशिश करें। आप फिर भी अन्य डेवलपर्स का कोड और AI समीक्षा देख सकते हैं। चर्चा में शामिल होने के लिए साइन इन करें और चुनौती हल करें।