Sort by

recency

|

948 Discussions

|

  • + 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;
    }
    
  • + 0 comments
    def flatlandSpaceStations(n, m, c):
        if n == m:
            return 0
        c = sorted(c)
        start_distance = c[0]
        end_distance = (n-1) - c[-1]
        mid_distance = None
        for i in range(len(c)-1):
            gap = c[i+1] - c[i]
            distance = gap // 2
            if mid_distance is None or distance > mid_distance:
                mid_distance = distance
                
        return max(start_distance, mid_distance or 0, end_distance)
    
  • + 0 comments

    js my solution... if (n == c.length) return 0;

    const lengthC = c.length - 1;

    c.sort((a, b) => a - b); console.log(c);

    let max = c[0]; for (let i = 0; i < lengthC; i++) { const tmp = Math.floor(c[i + 1] - (c[i + 1] + c[i]) / 2);

    if (tmp > max) max = tmp; }

    let lastOne = n - c[lengthC] - 1;

    if (lastOne > max) max = lastOne;

    console.log(max);

  • + 0 comments

    Python: My short solution…

    def flatlandSpaceStations(n, c):
        return max(
            (s := sorted(c))[0],  # dist.: 0 to 1st spaceport
            n - s[-1] - 1,  # dist.: last spaceport to n
            *map(lambda i: (s[i + 1] - s[i]) // 2,   # dist.: halfway between-
                range(len(s) - 1)))  # -each spaceport
    

    I updated my code to make it more readable. Functional solutions aren't always difficult to read.