Skip to content

Commit 04718f0

Browse files
committed
feat: update solutions to lc problem: No.2125
No.2125.Number of Laser Beams in a Bank
1 parent e3d3172 commit 04718f0

File tree

9 files changed

+157
-200
lines changed

9 files changed

+157
-200
lines changed

solution/2100-2199/2125.Number of Laser Beams in a Bank/README.md

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,37 @@
6666

6767
## 解法
6868

69-
### 方法一
69+
### 方法一:逐行统计
70+
71+
我们可以逐行统计每行的安全设备数量,如果当前行没有安全设备,直接跳过,否则我们将当前行的安全设备数量乘以前一行的安全设备数量,累加到答案中。然后更新前一行的安全设备数量为当前行的安全设备数量。
72+
73+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别为行数和列数。空间复杂度 $O(1)$。
7074

7175
<!-- tabs:start -->
7276

7377
```python
7478
class Solution:
7579
def numberOfBeams(self, bank: List[str]) -> int:
76-
last = ans = 0
77-
for b in bank:
78-
if (t := b.count('1')) > 0:
79-
ans += last * t
80-
last = t
80+
ans = pre = 0
81+
for row in bank:
82+
if (cur := row.count("1")) > 0:
83+
ans += pre * cur
84+
pre = cur
8185
return ans
8286
```
8387

8488
```java
8589
class Solution {
8690
public int numberOfBeams(String[] bank) {
87-
int last = 0;
88-
int ans = 0;
89-
for (String b : bank) {
90-
int t = 0;
91-
for (char c : b.toCharArray()) {
92-
if (c == '1') {
93-
++t;
94-
}
91+
int ans = 0, pre = 0;
92+
for (String row : bank) {
93+
int cur = 0;
94+
for (int i = 0; i < row.length(); ++i) {
95+
cur += row.charAt(i) - '0';
9596
}
96-
if (t > 0) {
97-
ans += last * t;
98-
last = t;
97+
if (cur > 0) {
98+
ans += pre * cur;
99+
pre = cur;
99100
}
100101
}
101102
return ans;
@@ -107,16 +108,12 @@ class Solution {
107108
class Solution {
108109
public:
109110
int numberOfBeams(vector<string>& bank) {
110-
int ans = 0;
111-
int last = 0;
112-
for (auto& b : bank) {
113-
int t = 0;
114-
for (char& c : b)
115-
if (c == '1')
116-
++t;
117-
if (t) {
118-
ans += last * t;
119-
last = t;
111+
int ans = 0, pre = 0;
112+
for (auto& row : bank) {
113+
int cur = count(row.begin(), row.end(), '1');
114+
if (cur) {
115+
ans += pre * cur;
116+
pre = cur;
120117
}
121118
}
122119
return ans;
@@ -125,33 +122,26 @@ public:
125122
```
126123
127124
```go
128-
func numberOfBeams(bank []string) int {
129-
ans, last := 0, 0
130-
for _, b := range bank {
131-
t := strings.Count(b, "1")
132-
if t > 0 {
133-
ans += t * last
134-
last = t
125+
func numberOfBeams(bank []string) (ans int) {
126+
pre := 0
127+
for _, row := range bank {
128+
if cur := strings.Count(row, "1"); cur > 0 {
129+
ans += pre * cur
130+
pre = cur
135131
}
136132
}
137-
return ans
133+
return
138134
}
139135
```
140136

141137
```ts
142138
function numberOfBeams(bank: string[]): number {
143-
let last = 0;
144-
let ans = 0;
145-
for (const r of bank) {
146-
let t = 0;
147-
for (const v of r) {
148-
if (v === '1') {
149-
t++;
150-
}
151-
}
152-
if (t !== 0) {
153-
ans += last * t;
154-
last = t;
139+
let [ans, pre] = [0, 0];
140+
for (const row of bank) {
141+
const cur = row.split('1').length - 1;
142+
if (cur) {
143+
ans += pre * cur;
144+
pre = cur;
155145
}
156146
}
157147
return ans;
@@ -161,18 +151,16 @@ function numberOfBeams(bank: string[]): number {
161151
```rust
162152
impl Solution {
163153
pub fn number_of_beams(bank: Vec<String>) -> i32 {
164-
let mut last = 0;
165154
let mut ans = 0;
166-
for r in bank.iter() {
167-
let mut t = 0;
168-
for &v in r.as_bytes() {
169-
if v == b'1' {
170-
t += 1;
171-
}
172-
}
173-
if t != 0 {
174-
ans += last * t;
175-
last = t;
155+
let mut pre = 0;
156+
for row in bank {
157+
let cur = row
158+
.chars()
159+
.filter(|&c| c == '1')
160+
.count() as i32;
161+
if cur > 0 {
162+
ans += pre * cur;
163+
pre = cur;
176164
}
177165
}
178166
ans
@@ -182,18 +170,17 @@ impl Solution {
182170

183171
```c
184172
int numberOfBeams(char** bank, int bankSize) {
185-
int last = 0;
186-
int ans = 0;
187-
for (int i = 0; i < bankSize; i++) {
188-
int t = 0;
189-
for (int j = 0; bank[i][j]; j++) {
173+
int ans = 0, pre = 0;
174+
for (int i = 0; i < bankSize; ++i) {
175+
int cur = 0;
176+
for (int j = 0; bank[i][j] != '\0'; ++j) {
190177
if (bank[i][j] == '1') {
191-
t++;
178+
cur++;
192179
}
193180
}
194-
if (t != 0) {
195-
ans += last * t;
196-
last = t;
181+
if (cur) {
182+
ans += pre * cur;
183+
pre = cur;
197184
}
198185
}
199186
return ans;

solution/2100-2199/2125.Number of Laser Beams in a Bank/README_EN.md

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,37 @@ This is because the 2<sup>nd</sup> row contains security devices, which breaks t
5858

5959
## Solutions
6060

61-
### Solution 1
61+
### Solution 1: Row by Row Counting
62+
63+
We can count the number of safety devices row by row. If the current row does not have any safety devices, we skip it. Otherwise, we multiply the number of safety devices in the current row by the number of safety devices in the previous row, and add it to the answer. Then we update the number of safety devices in the previous row to be the number of safety devices in the current row.
64+
65+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns, respectively. The space complexity is $O(1)$.
6266

6367
<!-- tabs:start -->
6468

6569
```python
6670
class Solution:
6771
def numberOfBeams(self, bank: List[str]) -> int:
68-
last = ans = 0
69-
for b in bank:
70-
if (t := b.count('1')) > 0:
71-
ans += last * t
72-
last = t
72+
ans = pre = 0
73+
for row in bank:
74+
if (cur := row.count("1")) > 0:
75+
ans += pre * cur
76+
pre = cur
7377
return ans
7478
```
7579

7680
```java
7781
class Solution {
7882
public int numberOfBeams(String[] bank) {
79-
int last = 0;
80-
int ans = 0;
81-
for (String b : bank) {
82-
int t = 0;
83-
for (char c : b.toCharArray()) {
84-
if (c == '1') {
85-
++t;
86-
}
83+
int ans = 0, pre = 0;
84+
for (String row : bank) {
85+
int cur = 0;
86+
for (int i = 0; i < row.length(); ++i) {
87+
cur += row.charAt(i) - '0';
8788
}
88-
if (t > 0) {
89-
ans += last * t;
90-
last = t;
89+
if (cur > 0) {
90+
ans += pre * cur;
91+
pre = cur;
9192
}
9293
}
9394
return ans;
@@ -99,16 +100,12 @@ class Solution {
99100
class Solution {
100101
public:
101102
int numberOfBeams(vector<string>& bank) {
102-
int ans = 0;
103-
int last = 0;
104-
for (auto& b : bank) {
105-
int t = 0;
106-
for (char& c : b)
107-
if (c == '1')
108-
++t;
109-
if (t) {
110-
ans += last * t;
111-
last = t;
103+
int ans = 0, pre = 0;
104+
for (auto& row : bank) {
105+
int cur = count(row.begin(), row.end(), '1');
106+
if (cur) {
107+
ans += pre * cur;
108+
pre = cur;
112109
}
113110
}
114111
return ans;
@@ -117,33 +114,26 @@ public:
117114
```
118115
119116
```go
120-
func numberOfBeams(bank []string) int {
121-
ans, last := 0, 0
122-
for _, b := range bank {
123-
t := strings.Count(b, "1")
124-
if t > 0 {
125-
ans += t * last
126-
last = t
117+
func numberOfBeams(bank []string) (ans int) {
118+
pre := 0
119+
for _, row := range bank {
120+
if cur := strings.Count(row, "1"); cur > 0 {
121+
ans += pre * cur
122+
pre = cur
127123
}
128124
}
129-
return ans
125+
return
130126
}
131127
```
132128

133129
```ts
134130
function numberOfBeams(bank: string[]): number {
135-
let last = 0;
136-
let ans = 0;
137-
for (const r of bank) {
138-
let t = 0;
139-
for (const v of r) {
140-
if (v === '1') {
141-
t++;
142-
}
143-
}
144-
if (t !== 0) {
145-
ans += last * t;
146-
last = t;
131+
let [ans, pre] = [0, 0];
132+
for (const row of bank) {
133+
const cur = row.split('1').length - 1;
134+
if (cur) {
135+
ans += pre * cur;
136+
pre = cur;
147137
}
148138
}
149139
return ans;
@@ -153,18 +143,16 @@ function numberOfBeams(bank: string[]): number {
153143
```rust
154144
impl Solution {
155145
pub fn number_of_beams(bank: Vec<String>) -> i32 {
156-
let mut last = 0;
157146
let mut ans = 0;
158-
for r in bank.iter() {
159-
let mut t = 0;
160-
for &v in r.as_bytes() {
161-
if v == b'1' {
162-
t += 1;
163-
}
164-
}
165-
if t != 0 {
166-
ans += last * t;
167-
last = t;
147+
let mut pre = 0;
148+
for row in bank {
149+
let cur = row
150+
.chars()
151+
.filter(|&c| c == '1')
152+
.count() as i32;
153+
if cur > 0 {
154+
ans += pre * cur;
155+
pre = cur;
168156
}
169157
}
170158
ans
@@ -174,18 +162,17 @@ impl Solution {
174162

175163
```c
176164
int numberOfBeams(char** bank, int bankSize) {
177-
int last = 0;
178-
int ans = 0;
179-
for (int i = 0; i < bankSize; i++) {
180-
int t = 0;
181-
for (int j = 0; bank[i][j]; j++) {
165+
int ans = 0, pre = 0;
166+
for (int i = 0; i < bankSize; ++i) {
167+
int cur = 0;
168+
for (int j = 0; bank[i][j] != '\0'; ++j) {
182169
if (bank[i][j] == '1') {
183-
t++;
170+
cur++;
184171
}
185172
}
186-
if (t != 0) {
187-
ans += last * t;
188-
last = t;
173+
if (cur) {
174+
ans += pre * cur;
175+
pre = cur;
189176
}
190177
}
191178
return ans;

solution/2100-2199/2125.Number of Laser Beams in a Bank/Solution.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
int numberOfBeams(char** bank, int bankSize) {
2-
int last = 0;
3-
int ans = 0;
4-
for (int i = 0; i < bankSize; i++) {
5-
int t = 0;
6-
for (int j = 0; bank[i][j]; j++) {
2+
int ans = 0, pre = 0;
3+
for (int i = 0; i < bankSize; ++i) {
4+
int cur = 0;
5+
for (int j = 0; bank[i][j] != '\0'; ++j) {
76
if (bank[i][j] == '1') {
8-
t++;
7+
cur++;
98
}
109
}
11-
if (t != 0) {
12-
ans += last * t;
13-
last = t;
10+
if (cur) {
11+
ans += pre * cur;
12+
pre = cur;
1413
}
1514
}
1615
return ans;

0 commit comments

Comments
 (0)