You are given the count of total devices ( n ) which are initially turned off, and an array of operations where each operation represents a range ([start, end]) (both inclusive). Each operation will test all devices in the specified range. You need to determine how many unique devices got tested after performing all given operations.
Assuming typical constraints like ( n \leq 10^9 ) and ( len(operations) \leq 10^5 ), we focus on ensuring an efficient solution.
To determine how many unique devices were tested, we can use a set to track all the device indices that were tested.
This strategy ensures uniqueness inherently because sets do not allow duplicate values.
def countTestedDevices(n, operations):
tested_devices = set()
for start, end in operations:
for device in range(start, end + 1):
tested_devices.add(device)
return len(tested_devices)
# Example Usage
n = 10
operations = [(1, 5), (2, 6), (8, 10)]
print(countTestedDevices(n, operations)) # Output should be 8
This naive approach ensures correctness, but it’s important to note the potential inefficiency in case of large ( n ).
Considering the potential inefficiency, we can optimize using a more advanced approach with a difference array or line sweep technique to avoid explicitly iterating over large ranges. An efficient approach involves:
Here’s the optimized code for efficiency using a marking strategy:
def countTestedDevices(n, operations):
if not operations:
return 0
tested_markers = [0] * (n + 2)
for start, end in operations:
tested_markers[start] += 1
if end + 1 <= n:
tested_markers[end + 1] -= 1
tested_count = 0
current_active_tests = 0
for i in range(1, n + 1):
current_active_tests += tested_markers[i]
if current_active_tests > 0:
tested_count += 1
return tested_count
# Example Usage
n = 10
operations = [(1, 5), (2, 6), (8, 10)]
print(countTestedDevices(n, operations)) # Output should be 8
This is much more efficient for large values of ( n ).
This improved approach balances both clarity and performance, making it suitable for a wide range of input sizes.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?