Sort by

recency

|

927 Discussions

|

  • + 0 comments
    // Complete the flatlandSpaceStations function below.
        static int flatlandSpaceStations(int n, int[] c) {
    
        	int numberOfCities = n;
        	int numberOfStations = c.length;
        	
        	// Each city can have one station. If numberOfCities == numberOfStations. Each city has station so distance will be 0
        	if (numberOfCities == numberOfStations) {
        		return 0;
        	}
        	
        	// If there is only station subtract 1 from numberOfCities
        	if (numberOfStations == 1) {
        		return numberOfCities - 1;
        	}
        	
        	/**
        	 * By default Priority queue gives priority to smaller elements. By using priority Queue we will be sure that we will always get the station in the 
        	 * same order as we are traversing the cities.
        	 * 
        	 * Suppose stations are given in order [4, 7, 1]. We will start iterating from city 0. City 0 till city 4 should check distance with spaceStationCity 1
        	 * and spaceStationCity 4.
        	 * 
        	 * After passing spaceStationCity 4 cities should check distance with spaceStationCity 4 and spaceStationCity 7.
        	 */
        	PriorityQueue<Integer> spaceStationsCityPriorityQueue = new PriorityQueue<>();
        	for (int spaceStationCity : c) {
        		spaceStationsCityPriorityQueue.add(spaceStationCity);
        	}
        	
        	// At this point we know that there are atl-east 2 space stations
        	int spaceStationCity1 = spaceStationsCityPriorityQueue.poll();
        	int spaceStationCity2 = spaceStationsCityPriorityQueue.poll();
        	
        	// We can also use max variable and check it instead of creating the array. But let's do it with array as questions is also showing the array.
        	int[] distanceArr = new int[n];
        	
        	/**
        	 * 
        	 * We have taken the first two stations. We are using priority queue. We know we will get the stations in order.
        	 * Start from city 0.
        	 * Check the city distance from space station 1 and space station 2.
        	 * Find the minimum of both distance.
        	 * Add minimum distance to distance array.
        	 * Once the space station city 2 reaches. Now change the space station city 
        	 * 
        	 */
        	for (int i = 0; i < numberOfCities; i++ ) {
        		
        		int city = i;
        		int spaceStationCity1Distance = Math.abs(city - spaceStationCity1); 
        		int spaceStationCity2Distance = Math.abs(city - spaceStationCity2); 
        		
        		int minSpaceStationDistance = Math.min(spaceStationCity1Distance, spaceStationCity2Distance);
        		distanceArr[i] = minSpaceStationDistance;
        		
        		if (city == spaceStationCity2) {
        			spaceStationCity1 = spaceStationCity2;
        			if (spaceStationsCityPriorityQueue.isEmpty()) {
        				spaceStationCity2 = 0;
        			} else {
        				spaceStationCity2 = spaceStationsCityPriorityQueue.poll();
        			}
        			
        		}
        		
        	}
        	
        	int maxDistance = findMax(distanceArr);
        	return maxDistance;
        
        }
        
        public static int findMax(int[] arr) {
        	
        	int max = Integer.MIN_VALUE;
        	
        	for (int number : arr) {
        		if (number > max) {
        			max = number;
        		}
        	}
        	
        	return max;
        	
        }
    
  • + 0 comments

    Here is my easy c++ solution, you can watch the explanation here : https://youtu.be/k2pd5_9mseI

    int flatlandSpaceStations(int n, vector<int> c) {
        sort(c.begin(), c.end());
        int result = c[0];
        for(int i = 1; i < c.size(); i++){
            result = max(result, (c[i] - c[i-1]) / 2 );
        }
        result = max(result, n - 1 - c[c.size() - 1]);
        return result;
    }
    

    This algorithm is actually O(Nlog(N)) because of the sorting, we can make it O(N) by using a counting sort since we know what the maximum element of our array will be. you will have to replace the line

        sort(c.begin(), c.end());
    

    with this

        c = countingtSort(c, n);
    

    And add this sort function in your editor

    vector<int> countingtSort(vector<int>c, int n) {
        vector<int> arr(n, 0), result;
        for(auto el: c) arr[el] = 1;
        for(int i = 0; i < n; i++) if(arr[i]) result.push_back(i);
        return result;
    }
    
  • + 0 comments
    def flatlandSpaceStations(n, c):
      c.sort()
      for i in range(len(c)):
        if i==0:a=c[i]
        if i==len(c)-1:
          a=max(n-1-c[i],a)
          break
        a=max((c[i+1]-c[i])//2,a)
      return a
    
  • + 0 comments

    def flatlandSpaceStations(n, c): c.sort() end_max = max(c[0], n-1-c[-1]) in_max = max(c[i]-c[i-1] for i in range(1, len(c))) if len(c)!=1 else 0 return max(end_max, in_max//2)

  • + 0 comments
    const flatlandSpaceStations = (n,c) => {
        let maxDistanceList = [];
        for(let city=0;city<n;city++) {
            let spaceStationDistance = c.map((spaceStation) => Math.abs(city - spaceStation));
            maxDistanceList.push(Math.min(...spaceStationDistance));
        }
        return Math.max(...maxDistanceList);
    }