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 :



title-img




                        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

Self Balancing Tree

An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ

View Solution →

Array and simple queries

Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty

View Solution →

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →