Logically Consistent Book - Google Top Interview Questions
Problem Statement :
You are given a two-dimensional list of integers lists. Each element contains [start, end, type], meaning that from pages [start, end] inclusive, there's either an even number of words (type = 0) or an odd number of words (type = 1). Return whether lists is logically consistent. Constraints n ≤ 100,000 where n is the length of lists Example 1 Input lists = [ [1, 5, 1], [6, 10, 0], [1, 10, 0] ] Output False Explanation If there are an odd number of words from pages [1, 5] and an even number of words from pages [6, 10], that must mean there's an odd number from pages [1, 10]. So this is a contradiction. Example 2 Input lists = [ [0, 2, 0], [2, 4, 0], [0, 4, 0] ] Output True Explanation If there are an even number of words from pages [0, 2] and an even number of words from pages [2, 4], that means there can be an even number from pages [0, 4]. So there's no contradiction. Example 3 Input lists = [ [0, 2, 1], [3, 4, 1], [0, 4, 0] ] Output True Explanation If there are an odd number of words from pages [0, 2] and an odd number of words from pages [3, 4], that must mean there's an even number from pages [0, 4]. So there's no contradiction.
Solution :
Solution in C++ :
unordered_map<int, vector<pair<int, int>>> edges;
bool solve(vector<vector<int>>& lists) {
unordered_map<int, int> parity;
edges.clear();
for (auto& edge : lists) {
edges[edge[0] - 1].emplace_back(edge[1], edge[2]);
edges[edge[1]].emplace_back(edge[0] - 1, edge[2]);
}
for (auto edge : edges) {
int src = edge.first;
if (parity.count(src)) continue;
parity[src] = 0;
queue<int> q;
q.push(src);
while (q.size()) {
int curr = q.front();
q.pop();
for (auto edge : edges[curr]) {
int nparity = parity[curr] ^ edge.second;
if (parity.count(edge.first) && parity[edge.first] != nparity) return false;
if (!parity.count(edge.first)) {
parity[edge.first] = nparity;
q.push(edge.first);
}
}
}
}
return true;
}
Solution in Java :
import java.util.*;
class Solution {
public boolean solve(int[][] nums) {
Comparator c = new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
if (a[0] != b[0])
return a[0] - b[0];
else
return a[1] - b[1];
}
};
PriorityQueue<int[]> pq = new PriorityQueue<int[]>(c);
for (int[] range : nums) pq.add(range);
while (pq.size() >= 2) {
int[] r1 = pq.poll();
int[] r2 = pq.peek();
if (r1[0] == r2[0]) {
if (r1[1] == r2[1]) {
if (r1[2] != r2[2])
return false;
} else {
pq.add(new int[] {r1[1] + 1, r2[1], Math.abs(r1[2] - r2[2])});
}
}
}
return true;
}
}
Solution in Python :
class Solution:
def solve(self, lists):
graph = defaultdict(list)
for i, j, p in lists:
graph[i].append([j + 1, p])
graph[j + 1].append([i, p])
state = {}
def dfs(node, color):
state[node] = color
for nei, w in graph[node]:
if nei not in state:
if not dfs(nei, color ^ w):
return False
elif state[nei] != color ^ w:
return False
return True
for node in graph:
if node not in state and dfs(node, 0) is False:
return False
return True
View More Similar Problems
Contacts
We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co
View Solution →No Prefix Set
There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio
View Solution →Cube Summation
You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor
View Solution →Direct Connections
Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do
View Solution →Subsequence Weighting
A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =
View Solution →Kindergarten Adventures
Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti
View Solution →