n-1 roads between n toll stations, each road has associated cost of travel, design a data structure that required O(n) space O(1) time to compute the cost from one station to the other station
suppose the index of station is from 0 to n, the index of road is from 1 to n
a[0]=0;//a[] is prefix sum cost array
For[1…n]
a[i]=a[i-1]+c[i]
Cost of ith road = a[i+1]-a[i]
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment