feat: add Number of Ways to Select Buildings (#1140)

* add Number of Ways to Select Buildings

* Update 2222.c

add new line at the end

* Rename README.md to DIRECTORY.md

* Update leetcode/src/2222.c

Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>

* fix review notes

Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
This commit is contained in:
Alexander Pantyukhin 2022-12-19 07:34:24 +04:00 committed by GitHub
parent af0ffcbd42
commit 0169283f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -112,5 +112,6 @@
| 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.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 | | 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [C](./src/2024.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 | | 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 |
| 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [C](./src/2270.c) | Medium | | 2270 | [Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | [C](./src/2270.c) | Medium |
| 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [C](./src/2304.c) | Medium | | 2304 | [Minimum Path Cost in a Grid](https://leetcode.com/problems/minimum-path-cost-in-a-grid/) | [C](./src/2304.c) | Medium |

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

@ -0,0 +1,30 @@
long numberOfWaysForChar(char * s, char c){
long firstBuildingAppearNumber = 0;
long secondBuildingAppearNumber = 0;
long result = 0;
int sLength = strlen(s);
for (int i = 0; i < sLength; i++){
if (s[i] == c){
result += secondBuildingAppearNumber;
firstBuildingAppearNumber += 1;
continue;
}
secondBuildingAppearNumber += firstBuildingAppearNumber;
}
return result;
}
// numberOfWays returns the sum of number ways of selecting first building
// and the number of ways of selecting second building which gives us the
// number of ways of selecting three building such that no
// consecutive buildings are in the same category.
// Runtime: O(n)
// Space: O(n)
long long numberOfWays(char * s){
return numberOfWaysForChar(s, '0') + numberOfWaysForChar(s, '1');
}