Sort by

recency

|

951 Discussions

|

  • + 0 comments

    Java 8 solution:

    static int flatlandSpaceStations(int n, int[] c) {
            // Sort space station location in ascending order
            Arrays.sort(c);
            
            // Set max distance intially as the distance between city 0 to the first space station
            int maxDistance = c[0];
            
            // Calculate the max distance of the city between two space station
            for(int i = 1; i <c.length; i++){
                maxDistance = Math.max(maxDistance, (c[i] - c[i-1])/2);
            }
            
            // Calculate the distance between the last n city to the last space station
            maxDistance = Math.max(maxDistance, (n-1) - c[c.length-1]);
            
            return maxDistance;
        }
    
  • + 0 comments

    I tried the below solution which seemed to pass all the test cases. This solution sorts the input array and has a time complexity of O(nlog(n)). The algorithm used is somewhat related to the two-pointer algorithm.

    static int flatlandSpaceStations(int n, int[] c) {

        if(c.length == n)
            return 0;
    
        int maxDist = Integer.MIN_VALUE;
    
        if(c.length == 1){
            for(int i=0; i < n; i++){
                maxDist = Math.max(calculateDistance(i, c[0]), maxDist);
            }    
        }else{
            int left = 0;
            int right = 1;
            int leftDist = 0;
            int rightDist = 0;
            Arrays.sort(c);
            for(int i=0; i < n; i++){
                leftDist = calculateDistance(i, c[left]);
                rightDist = calculateDistance(i, c[right]);
    
                if(leftDist > rightDist){
                    maxDist = Math.max(rightDist, maxDist);
                    if(right < c.length -1){
                        left++;
                        right++;    
                    }
    
                }else{
                    maxDist = Math.max(leftDist, maxDist);
                }
            }
        }
    
        return maxDist;
    
    
    }
    
  • + 0 comments

    Is the custom comparator too much? Maybe. But it's funnier that way.

    int flatlandSpaceStations(int n, vector<int> c) {
        int longuest = 0;
        
        for(int i = 0; i<n; i++ ){
            
            if(find(c.begin(), c.end(), i)==c.end()){
                int closest = *min_element(c.begin(),c.end(),
                                [&](int a, int b){
                                    return abs(a - i) < abs(b - i);
                                });
                
                longuest = abs(closest-i)>longuest? abs(closest-i) : longuest;
            }
            
        }
        
        return longuest;
    }
    
  • + 0 comments

    JAVA

    static int flatlandSpaceStations(int n, int[] c) {

        ArrayList<Integer> arrayList = new ArrayList<>();
        for(int i=0;i<n;i++){
            int k=0;
            int res = Integer.MAX_VALUE;
            while(k<c.length){
                int res1 = c[k]-i;
                if(res1<0){
                    res1 = 0-res1;
                }
                if(res>res1){
                    res = res1;
                }
                k++;
            }
            arrayList.add(res);
        }
        int max = 0;
        for(int i =0;i<arrayList.size();i++){
            if(arrayList.get(i)>max){
                max = arrayList.get(i);
            }
        }
        return max;
    }
    
  • + 0 comments
    function flatlandSpaceStations($n, $m, $c) {
        sort($c);
    
        $max = $c[0];
        for($i = 0; $i < $m - 1; $i++) {
            $dist = floor(($c[$i + 1] - $c[$i]) / 2);
            if($dist > $max) {
                $max = $dist;
            }
        }
        
        $max = max($max, $n - 1 - $c[$m - 1]);
        
        return $max;
    }