**Minimize Maximum Stadium Size - Google Top Interview Questions**

### Problem Statement :

You are given an integer n and a two-dimensional list of integers views. There are n teams in a tournament and you want to build two stadiums to host the games. Each element in views contains [a, b, people] meaning that when team a and team b compete, there are people number of people that will view that game. The viewership numbers among every pair of teams will be in views. You must permanently assign each team to a stadium. Every team in a stadium plays every other team in that stadium, and only within that stadium. Return the smallest possible maximum capacity of the two stadiums, where the capacity of a stadium is the number of seats required to fit the largest audience of a game in that stadium. Constraints 2 ≤ n ≤ 500 1 ≤ m ≤ 200,000 where m is the length of views Example 1 Input n = 3 views = [ [0, 1, 100], [0, 2, 900], [1, 2, 500] ] Output 100 Explanation We can put teams 0 and 1 in one stadium and team 2 in the other stadium. This means team 2 doesn't have to compete with any other team. Then, for the first stadium we need capacity of 100 to host the game between teams 0 and 1. Example 2 Input n = 2 views = [ [0, 1, 100] ] Output 0 Explanation If we put each team in separate stadiums, then they don't have to compete. Example 3 Input n = 4 views = [ [0, 1, 4], [0, 2, 1], [0, 3, 5], [1, 2, 4], [1, 3, 3], [2, 3, 4] ] Output 3 Explanation We can place teams 0 and 2 in one stadium, and 1 and 3 in the other stadium. In the first stadium, we need capacity of 1 and in the second stadium we need capacity of 3.

### Solution :

` ````
Solution in C++ :
int n, graph[501][501], which_stadium[501];
bool possible(int team, int stadium, const int& max_people) {
which_stadium[team] = stadium;
for (int i = 0; i < n; i++) {
if (which_stadium[i] != -1 && graph[team][i] > max_people &&
which_stadium[i] != (stadium ^ 1))
return false;
else if (which_stadium[i] == -1 && graph[team][i] > max_people)
if (!possible(i, stadium ^ 1, max_people)) return false;
}
return true;
}
int solve(int n, vector<vector<int>>& views) {
::n = n;
int left = 0, right = 0;
for (auto& v : views) right = max(right, graph[v[0]][v[1]] = graph[v[1]][v[0]] = v[2]);
auto can = [&](int mid) {
fill(which_stadium, which_stadium + n, -1);
for (int i = 0; i < n; i++)
if (which_stadium[i] == -1 && !possible(i, 0, mid)) return false;
return true;
};
while (left < right) {
int mid = left + (right - left) / 2;
if (can(mid))
right = mid;
else
left = mid + 1;
}
return right;
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
static ArrayList<Integer>[] graph;
public int solve(int N, int[][] views) {
sort(views);
graph = new ArrayList[N];
for (int i = 0; i < N; i++) {
graph[i] = new ArrayList<Integer>();
}
for (int i = views.length - 1; i >= 0; i--) {
// update the graph
graph[views[i][0]].add(views[i][1]);
graph[views[i][1]].add(views[i][0]);
// if there's an odd cycle, its impossible to assign stadiums.
if (!bipartite()) {
return views[i][2];
}
}
return 0;
}
public static boolean bipartite() {
int N = graph.length;
// assign 1-2 colors
int[] color = new int[N];
for (int i = 0; i < N; i++) {
if (color[i] == 0) {
color[i] = 1;
ArrayDeque<Integer> bfs = new ArrayDeque<Integer>();
bfs.add(i);
while (!bfs.isEmpty()) {
int n = bfs.pollFirst();
for (int next : graph[n]) {
if (color[next] == 0) {
color[next] = 3 - color[n];
bfs.add(next);
} else if (color[next] == color[n]) {
return false;
}
}
}
}
}
return true;
}
public static void sort(int[][] arr) {
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[2] - b[2];
// Ascending order.
}
});
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, n, views):
if n <= 2:
return 0
dist = [[-1] * n for _ in range(n)]
e = collections.defaultdict(list)
for x, y, v in views:
dist[x][y] = v
dist[y][x] = v
def two_colorable(target):
color = [-1] * n
found = False
def go(x, c, target):
for y in range(n):
v = dist[x][y]
if v > target:
if color[x] == color[y]:
nonlocal found
found = True
return
elif color[y] == -1:
color[y] = 1 - c
go(y, 1 - c, target)
for x in range(n):
if color[x] == -1:
color[x] = 1
go(x, 1, target)
if found:
return False
return True
left = 0
right = 10 ** 10
while left < right:
mid = (left + right) // 2
if two_colorable(mid):
right = mid
else:
left = mid + 1
return left
```

## View More Similar Problems

## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

View Solution →## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →## The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →