Skip to main content

2125. Number of Laser Beams in a Bank

Hi Guys, Today is 3 January 2024 and below is the solution for problem of the day

So, the question we have is Number of Laser Beams in a Bank

In this we have to find total number of lasers. So just imagine it as we have to find number of lines connecting the dots.  so if there are 2 dots. we will have one line, if we have 3 dots we will get 3 lines, and so on...

Now relate the above provided example, let's relate this to our problem statement.

 

We have 4 walls, first wall has 3 lasers, second wall has 0 lasers, third wall got 2 lasers and last wall has 1 laser. So as i suggested we need to connect dots so make sure to only join adjacent dots. So lasers from 1 and 3 wall  will connect to each other (6 lasers) and wall 3 and 4 will connect to each other (2 lasers). So total of 8 lasers, which is the required output anyways.

class Solution:
def numberOfBeams(self, bank: List[str]) -> int:
val = 0 # Initialize a variable `val` to store the total number of beams
temp = 0 # Initialize a temporary variable `temp` to store the count of lasers 
in the previous row

# Iterate through each row (represented by `lasers`) in the `bank`
for lasers in bank:
count = 0 # Initialize a variable `count` to store the count of lasers in
 the current row

# Iterate through each character (laser) in the current row
for laser in lasers:
if laser == "1":
count += 1 # Increment `count` if the character is "1"

# Check if there are any lasers in the current row
if count:
val += (temp * count) # Add the product of `temp` and `count` to the
 total `val`
temp = count # Update `temp` with the count of lasers in the current 
row for the next iteration

return val # Return the total number of beams


 

Code Explanation:

Explanation:

  1. The code defines a class named Solution.

  2. Inside the class, there's a method named numberOfBeams that takes a list of strings (bank) as its input and returns an integer.

  3. The method initializes two variables, val and temp, to keep track of the total number of beams and the count of lasers in the previous row, respectively.

  4. It then iterates through each row (lasers) in the bank.

  5. For each row, it counts the number of lasers (count) by iterating through each character in the row. If a character is "1", the count is incremented.

  6. If there are any lasers in the current row (i.e., count is non-zero), it adds the product of temp and count to the total val. This is done to calculate the number of beams formed by connecting lasers in consecutive rows.

  7. The temp variable is updated with the count of lasers in the current row for the next iteration.

  8. Finally, the method returns the total number of beams (val).

In summary, the code calculates the total number of beams formed by connecting lasers in consecutive rows, where each row is represented by a string of "0" and "1" characters. The number of beams is determined by multiplying the count of lasers in the current row with the count of lasers in the previous row.

 

Performance - 

1. Time Complexity - O(N*M), N=number of row (length of bank array) and M = number of cells in each row (length of each value in bank array)

2. Space complexity - O(1)

 


Hi, thanks for reading my post, I hope you enjoyed it. So, if you want me to keep you updated with the tech references in coming future please do share your valuable comments with me and also do not forget to share it with your friends....:)

 

 

Comments

Popular posts from this blog

WhatsApp hidden Fonts

WhatsApp is a messaging app used by over 1 million world wide. It have been the default messaging app for the people since it was first made, Social messaging app supports Android, IOS and windows. It surely have become crucial part of our lives but some of us may not know about some of its hidden features. Whatsapp gives its user full liberty to block useless contacts, choose wisely to let other people know whether you read their messages or not (read receipts), setting privacy, secret fonts, and recent updates gave us the option to post stories too, no doubt why it is number 1 messaging app. Some of us may know the feature of hidden text and some of us may not, they are very simple. If you ever wanted to highlight the particular text or say make it bold so you can do that on whatsapp Here are some cool tricks that you can do while typing messages to your friends. Italicize the text by placing asterisks before and after the text. _techgen_ make the text appear bold by placing tidle sy...

What is a Processor?

Smartphones have been the most important part of our lives. We have seen the evolution of our  handsets. First we saw landlines which were mess of tangled wires then we saw mobile phones which is successor of landlines. They were portable devices from which you can make calls then we witnessed emergence of Smartphones which were  considered as portable computers which also allows us to make calls. What is a Processor? Underneath that touchscreen display there is a full- fledged computer that commands your apps so that they function properly. A processor executes what you want your smartphone to do or in other word you can say processor is brain of computer. there is Exynos, Snapdragon, quad-core octa-core etc. What is a core? It is an element of surprises. They are present inside processor and are responsible for reading the command and executing them. First devices came with single core processor but later on scientists invented more advanced processors such as dual core , So...

Samsung launches CFG70 curved gaming monitors in India.

Samsung electronics launched its two gaming monitors in India on Thursday, LC24FG70 (24 inch) and LC27FG70 (27 inch)-priced at ₹ 35000 and ₹ 42000 respectively. The company claims that they are first curved gaming computer with a 1 millisecond moving picture response time (MPRT). These two monitors are part of CFG70 series of curved gaming computers which were launched in IFA 2016. Apart from ther fast response time with their VA panels, the both come with Quantum dot technologyand a 144 Hz refresh rate that is optimised for AMD's FreeSynch technology. Both the monitors support a full HD resolution, and company is touting a contrast ratio of 3000:1.Samsung is also boasting of a UX that's specially designed for gamers, with an "intuitive settings dashboard" as well as hotkeys on the front and back of the display to quickly adjust settings between presets. The company also says each monitor undergoes rigorous pre-shipment factory calibration for compatibility with vario...