diff --git a/src/Practice.java b/src/Practice.java index 01da8d0..85274f1 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,3 +1,5 @@ +import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -11,7 +13,14 @@ public class Practice { * @return the sum of the odd numbers in the array */ public static int oddSum(int[] nums) { - return 0; + int sum = 0; + if(nums == null) return sum; + for(int num : nums){ + if(num%2 == 1 || num%2 == -1){ + sum += num; + } + } + return sum; } /** @@ -26,7 +35,20 @@ public static int oddSum(int[] nums) { * @throws NullPointerException if words is null */ public static String shortestWord(Set words) { - return null; + int wordLength = Integer.MAX_VALUE; + String wordChamp = ""; + for (String word : words) { + if(word.length() < wordLength){ + wordChamp = word; + wordLength = word.length(); + } + else if(word.length() == wordLength){ + if(word.compareTo(wordChamp) < 0){ + wordChamp = word; + } + } + } + return wordChamp; } /** @@ -39,7 +61,13 @@ public static String shortestWord(Set words) { * @throws NullPointerException if ages is null */ public static Set adults(Map ages) { - return null; + Set returnable = new HashSet<>(); + for(String name : ages.keySet()){ + if(ages.get(name) >= 18){ + returnable.add(name); + } + } + return returnable; } /** @@ -50,7 +78,14 @@ public static Set adults(Map ages) { * @throws IllegalArgumentException if head is null */ public static int biggestNumber(ListNode head) { - return 0; + int kingNum = Integer.MIN_VALUE; + while(head != null){ + if(head.data > kingNum){ + kingNum = head.data; + } + head = head.next; + } + return kingNum; } /** @@ -67,7 +102,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 returnable = new HashMap<>(); + while(head != null){ + if(!returnable.containsKey(head.data)){ + returnable.put(head.data, 1); + } + else{ + returnable.replace(head.data, returnable.get(head.data)+1); + } + head = head.next; + } + return returnable; } @@ -80,7 +125,14 @@ public static Map frequencies(ListNode head) { * @return the number of levels in the tree */ public static int levelCount(BinaryTreeNode root) { - return 0; + return levelCountHelper(root, 0); + } + + public static int levelCountHelper(BinaryTreeNode root, int level) { + if (root == null) return level; + int leftLevel = levelCountHelper(root.left, level + 1); + int rightLevel = levelCountHelper(root.right, level + 1); + return Math.max(leftLevel, rightLevel); } @@ -108,7 +160,13 @@ 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; + return sumAtLevelHelper(root, level); + } + + public static int sumAtLevelHelper(BinaryTreeNode root, int level) { + if (root == null) return 0; + if(level == 1) return root.data; + return sumAtLevelHelper(root.left, level - 1) + sumAtLevelHelper(root.right, level - 1); } @@ -123,9 +181,28 @@ 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) { + int sumList = sumMatchList(head); + int sumTree = sumMatchTree(root); + if(sumList == sumTree) return true; return false; } + public static int sumMatchList(ListNode head) { + int sum = 0; + while(head != null){ + sum += head.data; + head = head.next; + } + return sum; + } + + public static int sumMatchTree(BinaryTreeNode root) { + if(root == null) return 0; + int left = sumMatchTree(root.left); + int right = sumMatchTree(root.right); + return root.data + left + right; + } + /** * Returns the sum of all the vertices in a graph that are reachable from a given * starting vertex. @@ -136,7 +213,21 @@ public static boolean sumMatch(BinaryTreeNode root, ListNode h * @return the sum of all the vertices */ public static int graphSum(Vertex start) { - return 0; + if(start == null) return 0; + int sum = 0; + Set> visited = new HashSet<>(); + graphSumHelper(start, visited); + for(Vertex cur : visited){ + sum+= cur.data; + } + return sum; + } + public static void graphSumHelper(Vertex start, Set> visited){ + if (start == null || visited.contains(start)) return; + visited.add(start); + for(Vertex neighbor : start.neighbors){ + graphSumHelper(neighbor, visited); + } } /** @@ -148,6 +239,23 @@ public static int graphSum(Vertex start) { * @return the count of vertices with outdegree 0 */ public static int sinkCount(Vertex start) { - return 0; + if(start == null) return 0; + int sum = 0; + Set> visited = new HashSet<>(); + sinkCountHelper(start, visited); + for(Vertex cur : visited){ + if(cur.neighbors.size() == 0 || cur.neighbors == null){ + sum++; + } + } + return sum; + } + + public static void sinkCountHelper(Vertex start, Set> visited){ + if(start == null || visited.contains(start)) return; + visited.add(start); + for(Vertex neighbor : start.neighbors){ + sinkCountHelper(neighbor, visited); + } } } \ No newline at end of file