Google Code Jam 2020 Qualification Round: Vestigium Solution

Posted baozitraining

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Google Code Jam 2020 Qualification Round: Vestigium Solution相关的知识,希望对你有一定的参考价值。

Problem Statement

Problem

Vestigium means "trace" in Latin. In this problem we work with Latin squares and matrix traces.

The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).

An N-by-N square matrix is a Latin square if each cell contains one of N different values, and no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the N values are the integers between 1 and N.

Given a matrix that contains only integers between 1 and N, we want to compute its trace and check whether it is a natural Latin square. To give some additional information, instead of simply telling us whether the matrix is a natural Latin square or not, please compute the number of rows and the number of columns that contain repeated values.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each starts with a line containing a single integer N: the size of the matrix to explore. Then, N lines follow. The i-th of these lines contains N integers Mi,1Mi,2 ..., Mi,NMi,j is the integer in the i-th row and j-th column of the matrix.

Output

For each test case, output one line containing Case #x: k r c, where x is the test case number (starting from 1), k is the trace of the matrix, r is the number of rows of the matrix that contain repeated elements, and c is the number of columns of the matrix that contain repeated elements.

Limits

Test set 1 (Visible Verdict)

Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ N ≤ 100.
1 ≤ Mi,j ≤ N, for all i, j.

Sample


Input
 

Output
 
3
4
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
4
2 2 2 2
2 3 2 3
2 2 2 3
2 2 2 2
3
2 1 3
1 3 2
1 2 3

  
Case #1: 4 0 0
Case #2: 9 4 4
Case #3: 8 0 2

  

In Sample Case #1, the input is a natural Latin square, which means no row or column has repeated elements. All four values in the main diagonal are 1, and so the trace (their sum) is 4.

In Sample Case #2, all rows and columns have repeated elements. Notice that each row or column with repeated elements is counted only once regardless of the number of elements that are repeated or how often they are repeated within the row or column. In addition, notice that some integers in the range 1 through N may be absent from the input.

In Sample Case #3, the leftmost and rightmost columns have repeated elements.

 

Problem link
 

Video Tutorial

You can find the detailed video tutorial here

 

Thought Process

The first problem in those Competitive Programming (CP) is generally easy. Just a simple simulation to count the sum of diagonal, and keep two sets to track duplication for row and col. This could also be a good 10 mins interview warm up questions just to see if candidate knows how to write code or not.

 

Solutions

 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 import java.util.*;
 4 
 5 public class Solution {
 6 
 7     public static void main(String[] args) {
 8         Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
 9 
10         int testCases = s.nextInt();
11         int caseNum = 1;
12         Solution vestigium = new Solution();
13 
14         while (caseNum <= testCases) {
15             int n = s.nextInt();
16             int[][] a = new int[n][n];
17             for (int i = 0; i < n; i++) {
18                 for (int j = 0; j < n; j++)
19                     a[i][j] = s.nextInt();
20             }
21 
22             Result r = vestigium.cal(a);
23             System.out.println(String.format("Case #%d: %d %d %d", caseNum, r.diagnalSum, r.dupRowCount, r.dupColCount));
24 
25             caseNum++;
26         }
27     }
28 
29     private Result cal(int[][] a) {
30         if (a == null || a.length == 0 || a[0].length == 0) {
31             return null;
32         }
33 
34         int row = a.length;
35         int col = a[0].length;
36 
37         List<Set> colList = new ArrayList<>();
38         for (int i = 0; i < row; i++) {
39             colList.add(new HashSet<Integer>());
40         }
41 
42         Set<Integer> rowSet = new HashSet<>();
43         int diagnolSum = 0;
44         int dupRow = 0;
45         int dupCol = 0;
46 
47         for (int i = 0; i < row; i++) {
48             for (int j = 0; j < col; j++) {
49                 if (i == j) {
50                     diagnolSum += a[i][j];
51                 }
52                 rowSet.add(a[i][j]);
53                 colList.get(j).add(a[i][j]);
54             }
55             if (rowSet.size() != row) {
56                 dupRow++;
57             }
58             rowSet.clear();
59         }
60 
61         for (Set<Integer> s : colList) {
62             if (s.size() != row) {
63                 dupCol++;
64             }
65         }
66 
67         return new Result(diagnolSum, dupRow, dupCol);
68     }
69 
70     private class Result {
71         int diagnalSum = 0;
72         int dupRowCount = 0;
73         int dupColCount = 0;
74 
75         public Result(int ds, int drc, int dcc) {
76             this.diagnalSum = ds;
77             this.dupRowCount = drc;
78             this.dupColCount = dcc;
79         }
80     }
81 }

 

Time Complexity: O(N^2)
Space Complexity: O(N) since we used extra sets

References

以上是关于Google Code Jam 2020 Qualification Round: Vestigium Solution的主要内容,如果未能解决你的问题,请参考以下文章

Google Code Jam 2020 Qualification Round: Vestigium Solution

Google Code Jam 2020 Round1B Blindfolded Bullseye

Google Code Jam在线測试题目--Alien Language

Google Code Jam 2016 Round 1B B

Google Code Jam 2016 Round 1C C

Google Code Jam 2019 Round 1A 题解