Easiest solution beats 90% with explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nInitialization: The variables n, m, p, and x are initialized.\n\nn represents the numbe
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nInitialization: The variables n, m, p, and x are initialized.\n\nn represents the number of rows in the bank array.\nm represents the length of the strings (columns) within the bank array.\np is a counter variable initialized to zero, used to track the number of \'1\'s in the current row.\nx is an accumulator variable initialized to zero, used to sum up the count of beams.\nIteration through Rows: The code iterates through each row of the bank array using the outer loop (for (int i = 0; i < n; i++)).\n\nCounting \'1\'s: For each row, it traverses through each character using the inner loop (for (int j = 0; j < m; j++)).\n\nWithin this loop, it checks if the character at position j in the current row (bank[i].charAt(j)) is equal to \'1\'.\nIf the character is \'1\', it increments the counter variable k.\nCalculating the Number of Beams:\n\nIf the counter k (the number of \'1\'s in the current row) is greater than zero, it means there is at least one beam in the row.\nThe code then checks if it\'s the first row (i == 0). If it is, it sets the initial value of p (the previous count) to the current count k.\nFor subsequent rows (i > 0), it calculates the number of beams by multiplying the previous count p with the current count k and adds this value to the accumulator x.\nAfter this calculation, it updates the value of p to the current count k, preparing for the next row.\nReturning the Result:\n\nOnce the iteration through all rows is completed, the code returns the accumulated value x, representing the total count of beams found in the bank.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInitialize Variables:\n\nInitialize n as the number of rows in the bank (bank.length).\nInitialize m as the length of strings (columns) within the bank (bank[0].length()).\nSet p as a counter to keep track of the number of \'1\'s in a row.\nInitialize x as an accumulator to sum up the count of beams.\nIterate through Rows and Columns:\n\nUse two nested loops to traverse through each row and column of the bank.\nOuter loop (for (int i = 0; i < n; i++)) iterates through rows.\nInner loop (for (int j = 0; j < m; j++)) iterates through columns within each row.\nCount \'1\'s in Each Row:\n\nInside the inner loop, check if the character at position (i, j) in the bank array is equal to \'1\'.\nIf the character is \'1\', increment the counter variable k.\nCalculate the Number of Beams:\n\nIf the counter k (the number of \'1\'s in the current row) is greater than zero:\nIf it\'s the first row (i == 0), set the initial value of p (the previous count) to the current count k.\nFor subsequent rows (i > 0), calculate the number of beams by multiplying the previous count p with the current count k.\nAdd this calculated value to the accumulator x to keep track of the total count of beams found.\nUpdate the Previous Count (p) and Continue:\n\nUpdate the value of p to the current count k to prepare for the next row\'s calculation.\nReturn the Total Count of Beams:\n\nOnce the iteration through all rows and columns is completed, return the accumulated value x, representing the total count of beams found in the bank.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nIteration through Rows and Columns: The code utilizes two nested loops to traverse through each element (character) in the 2D bank array.\nOuter loop: Runs n times (number of rows).\nInner loop: Runs m times (length of strings/columns).\nHence, the time complexity for iterating through the entire bank array is O(n * m).\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nVariables:\nn and m are integer variables, consuming constant space (O(1)).\np and x are integer variables used for counting and accumulation, also taking constant space (O(1)).\nArrays:\nArrays A and B are used in the code for processing elements from the bank array. However, these arrays are not explicitly declared in the provided code. Assuming any temporary arrays created internally for processing also consume space proportional to the input size, but it doesn\'t significantly impact the overall complexity.\n# Code\n```\nclass Solution {\n    public int numberOfBeams(String[] bank) {\n        int n=bank.length;\n        int m=bank[0].length();\n        int p=0;\n        int x=0;\n        for(int i=0;i<n;i++){\n            int k=0;\n            for(int j=0;j<m;j++){\n                if(bank[i].charAt(j)==\'1\')\n                k++;\n            }2\n            if(k>0){\n                if(i==0)\n                p=k;\n                else{\n                    x+=p*k;\n                    p=k;\n                }\n            }\n        }\n        return x;\n        \n    }\n}\n```\n![image.png](https://assets.leetcode.com/users/images/2d1574d8-c670-4c1f-ade5-68a87970492f_1704319158.6358745.png)\n
['Array', 'Math', 'Matrix', 'Java']