GetSpecialItems.cs

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
					
public class Program
{
	public class Point {
        public int Row;
        public int Col;
        public Point(int row, int col) { Row = row; Col = col; }
    }
	
	public static int GetSpecialItems(int[][] matrix) {
        int rows = matrix.Length;
        int cols = matrix[0].Length;
        
        var rowDict = new Dictionary<int, int>();
        var colDict = new Dictionary<int, int>();
        var points = new List<Point>();
        
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                if (matrix[i][j] == 1)
                {
                    if (!rowDict.ContainsKey(i))
                        rowDict[i] = 0;
                    rowDict[i]++;
                    
                    if (!colDict.ContainsKey(j))
                        colDict[j] = 0;
                    colDict[j]++;
                    
                    points.Add(new Point(i, j));
                }
            }
        }
        
        int res = 0;
        foreach (Point p in points)
        {
            if (rowDict[p.Row] == 1 && colDict[p.Col] == 1)
                res++;
        }
        
        return res;
    }
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		
		// tests
		var matrix = new int[][] {
			new int[] { 1, 0, 0 },
			new int[] { 0, 0, 1 },
			new int[] { 1, 0, 0 }
		};
		Console.WriteLine(GetSpecialItems(matrix).ToString()); // 1
	}
}
;