From cc90062be334aaf6857eeae161258ffbb1db0b88 Mon Sep 17 00:00:00 2001 From: marleomac3 Date: Wed, 23 Oct 2024 15:33:06 +0000 Subject: [PATCH 1/3] feat/fix: Fixed divergent branch issue. Changed the order of var declarations inpush method. logic was reversed. Also added exception handling. --- .../codedifferently/lesson12/Lesson12.java | 34 ++++++++++++++++++- .../com/codedifferently/lesson12/Stack.java | 17 ++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java index af7663e90..dfae1e06b 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Lesson12.java @@ -1,12 +1,44 @@ package com.codedifferently.lesson12; public class Lesson12 { + // node1 node2 node3 node4 null /** * Provide the solution to LeetCode 3062 here: * https://github.com/yang-su2000/Leetcode-algorithm-practice/tree/master/3062-winner-of-the-linked-list-game */ public String gameResult(ListNode head) { - return null; + // These vars keep track of the point for the even and odd indicies. + int oddPoints = 0; + int evenPoints = 0; + + /* + * This var will be redeclared throughout the loop and be used + * to point to the node pairs. + */ + ListNode curr = head; + + /** + * This loop will do two comparisons. The 1st comparison is to determine which index has a + * greater value. The 2nd comparison checks which index team is more points + */ + while (curr != null) { + if (curr.val > curr.next.val) { + evenPoints++; + } else if (curr.val < curr.next.val) { + oddPoints++; + } + + // "curr.next.next" allows for the loop to skip over an index + curr = curr.next.next; + } + + if (oddPoints > evenPoints) { + return "Odd"; + } else if (oddPoints < evenPoints) { + return "Even"; + } else { + return "Tie"; + } } } diff --git a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java index 8444fceca..956750e67 100644 --- a/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java +++ b/lesson_12/structs_java/structs_app/src/main/java/com/codedifferently/lesson12/Stack.java @@ -10,17 +10,28 @@ public Stack() { public void push(int value) { // Your code here + ListNode newTop = new ListNode(value); + newTop.next = top; + top = newTop; } public int pop() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Empty"); + } + int oldTopVal = top.val; + top = top.next; + return oldTopVal; } public int peek() { - return 0; + if (isEmpty()) { + throw new IllegalStateException("Empty"); + } + return top.val; } public boolean isEmpty() { - return true; + return top == null; } } From 6ea2309fbec1c671db6c826150456924a5b0f1ac Mon Sep 17 00:00:00 2001 From: marleomac3 Date: Thu, 24 Oct 2024 01:40:44 +0000 Subject: [PATCH 2/3] feat: implemented TS method for gameResult --- lesson_12/structs_ts/src/lesson12.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index d4455564e..a0c2a285a 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,6 +6,27 @@ 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 oddPoints: number = 0; + let evenPoints: number = 0; + + let curr = head; + + while (curr != null && curr.next != null) { + if (curr.val > curr.next.val) { + evenPoints++; + } else if (curr.val < curr.next.val) { + oddPoints++; + } + curr = curr.next.next as ListNode; + } + + if (oddPoints > evenPoints) { + return "Odd"; + } else if (oddPoints < evenPoints) { + return "Even"; + } else { + return "Tie"; + } } } + \ No newline at end of file From e5c25a88767853be846646c05e0bc1b2aab09d61 Mon Sep 17 00:00:00 2001 From: marleomac3 Date: Thu, 24 Oct 2024 01:47:58 +0000 Subject: [PATCH 3/3] fix: removed lesson12.ts from PR due to failing tests --- lesson_12/structs_ts/src/lesson12.ts | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/lesson_12/structs_ts/src/lesson12.ts b/lesson_12/structs_ts/src/lesson12.ts index a0c2a285a..d4455564e 100644 --- a/lesson_12/structs_ts/src/lesson12.ts +++ b/lesson_12/structs_ts/src/lesson12.ts @@ -6,27 +6,6 @@ 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 { - let oddPoints: number = 0; - let evenPoints: number = 0; - - let curr = head; - - while (curr != null && curr.next != null) { - if (curr.val > curr.next.val) { - evenPoints++; - } else if (curr.val < curr.next.val) { - oddPoints++; - } - curr = curr.next.next as ListNode; - } - - if (oddPoints > evenPoints) { - return "Odd"; - } else if (oddPoints < evenPoints) { - return "Even"; - } else { - return "Tie"; - } + return ''; } } - \ No newline at end of file