From 243af1210dd3dd057eafcdf00be286f52a46599c Mon Sep 17 00:00:00 2001 From: cibezim <123107253+cibezim@users.noreply.github.com> Date: Sun, 17 Mar 2024 23:32:56 +0000 Subject: [PATCH] feat: adds lesson8 functions logic containsCycle, getMaximumBinaryTreeHeight, containsDuplicates --- .../com/codedifferently/lesson8/Lesson8.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/lesson_08/collections/collections_app/src/main/java/com/codedifferently/lesson8/Lesson8.java b/lesson_08/collections/collections_app/src/main/java/com/codedifferently/lesson8/Lesson8.java index 252fba1f..d4413d39 100644 --- a/lesson_08/collections/collections_app/src/main/java/com/codedifferently/lesson8/Lesson8.java +++ b/lesson_08/collections/collections_app/src/main/java/com/codedifferently/lesson8/Lesson8.java @@ -1,5 +1,6 @@ package com.codedifferently.lesson8; +import java.util.HashMap; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; @@ -14,14 +15,45 @@ public static void main(String[] args) { } public static boolean containsCycle(LinkedListNode head) { + if (head == null || head.next == null) { + return false; + } + + LinkedListNode slow = head; + LinkedListNode fast = head.next; + + while (fast != null && fast.next != null) { + if (slow == fast) { + return true; + } + slow = slow.next; + fast = fast.next.next; + } + return false; } public static int getMaximumBinaryTreeHeight(BinaryTreeNode root) { - return -1; + if (root == null) { + return 0; + } + + int leftHeight = getMaximumBinaryTreeHeight(root.left); + int rightHeight = getMaximumBinaryTreeHeight(root.right); + + return Math.max(leftHeight, rightHeight) + 1; } public static boolean containsDuplicates(String[] values) { + HashMap map = new HashMap<>(); + + for (String value : values) { + if (map.containsKey(value)) { + return true; + } + map.put(value, 1); + } + return false; }