From 94f8372dedff12ac73e15d98ab6699bd46eae4fc Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 12 Oct 2020 22:53:29 +0000 Subject: [PATCH 1/4] Bump junit from 4.12 to 4.13.1 Bumps [junit](https://github.com/junit-team/junit4) from 4.12 to 4.13.1. - [Release notes](https://github.com/junit-team/junit4/releases) - [Changelog](https://github.com/junit-team/junit4/blob/main/doc/ReleaseNotes4.12.md) - [Commits](https://github.com/junit-team/junit4/compare/r4.12...r4.13.1) Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 10f4609..57c82d5 100644 --- a/pom.xml +++ b/pom.xml @@ -81,7 +81,7 @@ junit junit - 4.12 + 4.13.1 From 516fc7e727ef1d6641961094240facd41b3ddb0a Mon Sep 17 00:00:00 2001 From: Yogen Rai Date: Sat, 14 Nov 2020 11:02:22 -0600 Subject: [PATCH 2/4] Added O(1) solution for populating next pointer in perfect binary tree --- pom.xml | 3 +- .../algorithm/trees/PopulateNextRight.java | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/eprogrammerz/examples/algorithm/trees/PopulateNextRight.java diff --git a/pom.xml b/pom.xml index 10f4609..c828cf0 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,8 @@ org.projectlombok lombok - 1.16.8 + 1.18.12 + provided diff --git a/src/main/java/com/eprogrammerz/examples/algorithm/trees/PopulateNextRight.java b/src/main/java/com/eprogrammerz/examples/algorithm/trees/PopulateNextRight.java new file mode 100644 index 0000000..3718273 --- /dev/null +++ b/src/main/java/com/eprogrammerz/examples/algorithm/trees/PopulateNextRight.java @@ -0,0 +1,82 @@ +package com.eprogrammerz.examples.algorithm.trees; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; + +/** + * You are given a perfect binary tree where all leaves are on the same level, + * and every parent has two children. The binary tree has the following definition: + * struct Node { + * int val; + * Node *left; + * Node *right; + * Node *next; + * } + * + * https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ + */ +public class PopulateNextRight { + public Node connect(Node root) { + Node curr = root; + + while (curr != null && curr.left != null) { + Node runner = curr.left; + Node temp = runner; + Node prev = null; + + while (curr != null) { + runner = curr.left; + if (prev != null) { + prev.next = runner; + } + + runner.next = curr.right; + runner = runner.next; + prev = runner; + curr = curr.next; + } + + curr = temp; + } + + return root; + } + + @Test + public void test() { + Node root = new Node(1); + root.left = new Node(2); + root.right = new Node(3); + root.left.left = new Node(4); + root.left.right = new Node(5); + root.right.left = new Node(6); + root.right.right = new Node(7); + + Node populated = connect(root); + + assertEquals(3, populated.left.next.val); + assertEquals(6, populated.left.right.next.val); + } + + static class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() {} + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } + } +} From 86799b2de7a5f2187a95130be12b7b1e7fda1385 Mon Sep 17 00:00:00 2001 From: Yogen Rai Date: Sat, 3 Apr 2021 14:38:24 -0500 Subject: [PATCH 3/4] Add example to determine if array product is negative, positive or zero --- .../algorithm/leetcode/Multiplication.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/com/eprogrammerz/examples/algorithm/leetcode/Multiplication.java diff --git a/src/main/java/com/eprogrammerz/examples/algorithm/leetcode/Multiplication.java b/src/main/java/com/eprogrammerz/examples/algorithm/leetcode/Multiplication.java new file mode 100644 index 0000000..2d17fbd --- /dev/null +++ b/src/main/java/com/eprogrammerz/examples/algorithm/leetcode/Multiplication.java @@ -0,0 +1,25 @@ +package com.eprogrammerz.examples.algorithm.leetcode; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +// Return 1 if multiplication is +ve, -1 if negative and 0 if 0 +public class Multiplication { + public int solution(int[] A) { + int neg = 0; + for (int a : A) { + if (a < 0) neg++; + if (a == 0) return 0; + } + + return neg % 2 == 0 ? 1 : -1; + } + + @Test + public void test() { + assertEquals(1, solution(new int[]{-1,2,3,-1})); + assertEquals(-1, solution(new int[]{-1,2,3,-1,-2})); + assertEquals(0, solution(new int[]{-1,2,3,-1,-2,0})); + } +} From 9b7c9b6008206d3d094c503ce0a57aba88f99480 Mon Sep 17 00:00:00 2001 From: Yogen Rai Date: Sat, 8 May 2021 09:59:55 -0500 Subject: [PATCH 4/4] Added solution for Anadrome - Anagram of Palindrome --- pom.xml | 14 ++++++-- .../examples/algorithm/general/Anadrome.java | 34 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/eprogrammerz/examples/algorithm/general/Anadrome.java diff --git a/pom.xml b/pom.xml index 5f4acee..30891ae 100644 --- a/pom.xml +++ b/pom.xml @@ -12,8 +12,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.8 - 1.8 + 11 + 11 @@ -134,6 +134,16 @@ 2.1 test + + org.apache.beam + beam-sdks-java-harness + 2.22.0 + + + com.fasterxml.jackson.core + jackson-databind + 2.12.3 + diff --git a/src/main/java/com/eprogrammerz/examples/algorithm/general/Anadrome.java b/src/main/java/com/eprogrammerz/examples/algorithm/general/Anadrome.java new file mode 100644 index 0000000..a153640 --- /dev/null +++ b/src/main/java/com/eprogrammerz/examples/algorithm/general/Anadrome.java @@ -0,0 +1,34 @@ +package com.eprogrammerz.examples.algorithm.general; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Return number of character to be added to make a given string 'word' a anagram of palindrome. + */ +public class Anadrome { + public int changeToAnadrome(String word) { + int[] map = new int[128]; + int n = word.length(); + for (int i = 0; i < n; i++) { + map[word.charAt(i)]++; + } + + int odds = 0; + + for (int count: map) { + if (count % 2 != 0) odds++; + } + + if (odds == 0) return 0; + return odds - 1; + } + + @Test + public void test() { + assertEquals(1, changeToAnadrome("abcb")); + assertEquals(2, changeToAnadrome("abc")); + assertEquals(0, changeToAnadrome("tatoo")); + } +}