From 8cbb76a02b1e2123db2a8b376e4e92179a4c0e0f Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Wed, 18 Jan 2023 23:13:06 +0400 Subject: [PATCH] feat: add Number of Laser Beams in a Bank (#1174) Co-authored-by: David Leal --- leetcode/DIRECTORY.md | 1 + leetcode/src/2125.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 leetcode/src/2125.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index ecc19950..00e19f09 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -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 | diff --git a/leetcode/src/2125.c b/leetcode/src/2125.c new file mode 100644 index 00000000..0f65b24d --- /dev/null +++ b/leetcode/src/2125.c @@ -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; +}