-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum_optimized.py
More file actions
50 lines (36 loc) Β· 1.8 KB
/
Copy pathtwoSum_optimized.py
File metadata and controls
50 lines (36 loc) Β· 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import time
def find_two_sum_optimized(price_list, target_budget):
print(f"β‘ Running Hash Map Optimization for target sum: ${target_budget}...")
#this is our magical memory card (Hash Map).
seen_prices = {}
# Just a single loop (O(N) Time Complexity β super fast)
for index, current_price in enumerate(price_list):
required_partner = target_budget - current_price
# Check the memory card to see if the number we need is already there.
if required_partner in seen_prices:
return [seen_prices[required_partner], index]
# If it is not there, save the current number along with its position to the memory card.
seen_prices[current_price] = index
return []
def run_optimized_session():
print("π Initiating Sudhir's Premium O(N) Algorithm Optimization...\n")
laptop_prices = [400, 900, 1200, 500, 800]
target_budget = 1700
start_time = time.perf_counter()
# Calling optimized algorithms
indices = find_two_sum_optimized(laptop_prices, target_budget)
end_time = time.perf_counter()
duration = (end_time - start_time) * 1000
print("-" * 65)
if indices:
print(f"π― OPTIMIZED MATCH FOUND SUCCESSFULY!")
print(f"π Perfect Array Indices Located: {indices}")
print(f"π» Smart Pair: ${laptop_prices[indices[0]]} + ${laptop_prices[indices[1]]} = ${target_budget}")
else:
print("β Budget requirement pair not found.")
print(f"β‘ Optimized Execution Time: {duration:.4f} ms")
print(f"π Computational Complexity: O(N) - Linear Time Efficiency")
print("-" * 65)
print("\nπ High-Performance Architecture executed with 100% memory efficiency!")
if __name__ == "__main__":
run_optimized_session()