feat: add Number of Laser Beams in a Bank (#1174)

Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Alexander Pantyukhin 2023-01-18 23:13:06 +04:00 committed by GitHub
parent 3014b7352d
commit 8cbb76a02b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -115,6 +115,7 @@
| 1704 | [Determine if String Halves Are Alike](Determine if String Halves Are Alike) | [C](./src/1704.c) | Easy |
| 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.c) | Easy |
| 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [C](./src/2024.c) | Medium |
| 2125 | [Number of Laser Beams in a Bank](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/description/) | [C](./src/2125.c) | Medium |
| 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium |
| 2222 | [Number of Ways to Select Buildings](https://leetcode.com/problems/number-of-ways-to-select-buildings/) | [C](./src/2222.c) | Medium |
| 2256 | [Minimum Average Difference](https://leetcode.com/problems/minimum-average-difference/) | [C](./src/2256.c) | Medium |

30
leetcode/src/2125.c Normal file
View File

@ -0,0 +1,30 @@
int coundDevices(char* bankRow){
int result = 0;
int bankRowSize = strlen(bankRow);
for(int i = 0; i < bankRowSize; i++){
if (bankRow[i] == '1'){
result++;
}
}
return result;
}
// Counting devices in each row
// Runtime: O(n*m), n-number of bank rows, m - max size of row.
// Space: O(1)
int numberOfBeams(char ** bank, int bankSize){
int prevRowDevices = 0;
int result = 0;
for(int i = 0; i < bankSize; i++){
int devices = coundDevices(bank[i]);
if (devices == 0){
continue;
}
result += devices * prevRowDevices;
prevRowDevices = devices;
}
return result;
}