📖 Scenario: You are helping a factory manager decide the minimum time needed to produce a certain number of items using multiple machines working in parallel. Each machine produces items at its own speed.This is like finding the smallest time where the total items produced by all machines meet or exceed the target.
🎯 Goal: Build a Rust program that uses the binary search on answer technique to find the minimum time required to produce at least the target number of items.
📋 What You'll Learn
Create a vector called machines with the exact values [1, 2, 3, 4, 5] representing the time each machine takes to produce one item.
Create a variable called target and set it to 10 representing the total items to produce.
Implement a function called can_produce that takes time and returns true if machines can produce at least target items in that time.
Use binary search on the answer space to find the minimum time to produce at least target items.
Print the minimum time found.
💡 Why This Matters
🌍 Real World
Factories and production lines often need to find the minimum time to meet production goals using machines with different speeds.
💼 Career
This technique is useful for software engineers working on optimization problems, scheduling, and resource allocation.
Progress0 / 4 steps
1
Create the machines vector
Create a vector called machines with the exact values [1, 2, 3, 4, 5] representing the time each machine takes to produce one item.
DSA Rust
Hint
Use let machines = vec![1, 2, 3, 4, 5]; to create the vector.
2
Set the target number of items
Create a variable called target and set it to 10 representing the total items to produce.
DSA Rust
Hint
Use let target = 10; to set the target.
3
Implement the can_produce function
Implement a function called can_produce that takes time: u64, the machines: &Vec, and target: u64, and returns true if the total items produced by all machines in time is at least target. Use integer division to calculate items per machine.
DSA Rust
Hint
Loop over each machine's time and sum time / machine_time. Return true if sum ≥ target, else false.
4
Find and print the minimum time using binary search
Use binary search on the answer space between low = 0 and high = 1_000_000_000_000 to find the minimum time where can_produce(time, &machines, target) is true. Print the minimum time found using println!("{}", min_time);.
DSA Rust
Hint
Use a while loop with low and high. Update min_time when can_produce is true. Print min_time at the end.
Practice
(1/5)
1. What is the main idea behind the Binary Search on Answer technique?
easy
A. We guess an answer and use binary search to find the smallest or largest valid value.
B. We sort the array and then apply binary search to find an element.
C. We use binary search to find the middle element of a sorted list.
D. We apply binary search to find the maximum element in an unsorted array.
Solution
Step 1: Understand the technique purpose
Binary Search on Answer guesses a possible answer and checks if it satisfies the problem's conditions.
Step 2: Use binary search to narrow answer range
By checking mid-values and adjusting search space, we find the smallest or largest valid answer efficiently.
Final Answer:
We guess an answer and use binary search to find the smallest or largest valid value. -> Option A
Hint: Check mid squared against target, adjust low/high accordingly [OK]
Common Mistakes:
Returning mid instead of low after loop
Off-by-one errors in updating low or high
Confusing condition direction (>= vs >)
4. The following Rust code tries to find the maximum x such that x*x <= 20 using binary search on answer. What is the bug?
fn max_x() -> i32 {
let mut low = 0;
let mut high = 20;
while low <= high {
let mid = (low + high) / 2;
if mid * mid <= 20 {
low = mid;
} else {
high = mid - 1;
}
}
high
}
medium
A. The loop condition should be 'low < high' instead of 'low <= high'.
B. The update 'low = mid' can cause infinite loop when low == mid.
C. The function should return 'low' instead of 'high'.
D. The initial 'high' should be 21, not 20.
Solution
Step 1: Analyze loop update statements
When mid equals low, setting low = mid does not change low, causing infinite loop.
Step 2: Identify fix for infinite loop
To avoid infinite loop, update low to mid + 1 when condition holds.
Final Answer:
The update 'low = mid' can cause infinite loop when low == mid. -> Option B
Quick Check:
Update low to mid + 1 to progress loop [OK]
Hint: Always move low or high past mid to avoid infinite loops [OK]
Common Mistakes:
Not updating low or high correctly causing infinite loops
Returning wrong boundary after loop ends
Confusing loop condition operators
5. You want to find the minimum time required to complete all tasks given an array of task durations and a maximum allowed time. Using binary search on answer, which approach correctly finds the minimum maximum time per worker if tasks must be assigned in order and each worker can do consecutive tasks only?
Choose the correct high-level approach.
hard
A. Use binary search to find the worker count, then assign tasks randomly.
B. Sort tasks and binary search for the task with maximum duration to assign first.
C. Binary search the answer range of time, and for each mid, greedily assign tasks to workers checking if all tasks fit within mid time.
D. Apply binary search on the number of tasks, splitting tasks evenly among workers.
Solution
Step 1: Define answer range and check function
We binary search over possible maximum time per worker. For each mid, we check if tasks can be assigned in order without exceeding mid time per worker.
Step 2: Greedy assignment check
Assign tasks to workers greedily, moving to next worker when current sum exceeds mid. If all tasks fit within allowed workers, mid is valid.
Final Answer:
Binary search the answer range of time, and for each mid, greedily assign tasks to workers checking if all tasks fit within mid time. -> Option C