ง่าย 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

หายอดรวมก่อนส่วนลดให้ได้ก่อน

คำใบ้ 2

ค่อยลดเป็นเปอร์เซ็นต์เมื่อยอดถึงเกณฑ์เท่านั้น

คำใบ้ 3

ให้แน่ใจว่าส่วนลดคำนวณจากทั้งออเดอร์ ไม่ใช่ทีละชิ้น

ความซับซ้อนที่คาดหวัง

เวลา O(n), หน่วยความจำเพิ่ม O(1)

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

# นักโค้ด ภาษา เวลา หน่วยความจำ คะแนนจาก AI คำตอบ
1 LMLaura Mahlow Python 823 ms - 3/5 ดูโค้ด
2 HJhettory Jurash Python 828 ms - 3/5 ดูโค้ด

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

สปอยล์: มีคำตอบเต็ม 2 ข้อด้านล่าง ลองแก้ด้วยตัวเองก่อน คุณยังสามารถดูโค้ดของนักพัฒนาคนอื่นและรีวิว AI ได้ เข้าสู่ระบบและแก้โจทย์เพื่อร่วมพูดคุย