Skip to content

Commit

Permalink
feat(LeetCode): add solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
plskz committed Mar 26, 2023
1 parent 5edeabf commit 010f828
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
45 changes: 45 additions & 0 deletions LeetCode/lemonade-change.ts
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;
}
8 changes: 8 additions & 0 deletions LeetCode/rectangle-overlap.ts
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;
}

0 comments on commit 010f828

Please sign in to comment.