Skip to content

Commit b54e2b8

Browse files
authored
Update README.md
1 parent 83ba510 commit b54e2b8

File tree

1 file changed

+21
-45
lines changed

1 file changed

+21
-45
lines changed

README.md

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,45 @@ This is my attemp to make the coding experience easier for you guys so that you
55

66
## Always here to assist you guys.
77

8-
## Today's 02-01-24 [Problem Link](https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/solutions/4490329/daily-02-01-24/)
8+
## Today's 03-01-24 [Problem Link](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/solutions/?envType=daily-question&envId=2024-01-03)
99

1010
# Intuition
1111
<!-- Describe your first thoughts on how to solve this problem. -->
12-
Unitary way to think.
13-
12+
Basic multiplication.
1413
# Approach
1514
<!-- Describe your approach to solving the problem. -->
16-
- I counted the frequencies of every number in the given array
17-
- - used HashMap for that
18-
- The maximum frequency of all element will be the number of answer rows
19-
- - as in every row there should be unique elements
20-
- Created and initialised answerlist with empty sub-lists
21-
- Now, according to their frequencies adding elements to their rows
22-
- - elements with frequency '1' will be present in only first row
23-
- - elements with frequency '2' will be present in first and second row
24-
- - ... and so on
15+
- I kept track of number of '1' in a row
16+
- Now iterated over every row of array
17+
- - counted the number of '1' in current row
18+
- - number of beams will the product of current number of device and previous number of devices
19+
- - added the product to answer
20+
- - now, the current one will become the previous one to next row
2521
---
2622
Have a look at the code , still have any confusion then please let me know in the comments
2723
Keep Solving.:)
2824

29-
3025
# Complexity
31-
- Time complexity : $$O(cm)$$
26+
- Time complexity : $$O(l)$$
3227
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
33-
34-
- Space complexity : $$O(c)$$
28+
$$l$$ : length of array
29+
- Space complexity : $$O(1)$$
3530
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
36-
$$c$$ : number of unique elements in array
37-
$$m$$ : maximum frequency of any element
3831

3932
# Code
4033
```
4134
class Solution {
42-
public List<List<Integer>> findMatrix(int[] nums) {
43-
HashMap<Integer, Integer> m = new HashMap<>();
44-
int mf = 0; // to store maximum frequency of any integer : it will determine the number of rows
45-
46-
// adding numbers in hashmap along with its frequencies
47-
for( int i = 0; i < nums.length; i++){
48-
m.putIfAbsent(nums[i], 0);
49-
m.put( nums[i], m.get(nums[i]) + 1 );
50-
mf = Math.max(mf, m.get(nums[i]));
51-
}
52-
List<List<Integer>> l = new ArrayList<>(); // to store the answer list
53-
// the element with maximum frequency will be present in every sub-list of answerlist
54-
// making 'mf' rows in answer list
55-
for( int i = 0; i < mf; i++){
56-
List<Integer> t = new ArrayList<>();
57-
l.add(t);
58-
}
59-
60-
// according to their frequencies adding elements to their rows
61-
// elements with frequency '1' will be present in only first row
62-
// elements with frequency '2' will be present in first and second row
63-
// ... and so on
64-
65-
for( int c : m.keySet() ){
66-
for( int f = 0; f < m.get(c); f++){
67-
l.get(f).add(c);
35+
public int numberOfBeams(String[] bank) {
36+
int jawab = 0; // to store answer
37+
int picheek = 0; // to store number of '1' in previous state
38+
39+
for( String r : bank){
40+
int ek = (int) r.chars().filter( g -> g == '1').count(); // counting the number of '1' in current row
41+
if( ek != 0){ // number of beams will the product of current number of device and previous number of devices
42+
jawab += picheek*ek; // adding the product to answer
43+
picheek = ek; // now, the current one will become the previous one to next row
6844
}
6945
}
70-
return l;
46+
return jawab;
7147
}
7248
}
7349
```

0 commit comments

Comments
 (0)