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

Bank Queue Simulation

Simulate customers waiting in one line for several bank tellers and report the waiting times.

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

  • Serve customers in first-come, first-served order
  • Track when each server becomes free
  • Collect summary statistics from a simulation

โจทย์

ดูฉบับแปล

A bank has tellers tellers and a single waiting line. Customer i arrives at minute arrivals[i] and needs service[i] minutes at a teller. Customers are served strictly in arrival order (the order of the lists). When a customer reaches the front of the line, they go to the teller who becomes free the earliest (the lowest numbered one on a tie), and start at the later of their arrival time and that teller's free time. A teller is free from minute 0.

A customer's wait is their start time minus their arrival time.

Implement bank_queue_simulation(arrivals, service, tellers).

Input

arrivals: Arrival minutes in non-decreasing order.

service: Service minutes for each customer, in the same order.

tellers: The number of tellers.

Output

Return [total_wait, longest_wait, last_finish]: the sum of all waits, the largest single wait, and the minute the last customer finishes. With no customers return [0, 0, 0].

เงื่อนไข

0 ≤ number of customers ≤ 2,000

0 ≤ arrival ≤ 100,000

1 ≤ service ≤ 1,000

1 ≤ tellers ≤ 20

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
arrivals = [0, 1, 2, 3]
service = [5, 3, 4, 2]
tellers = 2

ผลลัพธ์ที่คาดหวัง:
expected = [4, 2, 8]
อินพุต:
std::vector<long long> arrivals = {0, 1, 2, 3};
std::vector<long long> service = {5, 3, 4, 2};
long long tellers = 2;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {4, 2, 8};
อินพุต:
long[] arrivals = new long[] {0, 1, 2, 3};
long[] service = new long[] {5, 3, 4, 2};
long tellers = 2;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {4, 2, 8};

คำอธิบาย

Customers 1 and 2 start at once (0 and 1). Teller 2 frees at 4, so customer 3 waits 2; teller 1 frees at 5, so customer 4 waits 2. Last finish is max(8, 7) = 8: [4, 2, 8].

ตัวอย่างที่ 2
อินพุต:
arrivals = [0, 0, 0]
service = [4, 4, 4]
tellers = 1

ผลลัพธ์ที่คาดหวัง:
expected = [12, 8, 12]
อินพุต:
std::vector<long long> arrivals = {0, 0, 0};
std::vector<long long> service = {4, 4, 4};
long long tellers = 1;

ผลลัพธ์ที่คาดหวัง:
std::vector<long long> expected = {12, 8, 12};
อินพุต:
long[] arrivals = new long[] {0, 0, 0};
long[] service = new long[] {4, 4, 4};
long tellers = 1;

ผลลัพธ์ที่คาดหวัง:
long[] expected = new long[] {12, 8, 12};

คำอธิบาย

With one teller the waits are 0, 4 and 8, and the last customer finishes at 12: [12, 8, 12].

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

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

คำใบ้ 1

Keep an array with the minute each teller becomes free.

คำใบ้ 2

For each customer pick the smallest free time (lowest index on a tie).

คำใบ้ 3

Start time is the later of arrival and that free time; the wait is start minus arrival.

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

O(n × k) time, O(k) extra space

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

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

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

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

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

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

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