LeetCode 2125: Number of Laser Beams in a Bank Solution

Master LeetCode problem 2125 (Number of Laser Beams in a Bank), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2125. Number of Laser Beams in a Bank

Problem Explanation

Explanation:

To solve this problem, we can iterate through each pair of rows containing security devices. For each pair, we check the columns where both rows have security devices and count the number of laser beams that can be formed between them.

  1. Iterate through all pairs of rows containing security devices.
  2. For each pair of rows, iterate through the columns to count the number of laser beams that can be formed.
  3. Count the beams based on the valid conditions mentioned in the problem.

Time Complexity: O(m * n^2), where m is the number of rows and n is the number of columns.

Space Complexity: O(1) as we are not using any extra space.

Solution Code

class Solution {
    public int numberOfLaserBeams(String[] bank) {
        int count = 0;
        int m = bank.length;
        int n = bank[0].length();
        
        for (int i = 0; i < m; i++) {
            for (int j = i + 1; j < m; j++) {
                for (int k = 0; k < n; k++) {
                    if (bank[i].charAt(k) == '1' && bank[j].charAt(k) == '1') {
                        for (int r = i + 1; r < j; r++) {
                            if (bank[r].charAt(k) == '1') {
                                count--;
                                break;
                            }
                        }
                        count++;
                    }
                }
            }
        }
        
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2125 (Number of Laser Beams in a Bank)?

This page provides optimized solutions for LeetCode problem 2125 (Number of Laser Beams in a Bank) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2125 (Number of Laser Beams in a Bank)?

The time complexity for LeetCode 2125 (Number of Laser Beams in a Bank) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2125 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2125 in Java, C++, or Python.

Back to LeetCode Solutions