From 4b2725b8f801cf9d99985799c34524ecb9755e88 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 21 Jul 2024 22:30:55 +0800 Subject: [PATCH] 176th Commit --- .../minimumChairs.test.ts | 9 +++++++++ .../minimumChairs.ts | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/page-29/3168. Minimum Number of Chairs in a Waiting Room/minimumChairs.test.ts create mode 100644 src/page-29/3168. Minimum Number of Chairs in a Waiting Room/minimumChairs.ts diff --git a/src/page-29/3168. Minimum Number of Chairs in a Waiting Room/minimumChairs.test.ts b/src/page-29/3168. Minimum Number of Chairs in a Waiting Room/minimumChairs.test.ts new file mode 100644 index 0000000..11a03dd --- /dev/null +++ b/src/page-29/3168. Minimum Number of Chairs in a Waiting Room/minimumChairs.test.ts @@ -0,0 +1,9 @@ +import { minimumChairs } from './minimumChairs'; + +describe('3168. Minimum Number of Chairs in a Waiting Room', () => { + test('minimumChairs', () => { + expect(minimumChairs('EEEEEEE')).toBe(7); + expect(minimumChairs('ELELEEL')).toBe(2); + expect(minimumChairs('ELEELEELLL')).toBe(3); + }); +}); diff --git a/src/page-29/3168. Minimum Number of Chairs in a Waiting Room/minimumChairs.ts b/src/page-29/3168. Minimum Number of Chairs in a Waiting Room/minimumChairs.ts new file mode 100644 index 0000000..c1f4534 --- /dev/null +++ b/src/page-29/3168. Minimum Number of Chairs in a Waiting Room/minimumChairs.ts @@ -0,0 +1,20 @@ +type MinimumChairs = (s: string) => number; + +/** + * Accepted + */ +export const minimumChairs: MinimumChairs = (s) => { + let currentPeople = 0; + let maxPeople = 0; + + for (const event of s) { + if (event === 'E') { + currentPeople += 1; + if (currentPeople > maxPeople) maxPeople = currentPeople; + } else if (event === 'L') { + currentPeople -= 1; + } + } + + return maxPeople; +};