Minimum Light Radius - Google Top Interview Questions


Problem Statement :


You are given a list of integers nums representing coordinates of houses on a 1-dimensional line. 

You have 3 street lights that you can put anywhere on the coordinate line and a light at coordinate x lights up houses in [x - r, x + r], inclusive. 

Return the smallest r required such that we can place the 3 lights and all the houses are lit up.

Constraints

n ≤ 100,000 where n is the length of nums

Example 1

Input

nums = [3, 4, 5, 6]

Output

0.5

Explanation

If we place the lamps on 3.5, 4.5 and 5.5 then with r = 0.5 we can light up all 4 houses.



Solution :



title-img




                        Solution in C++ :

int need(vector<int>& v, double r) {
    int ret = 0;
    for (int i = 0; i < v.size();) {
        ret++;
        double upto = v[i] + 2 * r;
        int j = i + 1;
        while (j < v.size() && v[j] <= upto) j++;
        i = j;
    }
    return ret;
}

double solve(vector<int>& nums) {
    if (nums.size() == 0) return 0;
    sort(nums.begin(), nums.end());
    double lhs = 0;
    double rhs = nums.back() - nums[0];
    for (int qq = 0; qq < 40; qq++) {
        double mid = (lhs + rhs) / 2;
        if (need(nums, mid) <= 3)
            rhs = mid;
        else
            lhs = mid;
    }
    return rhs;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    int[] nums;
    public double solve(int[] nums) {
        if (nums.length <= 3)
            return 0;
        this.nums = nums;
        Arrays.sort(nums);
        int l = 0, r = nums[nums.length - 1];
        while (l < r) {
            int m = l + (r - l) / 2;
            if (good(m)) {
                r = m;
            } else {
                l = m + 1;
            }
        }
        return (l + 0.0) / 2;
    }
    public boolean good(int range) {
        int end = range + nums[0], ct = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > end) {
                ct++;
                end = nums[i] + range;
            }
        }
        return ct <= 2;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, nums):
        nums.sort()
        N = len(nums)
        if N <= 3:
            return 0
        LIGHTS = 3

        def can(diameter):
            start = nums[0]
            end = start + diameter
            for i in range(LIGHTS):
                idx = bisect_right(nums, end)
                if idx >= N:
                    return True
                start = nums[idx]
                end = start + diameter
            return False

        lo = -1
        hi = nums[-1] - nums[0]
        # Invariant: the answer lies in the interval (lo, hi]
        while lo < hi - 1:
            mid = (lo + hi) // 2
            if can(mid):
                hi = mid
            else:
                lo = mid
        return hi / 2
                    


View More Similar Problems

Sparse Arrays

There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results. Example: strings=['ab', 'ab', 'abc'] queries=['ab', 'abc', 'bc'] There are instances of 'ab', 1 of 'abc' and 0 of 'bc'. For each query, add an element to the return array, results=[2,1,0]. Fun

View Solution →

Array Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

View Solution →

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →