From c5032c7fe01b0f3bc8c47813efa4ca96571a2ad2 Mon Sep 17 00:00:00 2001 From: Lizock Date: Tue, 23 Sep 2025 11:45:29 -0700 Subject: [PATCH 01/10] Completed oddSum --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..8fe92cd 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,7 +11,19 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + if(nums == null) { + return 0; + } + + int sum = 0; + + for (int i : nums) { + if (i % 2 != 0){ + sum = sum + i; + } + i++; + } + return sum; } /** From 6930281fcaf5cdc99adaf4c01e28aa376b59eb3f Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 19:00:54 -0700 Subject: [PATCH 02/10] Completed shortestWord problem --- src/Practice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 8fe92cd..94e59e3 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -38,7 +38,21 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + if (words == null || words.isEmpty()){ + throw new IllegalArgumentException("Set is empty"); + } + + String shortest = null; + for (String word : words){ + if (shortest == null){ + shortest = word; + } else if (word.length() < shortest.length()){ + shortest = word; + } else if (word.length() == shortest.length() && word.compareTo(shortest) < 0){ + shortest = word; + } + } + return shortest; } /** From fed5fc71a2f014bcfcb8f116b72a2f01ddea02da Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 19:30:54 -0700 Subject: [PATCH 03/10] Completed adults problem --- src/Practice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 94e59e3..091b7da 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -65,7 +66,18 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + if (ages == null){ + throw new NullPointerException(); + } + + Set names = new HashSet<>(); + + for (String name : ages.keySet()){ + if(ages.get(name) >= 18){ + names.add(name); + } + } + return names; } /** From 44ddaeca4e62e3da9e3e01ce7a9f6fa1d941395e Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 19:56:57 -0700 Subject: [PATCH 04/10] Completed biggestNumber problem --- src/Practice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 091b7da..59527d2 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -88,7 +88,20 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - return 0; + if (head == null){ + throw new NullPointerException(); + } + + int biggest = head.data; + ListNode current = head.next; + + while(current != null){ + if(current.data > biggest){ + biggest = current.data; + } + current = current.next; + } + return biggest; } /** From 594b1d86d5e8eaaea70f847c63c2b0a2c26dbfdc Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 20:42:17 -0700 Subject: [PATCH 05/10] Completed frequencies problem --- src/Practice.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 59527d2..477d8cc 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -118,7 +119,17 @@ public static int biggestNumber(ListNode head) { * @return a frequency map of values in the list */ public static Map frequencies(ListNode head) { - return null; + Map frequency = new HashMap<>(); + + ListNode current = head; + + while (current != null) { + T value = current.data; + frequency.put(value, frequency.getOrDefault(value, 0) + 1); + current = current.next; + } + + return frequency; } From 5311255aae5dba61ededd76182e77a35133ea29f Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 21:27:26 -0700 Subject: [PATCH 06/10] Completed levelCount problem --- src/Practice.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 477d8cc..9f144d0 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -142,7 +142,14 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + if(root == null){ + return 0; + } + + int leftLevels = levelCount(root.left); + int rightLevels = levelCount(root.right); + + return 1 + Math.max(leftLevels, rightLevels); } From ac60208be5532aa56fc9106fe894a2dda9af43ac Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 21:46:13 -0700 Subject: [PATCH 07/10] Completed sumAtLevel problem --- src/Practice.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 9f144d0..05ec4a4 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -177,7 +177,15 @@ public static int levelCount(BinaryTreeNode root) { * @return the sum of the nodes at the given level */ public static int sumAtLevel(BinaryTreeNode root, int level) { - return 0; + if (root == null){ + return 0; + } + + if (level == 1){ + return root.data; + } + + return sumAtLevel(root.left, level - 1) + sumAtLevel(root.right, level - 1); } From b460a307cdd4f8aba8ffb631160bf012e3958445 Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 22:16:47 -0700 Subject: [PATCH 08/10] Completed sumMatch problem and added sumTreeHelper method --- src/Practice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 05ec4a4..7d754de 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -200,7 +200,23 @@ public static int sumAtLevel(BinaryTreeNode root, int level) { * @return true if the sums are equal, false otherwise */ public static boolean sumMatch(BinaryTreeNode root, ListNode head) { - return false; + int treeSum = sumTreeHelper(root); + + int listSum = 0; + ListNode current = head; + while (current != null){ + listSum += current.data; + current = current.next; + } + + return treeSum == listSum; + } + + private static int sumTreeHelper(BinaryTreeNode node){ + if (node == null){ + return 0; + } + return node.data + sumTreeHelper(node.left) + sumTreeHelper(node.right); } /** From bc7145e808eaac1b8833906b51367a44a26990c3 Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 22:32:45 -0700 Subject: [PATCH 09/10] Completed graphSum problem and added dfsSumHelper method --- src/Practice.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 7d754de..e2b2dab 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -229,7 +229,22 @@ private static int sumTreeHelper(BinaryTreeNode node){ * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return dfsSumHelper(start, visited); + } + + private static int dfsSumHelper(Vertex vertex, Set> visited){ + if (vertex == null || visited.contains(vertex)){ + return 0; + } + + visited.add(vertex); + int sum = vertex.data; + + for (Vertex neighbor : vertex.neighbors){ + sum += dfsSumHelper(neighbor, visited); + } + return sum; } /** From 0ed8323f7c0d49e26210bbd6b7674e9e1e8cc53d Mon Sep 17 00:00:00 2001 From: Lizock Date: Wed, 24 Sep 2025 23:00:37 -0700 Subject: [PATCH 10/10] Completed sinkCount problem and added dfsSinkCountHelper method --- src/Practice.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index e2b2dab..2db49ed 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -256,6 +256,25 @@ private static int dfsSumHelper(Vertex vertex, Set> vis * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + Set> visited = new HashSet<>(); + return dfsSinkCountHelper(start, visited); + } + + private static int dfsSinkCountHelper(Vertex vertex, Set> visited) { + if (vertex == null || visited.contains(vertex)){ + return 0; + } + + visited.add(vertex); + + int count = 0; + if (vertex.neighbors.isEmpty()){ + count = 1; + } + + for (Vertex neighbor : vertex.neighbors) { + count += dfsSinkCountHelper(neighbor, visited); + } + return count; } } \ No newline at end of file