ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Shipping Cost Function

Complete the function that prices a package from its weight in ounces and the delivery speed.

สิ่งที่คุณจะได้เรียนรู้

  • Validate inputs before computing
  • Count started blocks with ceiling division
  • Apply a multiplier based on a boolean

โจทย์

ดูฉบับแปล

Complete the function for an online craft store that returns the shipping price of a package in cents. The store's rules are:

  • Packages up to 16 ounces cost 500 cents.
  • Above 16 ounces, add 125 cents for every started 8 ounces beyond the first 16 (17 to 24 ounces adds 125, 25 to 32 adds 250, and so on).
  • Express delivery doubles the whole price.
  • The carrier only takes packages from 1 ounce up to 1120 ounces (70 pounds).

Implement shipping_cost_function(weight_ounces, express).

Input

weight_ounces: The weight of the package in whole ounces.

express: true for express delivery, false for standard.

Output

Return the price in cents. If the weight is below 1 or above 1120, return -1.

เงื่อนไข

−10,000 ≤ weight_ounces ≤ 10,000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
weight_ounces = 30
express = False

ผลลัพธ์ที่คาดหวัง:
expected = 750
อินพุต:
long long weight_ounces = 30;
bool express = false;

ผลลัพธ์ที่คาดหวัง:
long long expected = 750;
อินพุต:
long weight_ounces = 30;
bool express = false;

ผลลัพธ์ที่คาดหวัง:
long expected = 750;

คำอธิบาย

30 ounces is 14 ounces over 16, which starts 2 blocks of 8 ounces: 500 + 2 × 125 = 750 cents.

ตัวอย่างที่ 2
อินพุต:
weight_ounces = 10
express = True

ผลลัพธ์ที่คาดหวัง:
expected = 1000
อินพุต:
long long weight_ounces = 10;
bool express = true;

ผลลัพธ์ที่คาดหวัง:
long long expected = 1000;
อินพุต:
long weight_ounces = 10;
bool express = true;

ผลลัพธ์ที่คาดหวัง:
long expected = 1000;

คำอธิบาย

10 ounces costs the base 500 cents, doubled for express: 1000.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Return -1 right away for weights outside 1 to 1120.

คำใบ้ 2

Started blocks of 8 over 16 ounces is (extra + 7) / 8 with integer division.

คำใบ้ 3

Double the final total, not just the extra charge, for express.

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

O(1) time, O(1) extra space

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

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

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

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

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

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

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก