-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
570 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ms/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Alert Using Same Key-Card Three or More Times in a One Hour Period | ||
|
||
LeetCode #: [1604](https://leetcode.com/problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/) | ||
|
||
Difficulty: Medium | ||
|
||
Topic: String, Ordered Map. | ||
|
||
## Problem | ||
|
||
Leetcode company workers use key-cards to unlock office doors. Each time a worker uses their key-card, the security system saves the worker's name and the time when it was used. The system emits an alert if any worker uses the key-card three or more times in a one-hour period. | ||
|
||
You are given a list of strings `keyName` and `keyTime` where `[keyName[i], keyTime[i]]` corresponds to a person's name and the time when their key-card was used in a single day. | ||
|
||
Access times are given in the 24-hour time format "HH:MM", such as `"23:51"` and `"09:49"`. | ||
|
||
Return a list of unique worker names who received an alert for frequent keycard use. Sort the names in ascending order alphabetically. | ||
|
||
Notice that `"10:00"` - `"11:00"` is considered to be within a one-hour period, while `"23:51"` - `"00:10"` is not considered to be within a one-hour period. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: keyName = ["daniel","daniel","daniel","luis","luis","luis","luis"], keyTime = ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"] | ||
Output: ["daniel"] | ||
Explanation: "daniel" used the keycard 3 times in a one-hour period ("10:00","10:40", "11:00"). | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: keyName = ["alice","alice","alice","bob","bob","bob","bob"], keyTime = ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"] | ||
Output: ["bob"] | ||
Explanation: "bob" used the keycard 3 times in a one-hour period ("21:00","21:20", "21:30"). | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: keyName = ["john","john","john"], keyTime = ["23:58","23:59","00:01"] | ||
Output: [] | ||
``` | ||
|
||
Example 4: | ||
|
||
```text | ||
Input: keyName = ["leslie","leslie","leslie","clare","clare","clare","clare"], keyTime = ["13:00","13:20","14:00","18:00","18:51","19:30","19:49"] | ||
Output: ["clare","leslie"] | ||
``` | ||
|
||
Constraints: | ||
|
||
- `1 <= keyName.length, keyTime.length <= 105` | ||
- `keyName.length == keyTime.length` | ||
- `keyTime` are in the format "HH:MM". | ||
- `[keyName[i], keyTime[i]]` is unique. | ||
- `1 <= keyName[i].length <= 10` | ||
- `keyName[i]` contains only lowercase English letters. |
56 changes: 56 additions & 0 deletions
56
problems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/alertNames.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const sortBy = require('lodash/sortBy') | ||
const sortedIndex = require('lodash/sortedIndex') | ||
|
||
const getDateDiffInMinutes = (date1, date2) => { | ||
const diffMs = date2 - date1 | ||
return Math.round(diffMs / 1000 / 60) | ||
} | ||
|
||
/** | ||
* @param {string[]} keyName | ||
* @param {string[]} keyTime | ||
* @return {string[]} | ||
*/ | ||
const alertNames = function (keyName, keyTime) { | ||
const namesSet = new Set() | ||
|
||
const nameTimesMap = new Map() | ||
for (let i = 0; i < keyName.length; i++) { | ||
const name = keyName[i] | ||
const time = keyTime[i] | ||
|
||
const times = nameTimesMap.get(name) || [] | ||
|
||
const idx = sortedIndex(times, time) | ||
times.splice(idx, 0, time) | ||
nameTimesMap.set(name, times) | ||
} | ||
|
||
for (const [name, times] of nameTimesMap.entries()) { | ||
const lastHour = [] | ||
for (const time of times) { | ||
const [hour, min] = time.split(':') | ||
|
||
const date = new Date() | ||
date.setUTCHours(parseInt(hour), parseInt(min), 0, 0) | ||
|
||
while ( | ||
lastHour[0] && | ||
getDateDiffInMinutes(lastHour[0], date) > 60 | ||
) { | ||
lastHour.shift() | ||
} | ||
|
||
lastHour.push(date) | ||
|
||
if (lastHour.length >= 3) { | ||
namesSet.add(name) | ||
} | ||
} | ||
} | ||
|
||
const result = new Array(...namesSet) | ||
return sortBy(result) | ||
} | ||
|
||
module.exports = alertNames |
37 changes: 37 additions & 0 deletions
37
...ems/alert-using-same-key-card-three-or-more-times-in-a-one-hour-period/alertNames.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const alertNames = require('./alertNames') | ||
|
||
test('Example 1', () => { | ||
const keyName = ['daniel', 'daniel', 'daniel', 'luis', 'luis', 'luis', 'luis'] | ||
const keyTime = ['10:00', '10:40', '11:00', '09:00', '11:00', '13:00', '15:00'] | ||
|
||
const result = alertNames(keyName, keyTime) | ||
|
||
expect(result).toStrictEqual(['daniel']) | ||
}) | ||
|
||
test('Example 2', () => { | ||
const keyName = ['alice', 'alice', 'alice', 'bob', 'bob', 'bob', 'bob'] | ||
const keyTime = ['12:01', '12:00', '18:00', '21:00', '21:20', '21:30', '23:00'] | ||
|
||
const result = alertNames(keyName, keyTime) | ||
|
||
expect(result).toStrictEqual(['bob']) | ||
}) | ||
|
||
test('Example 3', () => { | ||
const keyName = ['john', 'john', 'john'] | ||
const keyTime = ['23:58', '23:59', '00:01'] | ||
|
||
const result = alertNames(keyName, keyTime) | ||
|
||
expect(result).toStrictEqual([]) | ||
}) | ||
|
||
test('Example 4', () => { | ||
const keyName = ['leslie', 'leslie', 'leslie', 'clare', 'clare', 'clare', 'clare'] | ||
const keyTime = ['13:00', '13:20', '14:00', '18:00', '18:51', '19:30', '19:49'] | ||
|
||
const result = alertNames(keyName, keyTime) | ||
|
||
expect(result).toStrictEqual(['clare', 'leslie']) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @param {number} big | ||
* @param {number} medium | ||
* @param {number} small | ||
*/ | ||
const ParkingSystem = function (big, medium, small) { | ||
this.big = big | ||
this.medium = medium | ||
this.small = small | ||
} | ||
|
||
/** | ||
* @param {number} carType | ||
* @return {boolean} | ||
*/ | ||
ParkingSystem.prototype.addCar = function (carType) { | ||
if (carType === 1) { | ||
if (this.big === 0) { | ||
return false | ||
} | ||
|
||
this.big-- | ||
return true | ||
} | ||
|
||
if (carType === 2) { | ||
if (this.medium === 0) { | ||
return false | ||
} | ||
|
||
this.medium-- | ||
return true | ||
} | ||
|
||
if (this.small === 0) { | ||
return false | ||
} | ||
|
||
this.small-- | ||
return true | ||
} | ||
|
||
/** | ||
* Your ParkingSystem object will be instantiated and called as such: | ||
* var obj = new ParkingSystem(big, medium, small) | ||
* var param_1 = obj.addCar(carType) | ||
*/ | ||
|
||
module.exports = ParkingSystem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const ParkingSystem = require('./ParkingSystem') | ||
|
||
test('Example 1', () => { | ||
const parkingSystem = new ParkingSystem(1, 1, 0) | ||
|
||
expect(parkingSystem.addCar(1)).toBe(true) | ||
expect(parkingSystem.addCar(2)).toBe(true) | ||
expect(parkingSystem.addCar(3)).toBe(false) | ||
expect(parkingSystem.addCar(1)).toBe(false) | ||
}) | ||
|
||
test('Adding big car til no more space', () => { | ||
const parkingSystem = new ParkingSystem(2, 0, 0) | ||
|
||
expect(parkingSystem.addCar(1)).toBe(true) | ||
expect(parkingSystem.addCar(1)).toBe(true) | ||
expect(parkingSystem.addCar(1)).toBe(false) | ||
}) | ||
|
||
test('Adding medium car til no more space', () => { | ||
const parkingSystem = new ParkingSystem(0, 2, 0) | ||
|
||
expect(parkingSystem.addCar(2)).toBe(true) | ||
expect(parkingSystem.addCar(2)).toBe(true) | ||
expect(parkingSystem.addCar(2)).toBe(false) | ||
}) | ||
|
||
test('Adding small car til no more space', () => { | ||
const parkingSystem = new ParkingSystem(0, 0, 2) | ||
|
||
expect(parkingSystem.addCar(3)).toBe(true) | ||
expect(parkingSystem.addCar(3)).toBe(true) | ||
expect(parkingSystem.addCar(3)).toBe(false) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Design Parking System | ||
|
||
LeetCode #: [1603](https://leetcode.com/problems/design-parking-system/) | ||
|
||
Difficulty: Easy | ||
|
||
Topic: Design. | ||
|
||
## Problem | ||
|
||
Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size. | ||
|
||
Implement the `ParkingSystem` class: | ||
|
||
- `ParkingSystem(int big, int medium, int small)` Initializes object of the `ParkingSystem` class. The number of slots for each parking space are given as part of the constructor. | ||
- `bool addCar(int carType)` Checks whether there is a parking space of `carType` for the car that wants to get into the parking lot. `carType` can be of three kinds: big, medium, or small, which are represented by `1`, `2`, and `3` respectively. A car can only park in a parking space of its `carType`. If there is no space available, return `false`, else park the car in that size space and return `true`. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input | ||
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"] | ||
[[1, 1, 0], [1], [2], [3], [1]] | ||
Output | ||
[null, true, true, false, false] | ||
Explanation | ||
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0); | ||
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car | ||
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car | ||
parkingSystem.addCar(3); // return false because there is no available slot for a small car | ||
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied. | ||
``` | ||
|
||
Constraints: | ||
|
||
- `0 <= big, medium, small <= 1000` | ||
- `carType` is `1`, `2`, or `3` | ||
- At most 1000 calls will be made to `addCar` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Even Odd Tree | ||
|
||
LeetCode #: [1609](https://leetcode.com/problems/even-odd-tree/) | ||
|
||
Difficulty: Medium. | ||
|
||
Topics: Tree. | ||
|
||
## Problem | ||
|
||
A binary tree is named Even-Odd if it meets the following conditions: | ||
|
||
- The root of the binary tree is at level index `0`, its children are at level index `1`, their children are at level index `2`, etc. | ||
- For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right). | ||
- For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right). | ||
|
||
Given the `root` of a binary tree, return `true` if the binary tree is Even-Odd, otherwise return `false`. | ||
|
||
Example 1: | ||
|
||
```text | ||
Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2] | ||
Output: true | ||
Explanation: The node values on each level are: | ||
Level 0: [1] | ||
Level 1: [10,4] | ||
Level 2: [3,7,9] | ||
Level 3: [12,8,6,2] | ||
Since levels 0 and 2 are all odd and increasing, and levels 1 and 3 are all even and decreasing, the tree is Even-Odd. | ||
``` | ||
|
||
Example 2: | ||
|
||
```text | ||
Input: root = [5,4,2,3,3,7] | ||
Output: false | ||
Explanation: The node values on each level are: | ||
Level 0: [5] | ||
Level 1: [4,2] | ||
Level 2: [3,3,7] | ||
Node values in the level 2 must be in strictly increasing order, so the tree is not Even-Odd. | ||
``` | ||
|
||
Example 3: | ||
|
||
```text | ||
Input: root = [5,9,1,3,5,7] | ||
Output: false | ||
Explanation: Node values in the level 1 should be even integers. | ||
``` | ||
|
||
Example 4: | ||
|
||
```text | ||
Input: root = [1] | ||
Output: true | ||
``` | ||
|
||
Example 5: | ||
|
||
```text | ||
Input: root = [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17] | ||
Output: true | ||
``` | ||
|
||
Constraints: | ||
|
||
- The number of nodes in the tree is in the range `[1, 105]`. | ||
- `1 <= Node.val <= 106` | ||
|
||
## Solution Explanation | ||
|
||
We use breadth-first search to iterate through the nodes by levels. The `checkLeftRight` function describes the rules for the even-indexed level and odd-indexed level as specified in the problem description. |
Oops, something went wrong.