Solved LeetCode 2125: Number of Laser Beams in a Bank with Python

🔥 Day 61 of #100DaysOfCode 💡 LeetCode Problem 2125 – Number of Laser Beams in a Bank (Medium) Today’s challenge focused on analyzing a 2D binary matrix representing a bank floor plan equipped with anti-theft laser security devices. Each ‘1’ indicates a device, and each ‘0’ indicates an empty cell. The goal was to determine how many laser beams exist between devices on different rows, ensuring no other row between them contains any active devices. 🧠 Key Idea: Count the number of ‘1’s (active devices) in each row. For every non-empty row, multiply its device count by the previous non-empty row’s count — this gives the number of beams formed between them. ⚙️ Approach: Traverse each row. Count devices ('1') in that row. If the row is not empty, add prev * curr to the result. Update prev as the current count for the next iteration. 💻 Code (Python): class Solution: def numberOfBeams(self, bank: List[str]) -> int: prev, result = 0, 0 for row in bank: curr = row.count('1') if curr: result += prev * curr prev = curr return result 📈 Result: ✅ Runtime: 5 ms — Beats 80.12% ✅ Memory: 19.34 MB — Beats 93.57% 🧩 Concepts Applied: String traversal Matrix interpretation Counting and dynamic accumulation Each day’s challenge continues strengthening my logic-building and Python efficiency. 🚀 #LeetCode #100DaysOfCode #Python #ProblemSolving #CodingJourney #Consistency

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories