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

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 →

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →