From 151d59ce991dde442a195720c0ff1ced8660e11b Mon Sep 17 00:00:00 2001 From: XavierCruz5106 Date: Mon, 21 Oct 2024 22:10:16 +0000 Subject: [PATCH 1/4] feat: added Stack impl/ LeetCode 3062 finished in TypeScript --- lesson_12/structs_ts/src/lesson12.ts | 28 ++++++++++++++++++++++++- lesson_12/structs_ts/src/stack.ts | 31 ++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..41cb823c1 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,6 +6,32 @@ export class Lesson12 { * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game */ public gameResult(head: ListNode | null): string { - return ''; + let e_pts = 0; + let o_pts = 0; + + if (head === null){ + return ""; + } + + let curr: ListNode | undefined = head; + + while (curr != null) { + let next: ListNode | undefined = curr.next; + if (next === undefined){ + return ""; + } + if (curr.val > next!.val){ + e_pts++; + } else if (curr.val < next!.val){ + o_pts++; + } + curr = next.next; + } + + if (e_pts === o_pts) { + return "Tie"; + } + + return e_pts > o_pts ? "Even" : "Odd"; } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index b931ec0ef..ca73343b5 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -8,18 +8,41 @@ export class Stack { } push(value: number): void { - throw new Error('Not implemented'); + let node: ListNode | undefined = new ListNode(value); + + node.next = this.top; + this.top = node; + } pop(): number | undefined { - throw new Error('Not implemented'); + if(this.isEmpty()){ + throw new Error('Stack is empty'); + } + + if (this.top){ + let value_to_pop: number = this.top.val + this.top = this.top.next; + return value_to_pop; + } + + return undefined; } peek(): number | null { - throw new Error('Not implemented'); + if (this.isEmpty()){ + throw new Error('Stack is empty'); + } + + if (this.top){ + return this.top.val; + } + + return null; + } isEmpty(): boolean { - throw new Error('Not implemented'); + return top === undefined; } } From f637f8b10caae25c0b670854b14eda792267d478 Mon Sep 17 00:00:00 2001 From: XavierCruz5106 Date: Mon, 21 Oct 2024 22:14:27 +0000 Subject: [PATCH 2/4] chore: fixed syntax --- lesson_12/structs_ts/src/lesson12.ts | 18 +++++++++--------- lesson_12/structs_ts/src/stack.ts | 14 ++++++-------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index 41cb823c1..11e6e9884 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -9,29 +9,29 @@ export class Lesson12 { let e_pts = 0; let o_pts = 0; - if (head === null){ - return ""; + if (head === null) { + return ''; } let curr: ListNode | undefined = head; while (curr != null) { - let next: ListNode | undefined = curr.next; - if (next === undefined){ - return ""; + const next: ListNode | undefined = curr.next; + if (next === undefined) { + return ''; } - if (curr.val > next!.val){ + if (curr.val > next?.val) { e_pts++; - } else if (curr.val < next!.val){ + } else if (curr.val < next?.val) { o_pts++; } curr = next.next; } if (e_pts === o_pts) { - return "Tie"; + return 'Tie'; } - return e_pts > o_pts ? "Even" : "Odd"; + return e_pts > o_pts ? 'Even' : 'Odd'; } } diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index ca73343b5..f20d3ecf0 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -8,20 +8,19 @@ export class Stack { } push(value: number): void { - let node: ListNode | undefined = new ListNode(value); + const node: ListNode | undefined = new ListNode(value); node.next = this.top; this.top = node; - } pop(): number | undefined { - if(this.isEmpty()){ + if (this.isEmpty()) { throw new Error('Stack is empty'); } - if (this.top){ - let value_to_pop: number = this.top.val + if (this.top) { + const value_to_pop: number = this.top.val; this.top = this.top.next; return value_to_pop; } @@ -30,16 +29,15 @@ export class Stack { } peek(): number | null { - if (this.isEmpty()){ + if (this.isEmpty()) { throw new Error('Stack is empty'); } - if (this.top){ + if (this.top) { return this.top.val; } return null; - } isEmpty(): boolean { From 687e0443c6e4f5b7c2d79f9c1517a9f6168c49b8 Mon Sep 17 00:00:00 2001 From: XavierCruz5106 Date: Tue, 22 Oct 2024 13:02:55 +0000 Subject: [PATCH 3/4] resubmit: resubmission of LeetCode 3062 --- lesson_12/structs_ts/src/lesson12.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index 11e6e9884..02738d730 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -20,11 +20,13 @@ export class Lesson12 { if (next === undefined) { return ''; } + if (curr.val > next?.val) { e_pts++; } else if (curr.val < next?.val) { o_pts++; } + curr = next.next; } From 4d27f46af62144faff1340cfe95f4802c7688cee Mon Sep 17 00:00:00 2001 From: XavierCruz5106 Date: Tue, 22 Oct 2024 13:04:30 +0000 Subject: [PATCH 4/4] fix: top is not defined --- lesson_12/structs_ts/src/stack.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lesson_12/structs_ts/src/stack.ts b/lesson_12/structs_ts/src/stack.ts index f20d3ecf0..ad9be231a 100644 --- a/lesson_12/structs_ts/src/stack.ts +++ b/lesson_12/structs_ts/src/stack.ts @@ -41,6 +41,6 @@ export class Stack { } isEmpty(): boolean { - return top === undefined; + return this.top === undefined; } }