Algorithm for Calculating Beams in a String Array || Easy✅ || Beginner Friendly🔥🔥
Intuition\nMy first thought is to iterate over each string in the bank array and calculate the row count for each string using the calculateRowCount function. T
# Intuition\nMy first thought is to iterate over each string in the `bank` array and calculate the row count for each string using the `calculateRowCount` function. Then, I can compare the row count of each string with the previous row count and count the number of strings that have a different row count. Finally, I can return the count.\n\n# Approach\nThe function called `numberOfBeams` that takes an array of strings called `bank` as input and returns a number.\n\nThe function initializes two variables: `prevRowCount` with a value of 0 and `total` with a value of 0.\n\nThere is also a nested function called `calculateRowCount` that takes a string `s` as input and returns a number. This function splits the string into an array of characters using the `split(\'\')` method, then uses the `reduce` method to iterate over the array and calculate the sum of the numbers in the array. It does this by converting each character to a number using `parseInt(c)` and adding it to the `count` accumulator. The initial value of `count` is 0.\n\nThe main function then enters a for loop that iterates over each element `row` in the `bank` array.\n\nInside the loop, it calls the `calculateRowCount` function passing `row` as an argument and assigns the result to the variable `currentRowCount`.\n\nIt then checks if `currentRowCount` is not equal to 0. If it is not 0, it multiplies `currentRowCount` by `prevRowCount` and adds the result to the `total` variable. It then updates the value of `prevRowCount` to be equal to `currentRowCount`.\n\nAfter the loop, the function returns the value of `total`, which represents the sum of the products of each non-zero `currentRowCount` and its corresponding `prevRowCount`.\n\n# Complexity\nThe time complexity of this function depends on the length of the `bank` array and the length of each string in the array. \n\nThe `calculateRowCount` function has a time complexity of O(n), where n is the length of the string `s`. This is because it uses the `split(\'\')` method to split the string into an array of characters, and then uses the `reduce` method to iterate over the array.\n\nThe main function has a for loop that iterates over each element in the `bank` array. Therefore, the time complexity of the main function is O(m), where m is the length of the `bank` array.\n\nOverall, the time complexity of the function is O(m * n), where m is the length of the `bank` array and n is the length of the longest string in the array.\n\nThe space complexity of this function is O(1) because it only uses a constant amount of additional space to store the variables `prevRowCount`, `total`, `currentRowCount`, and `count`. The space used by the input `bank` array is not counted in the space complexity analysis.\n\n# Code\n```\n/**\n * @param {string[]} bank\n * @return {number}\n */\nvar numberOfBeams = function(bank) {\n    let prevRowCount = 0;\n    let total = 0;\n\n    const calculateRowCount = (s) => {\n        return s.split(\'\').reduce((count, c) => count + parseInt(c), 0);\n    };\n\n    for (const row of bank) {\n        const currentRowCount = calculateRowCount(row);\n        if (currentRowCount !== 0) {\n            total += currentRowCount * prevRowCount;\n            prevRowCount = currentRowCount;\n        }\n    }\n\n    return total;\n};\n\n```
['JavaScript']