Intersecting Lines - Facebook Top Interview Questions


Problem Statement :


You are given a two-dimensional list of integers lines and integers lo and hi. 

Each element in lines contains [m, b] which represents the line y = mx + b. 

Return the number of lines that intersect with at least one other line between x = lo and x = hi, inclusive.

Constraints

0 ≤ n ≤ 100,000 where n is the length of lines

Example 1


Input

lines = [

    [2, 3],

    [-3, 5],

    [4, 6]

]

lo = 0

hi = 1

Output

2

Explanation

Lines [2, 3] and [-3, 5] intersect at x = 0.4, which is between x = 0 and x = 1.

Example 2

Input


lines = [

    [-1, 0],

    [-1, 1],

    [-1, 2],

    [-1, 3]

]

lo = 0

hi = 1

Output

0

Explanation

None of the lines intersect anywhere.



Solution :



title-img




                        Solution in C++ :

void eval(vector<pair<long long, long long>>& ret, vector<pair<long long, long long>>& lines,
          int x) {
    ret.clear();
    for (int i = 0; i < lines.size(); i++) {
        ret.emplace_back(lines[i].first * x + lines[i].second, i);
    }
    sort(ret.begin(), ret.end());
}

int solve(vector<vector<int>>& olines, int lo, int hi) {
    vector<pair<long long, long long>> lines;
    for (auto& out : olines) lines.emplace_back(out[0], out[1]);
    vector<pair<long long, long long>> lhs, rhs;
    eval(lhs, lines, lo);
    eval(rhs, lines, hi);
    int ret = 0;
    map<int, int> dp;
    for (int i = 0; i < lhs.size();) {
        int j = i + 1;
        while (j < lhs.size() && lhs[i].first == lhs[j].first) j++;
        for (int k = i; k < j; k++) {
            dp[lhs[k].second] = j - 1;
        }
        i = j;
    }
    multiset<int> seen;
    int reallhs = 0;
    for (int i = 0; i < rhs.size();) {
        int j = i + 1;
        while (j < rhs.size() && rhs[j].first == rhs[i].first) j++;
        for (int k = i; k < j; k++) {
            seen.insert(dp[rhs[k].second]);
        }
        if (reallhs + seen.size() - 1 == *seen.rbegin()) {
            if (seen.size() > 1) ret += seen.size();
            reallhs += seen.size();
            seen.clear();
        }
        i = j;
    }
    assert(seen.size() == 0);
    return ret;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(int[][] lines, int lo, int hi) {
        long[][] p = new long[lines.length][2];
        for (int i = 0; i < lines.length; i++) {
            p[i][0] = lines[i][0] * lo + lines[i][1];
            p[i][1] = lines[i][0] * hi + lines[i][1];
        }
        Arrays.sort(p, new Comparator<long[]>() {
            public int compare(long[] a, long[] b) {
                if (a[0] == b[0])
                    return Long.compare(b[1], a[1]);
                return Long.compare(a[0], b[0]);
            }
        });
        long max = p[0][1], min = p[p.length - 1][1];
        HashSet<Integer> set = new HashSet();
        for (int i = 1; i < p.length; i++) {
            if (p[i][1] <= max)
                set.add(i);
            max = Math.max(max, p[i][1]);
        }
        for (int i = p.length - 2; i > -1; i--) {
            if (p[i][1] >= min)
                set.add(i);
            min = Math.min(min, p[i][1]);
        }
        return set.size();
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, lines, lo, hi):
        segments = [(m * lo + b, m * hi + b, i) for i, (m, b) in enumerate(lines)]
        segments.sort()
        res = [0 for _ in lines]

        cstarting = Counter([a for a, b, i in segments])
        for (x, y, i) in segments:
            if cstarting[x] > 1:
                res[i] = 1

        curmax = -(10 ** 10)
        prevx = -(10 ** 10)
        for (x, y, i) in segments:
            if x == prevx:
                res[i] = 1
            if y <= curmax:
                res[i] = 1
            curmax = max(curmax, y)
            prevx = x

        curmin = 10 ** 10
        prevx = 10 ** 10

        for (x, y, i) in segments[::-1]:
            if x == prevx:
                res[i] = 1
            if y >= curmin:
                res[i] = 1
            curmin = min(curmin, y)
            prevx = x

        return sum(res)
                    


View More Similar Problems

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →

Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →