✔💯 DAY 395 | EASY | 100% 0ms | [PYTHON/JAVA/C++] | EXPLAINED | Approach 💫
Please Upvote as it really motivates me \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\n# Intuition & Approach\n Describe your approach to solving the problem. \n##### \u
# Please Upvote as it really motivates me \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\n# Intuition & Approach\n<!-- Describe your approach to solving the problem. -->\n##### \u2022\tWe are given an array  arr and a matrix  mat.\n##### \u2022\tWe first create a  HashMap called  map to store the coordinates of each element in  mat. The key of the  HashMap is the value of the element, and the value is an array of two integers representing the row and column indices of the element.\n##### \u2022\tWe also create two arrays row and  col to keep track of the number of elements painted in each row and column of  mat.\n##### \u2022\tWe then iterate over the elements of arr and for each element, we get its coordinates from the  map\n##### \u2022\tWe increment the count of painted elements in the row and column corresponding to the coordinates of the current element.\n##### \u2022\tWe then check if the count of painted elements in the row or column of the current element is equal to the length of the row or column, respectively. If either of these conditions is true, it means that the entire row or column has been painted, and we return the index of the current element.\n##### \u2022\tIf we have iterated over all the elements of  arr and have not found any completely painted row or column, we return -1.\n##### \u2022\tThe intuition behind this approach is that we can keep track of the number of painted elements in each row and column of mat as we iterate over the elements of  arr. If the count of painted elements in any row or column becomes equal to the length of the row or column, it means that the entire row or column has been painted, and we can return the index of the current element. If we have iterated over all the elements of  arr and have not found any completely painted row or column, it means that there is no completely painted row or column in  mat, and we return -1.\n\n\n\n\n# Complexity\n- Time complexity: o(n2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:o(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\n    public int firstCompleteIndex(int[] arr, int[][] mat) {\n        int m= mat.length;\n        int n = mat[0].length;\n        HashMap<Integer,int[]> map = new HashMap<>();\n        for(int i=0;i<m;i++)\n            for(int j=0;j<n;j++)\n                map.put(mat[i][j],new int[]{i,j});\n        int row[] = new int[m];\n        int col[] = new int[n];\n        for(int i=0;i<arr.length;i++){\n            int x[] = map.get(arr[i]);\n            row[x[0]]++;\n            col[x[1]]++;\n            if(row[x[0]]==n || col[x[1]]==m) return i;            \n        }\n        return -1;\n    }\n```\n\n```python []\ndef firstCompleteIndex(arr: List[int], mat: List[List[int]]) -> int:\n    m = len(mat)\n    n = len(mat[0])\n    map = {}\n    for i in range(m):\n        for j in range(n):\n            map[mat[i][j]] = [i, j]\n    row = [0] * m\n    col = [0] * n\n    for i in range(len(arr)):\n        x = map[arr[i]]\n        row[x[0]] += 1\n        col[x[1]] += 1\n        if row[x[0]] == n or col[x[1]] == m:\n            return i\n    return -1\n```\n```c++ []\n#include <unordered_map>\n#include <vectorusing namespace std;\n\nint firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {\n    int m = mat.size();\n    int n = mat[0].size();\n    unordered_map<int, vector<int>> map;\n    for (int i = 0; i < m; i++) {\n        for (int j = 0; j < n; j++) {\n            map[mat[i][j]] = {i, j};\n        }\n    }\n    vector<int> row(m);\n    vector<int> col(n);\n    for (int i = 0; i < arr.size(); i++) {\n        vector<int> x = map[arr[i]];\n        row[x[0]]++;\n        col[x[1]]++;\n        if (row[x[0]] == n || col[x[1]] == m) {\n            return i;\n        }\n    }\n    return -1;\n}\n```\n\n# Please Upvote as it really motivates me \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\n\n\n![BREUSELEE.webp](https://assets.leetcode.com/users/images/062630f0-ef80-4e74-abdb-302827b99235_1680054012.5054147.webp)\n![image.png](https://assets.leetcode.com/users/images/303fa18d-281d-49f0-87ef-1a018fc9a488_1681355186.0923774.png)\n\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A
['Python', 'C++', 'Java', 'Python3']