# Border Crossing - Google Top Interview Questions

### Problem Statement :

```You are given two-dimensional lists of integers roads and countries as well as integers start and end.
Each element in roads contains [x, y, weight] meaning that to travel from city x to y costs weight. Each
element countries[i] represents all of the cities that belong to country i. Every city belongs to exactly
one country.

You are currently in city start and want to travel to city end. During this trip, you want to minimize
country-to-country border hops as a first priority, and then minimize total weight. Return a list of two
elements where the first is the number of country-to-country hops and the second element is the total
weight needed to make this trip. You can assume there is a solution.

Constraints

1 ≤ n ≤ 100,000 where n is the length of roads

Example 1

Input

[0, 1, 1],

[1, 2, 1],

[0, 2, 4]

]

countries = [

,

,



]

start = 0

end = 2

Output

[1, 4]

Explanation

There are two paths from start to end, [0, 2] and [0, 1, 2]. [0, 2] crosses country boarder 1 time and has
total weight of 4. Path [0, 1, 2] crosses country boarders 2 times and has total weight of 3. Thus we
return [1, 4].

Example 2

Input

[0, 1, 1],

[1, 3, 2],

[0, 2, 2],

[2, 3, 2]

]

countries = [

,

[1, 2],



]

start = 0

end = 3

Output

[2, 3]

Explanation

There are two paths from start to end, [0, 1, 3] and [0, 2, 3]. Path [0, 1, 3] crosses country boarders 2
times and has total weight of 3. Path [0, 2, 3] crosses country boarder 2 times and has total weight of 4.
Thus we return [2, 3].```

### Solution :

```                        ```Solution in C++ :

vector<int> solve(vector<vector<int>>& roads, vector<vector<int>>& countries, int start, int end) {
int n = 0;
}
n++;
vector<vector<pair<int, int>>> graph(n);
vector<int> country(n);
for (int i = 0; i < (int)countries.size(); i++) {
for (auto& who : countries[i]) {
country[who] = i;
}
}
}
list<int> q;
q.push_back(start);
vector<pair<int, int>> cost(n);
for (int i = 0; i < n; i++) {
cost[i] = {INT_MAX, INT_MAX};
}
cost[start] = {0, 0};
while (!q.empty()) {
int curr = q.front();
q.pop_front();
for (auto& [v, w] : graph[curr]) {
int change = country[v] != country[curr];
if (cost[v].first > cost[curr].first + change) {
cost[v].first = cost[curr].first + change;
cost[v].second = cost[curr].second + w;
if (change) {
q.push_back(v);
} else {
q.push_front(v);
}
} else if (cost[v].first == cost[curr].first + change &&
cost[v].second > cost[curr].second + w) {
cost[v].second = cost[curr].second + w;
if (change) {
q.push_back(v);
} else {
q.push_front(v);
}
}
}
}
return {cost[end].first, cost[end].second};
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
public int[] solve(int[][] roads, int[][] countries, int source, int end) {
HashMap<Integer, List<int[]>> g = new HashMap<>();

if (!g.containsKey(u)) {
g.put(u, new ArrayList<>());
}
}

HashMap<Integer, Integer> country = new HashMap<>();
for (int i = 0; i < countries.length; i++) {
for (int j : countries[i]) {
country.put(j, i);
}
}

PriorityQueue<int[]> pq = new PriorityQueue<>((p, q) -> {
return Long.compare(p * (long) 1e12 + p, q * (long) 1e12 + q);
});

HashSet<Integer> relaxed = new HashSet<>();

while (pq.size() > 0) {
int[] node = pq.poll();

if (node == end) {
return new int[] {node, node};
}

int u = node;

if (relaxed.contains(u))
continue;

if (g.containsKey(u)) {
for (int[] edge : g.get(u)) {
int v = edge;
pq.add(new int[] {v, node + (country.get(u) != country.get(v) ? 1 : 0),
node + edge});
}
}
}

return new int[] {0, 0};
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, roads, countries, start, end):
cntry = defaultdict(int)
for i, arr in enumerate(countries):
for c in arr:
cntry[c] = i

for x, y, w in roads:
if cntry[x] != cntry[y]:
w += 10 ** 10

dist = defaultdict(lambda: 10 ** 20)
dist[start] = 0
visited = set()

h = [(0, start)]
while h:
d, city = heappop(h)
if city in visited:
continue
if dist[c] > d + w:
dist[c] = d + w
heappush(h, (d + w, c))

return dist[end] // 10 ** 10, dist[end] % 10 ** 10```
```

## 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

## 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

## 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) =