-
Notifications
You must be signed in to change notification settings - Fork 13
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
2 changed files
with
53 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function lemonadeChange(bills: number[]): boolean { | ||
const currentMoney = { | ||
5: 0, | ||
10: 0, | ||
20: 0, | ||
}; | ||
|
||
for (let i = 0; i < bills.length; i++) { | ||
const currentBill = bills[i]; | ||
|
||
switch (currentBill) { | ||
// first case - customer paid 5$ - no change needed | ||
case 5: | ||
currentMoney[5]++; | ||
break; | ||
|
||
// second case - customer paid 10$ - if we have a 5$ bill, give it. Otherwise - return false | ||
case 10: | ||
if (currentMoney[5] === 0) { | ||
return false; | ||
} | ||
currentMoney[5]--; | ||
currentMoney[10]++; | ||
break; | ||
|
||
// third case - customer paid 20$ - if we have 5$ and 10$ bills - give that. | ||
// else - if we have three 5$ bills - give that. | ||
// otherwise - return false. | ||
case 20: | ||
if (currentMoney[10] > 0 && currentMoney[5] > 0) { | ||
currentMoney[10]--; | ||
currentMoney[5]--; | ||
currentMoney[20]++; | ||
} else if (currentMoney[5] >= 3) { | ||
currentMoney[5] -= 3; | ||
currentMoney[20]++; | ||
} else { | ||
return false; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
} |
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,8 @@ | ||
function isRectangleOverlap(rec1: number[], rec2: number[]): boolean { | ||
let x1 = Math.max(rec1[0], rec2[0]); | ||
let y1 = Math.max(rec1[1], rec2[1]); | ||
let x2 = Math.min(rec1[2], rec2[2]); | ||
let y2 = Math.min(rec1[3], rec2[3]); | ||
|
||
return x1 < x2 && y1 < y2; | ||
} |