Organizing Containers of Balls

  • + 0 comments

    c# code, O(nlogn)

        public static string organizingContainers(List<List<int>> container)
        {
            int n = container.Count;
            List<long> containerCapacity = new List<long>();
            List<long> ballByTypeCount = new List<long>();
            for (int i = 0; i < n; i++) {
                containerCapacity.Add(0);
                ballByTypeCount.Add(0);
            }
            for (int i = 0; i < n; i++){
                for (int j = 0; j < n; j++){
                    containerCapacity[i] += container[i][j];
                    ballByTypeCount[j] += container[i][j];                
                }
            }
            containerCapacity.Sort();
            ballByTypeCount.Sort();
            for (int i = 0; i < n; i++){
                if (containerCapacity[i] != ballByTypeCount[i])
                    return "Impossible";
            }
            return "Possible";
        }