We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is my solution in Swift language with O(n) complexity
// Complete the flatlandSpaceStations function below.funcflatlandSpaceStations(n:Int,c:[Int])->Int{vardistances=Array<Int>(repeating:n,count:n)// Set location with stations to zeroforindexinc{distances[index]=0}// Update distances from left to rightforindexin1..<distances.count{ifdistances[index]!=0{distances[index]=distances[index-1]+1}}// Update min distance from right to leftforindexin(0..<distances.count-1).reversed(){vardistance=0ifdistances[index]!=0{distance=distances[index+1]+1}distances[index]=min(distances[index],distance)}// Find maxletnumMax=distances.reduce(0,{max($0,$1)})returnnumMax}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Flatland Space Stations
You are viewing a single comment's thread. Return to all comments →
Here is my solution in Swift language with O(n) complexity