Skip to content

Feat: Lesson_12 extra credit: typescript implementations added - James Capparell #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,59 @@

public class Lesson12 {

/**
* 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;
int score_even = 0;
int score_odd = 0;
int evenNum = 0;
int oddNum = 0;
for (int i = 0; i < getLength(head); i++) {
if (i % 2 == 0) {
ListNode resultNode = getElementAt(head, i);
evenNum = resultNode.val;
} else if (i % 2 == 1) {
ListNode resultNode = getElementAt(head, i);
oddNum = resultNode.val;
if (evenNum > oddNum) {
score_even += 1;
} else if (evenNum < oddNum) {
score_odd += 1;
}
}
}
if (score_even > score_odd) {
return "Even";
} else if (score_even < score_odd) {
return "Odd";
} else {
return "Tie";
}
}

public ListNode getElementAt(ListNode head, int position) {
ListNode current = head;
int index = 0;

// Traverse the list until reaching the desired position
while (current != null) {
if (index == position) {
return current; // Return the node at the specified position
}
current = current.next; // Move to the next node
index++; // Increment the index
}

return null; // Return null if the position is out of bounds
}

public int getLength(ListNode head) {
int length = 0;
ListNode current = head;

while (current != null) {
length++; // increment to keep count through each element
current = current.next; // moves on to next element
}

return length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ public Stack() {
}

public void push(int value) {
// Your code here
ListNode newNode = new ListNode(value, top);
top = newNode;
}

public int pop() {
return 0;
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
int value = top.val;
top = top.next;
return value;
}

public int peek() {
return 0;
if (isEmpty()) {
throw new IllegalStateException("Stack is empty");
}
return top.val;
}

public boolean isEmpty() {
return true;
return top == null;
}
}
36 changes: 31 additions & 5 deletions lesson_12/structs_ts/src/lesson12.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import { ListNode } from './list_node.js';

export class Lesson12 {
/**
* 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 gameResult(head: ListNode | null): string {
return '';
let scoreEven = 0;
let scoreOdd = 0;
let current: ListNode | null = head;
let index = 0;

while (current !== null) {
// Check if the index is even
if (index % 2 === 0) {
// Only check nextNode if current is not the last node
const nextNode = current.next;
if (nextNode != undefined) {
if (current.val > nextNode.val) {
scoreEven += 1;
} else if (current.val < nextNode.val) {
scoreOdd += 1;
}
}
}

// Move to the next node
current = current.next ?? null;
index++; // Increment the index
}

if (scoreEven > scoreOdd) {
return 'Even';
} else if (scoreEven < scoreOdd) {
return 'Odd';
} else {
return 'Tie';
}
}
}
21 changes: 15 additions & 6 deletions lesson_12/structs_ts/src/stack.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
import { ListNode } from './list_node.js';

export class Stack {
private top: ListNode | undefined;
private top?: ListNode;

constructor() {
this.top = undefined;
}

push(value: number): void {
throw new Error('Not implemented');
const newNode = new ListNode(value, this.top);
this.top = newNode;
}

pop(): number | undefined {
throw new Error('Not implemented');
if (this.isEmpty()) {
throw new Error('Stack is empty');
}
const value = this.top?.val; // Non-null assertion since we've checked for emptiness
this.top = this.top?.next; // No change needed here, TypeScript allows accessing next
return value;
}

peek(): number | null {
throw new Error('Not implemented');
peek(): number | undefined {
if (this.isEmpty()) {
throw new Error('Stack is empty');
}
return this.top?.val; // Non-null assertion since we've checked for emptiness
}

isEmpty(): boolean {
throw new Error('Not implemented');
return this.top === undefined; // Check for undefined since top is optional
}
}