7 lines Python / NumPy
Perfect opportunity to practice NumPy. Gets accepted in ~2200 ms, seems relatively fast. Actual time is about 900 ms, judge overhead is ~1300 here. See benchmar
Perfect opportunity to practice NumPy. Gets accepted in ~2200 ms, seems relatively fast. Actual time is about 900 ms, judge overhead is ~1300 here. See benchmark in the comments.\n```\nimport numpy as np\n\nclass Solution:\n    def maxTrailingZeros(self, grid: List[List[int]]) -> int:\n        A = np.array(grid)\n        def cumdivs(d):\n            D = sum(A % d**i == 0 for i in range(1, 10))\n            return D.cumsum(0) + D.cumsum(1) - D\n        return max(np.minimum(cumdivs(2), cumdivs(5)).max()\n                   for _ in range(4)\n                   if [A := np.rot90(A)])\n```\nMy helper function `cumdivs` first computes divisibility by divisor 2 or 5, as that\'s the factors we need to get zeros: It counts for each value in the grid how often it is divisible by divisor `d`. I try up to 9 times, since 2<sup>9</sup>=512 is the largest possible (since the numbers are guaranteed to not exceed 1000). Then it computes and combines the cumulative sums of the divisibilities on both axes (and subtracts each value\'s divisibility in order to not double-count it).\n\nThat only covers one pair of directions. So I do it four times, rotating the matrix by 90 degrees in between.
['Python']