diff --git a/challenge_0/java/jdfurlan/README.md b/challenge_0/java/jdfurlan/README.md new file mode 100644 index 000000000..6c0a36a6e --- /dev/null +++ b/challenge_0/java/jdfurlan/README.md @@ -0,0 +1,17 @@ +# HelloWorld +###### Java 8 + +### 1. Approach to Solving the problem + +Make it unnecessarily slow! + +### 2. How to compile and run this code + +``` +javac HelloWorld.java +java HelloWorld +``` + +### 3. How this program works + +Simply run it and you'll see "Hello, World!" diff --git a/challenge_0/java/jdfurlan/src/HelloWorld.java b/challenge_0/java/jdfurlan/src/HelloWorld.java new file mode 100644 index 000000000..d92338fb9 --- /dev/null +++ b/challenge_0/java/jdfurlan/src/HelloWorld.java @@ -0,0 +1,12 @@ + +public class HelloWorld { + + public static void main(String[] args) { + char[] hw = "Hello, World!".toCharArray(); + for (char c : hw) { + System.out.print(c); + } + + } + +} diff --git a/challenge_1/java/jdfurlan/README.md b/challenge_1/java/jdfurlan/README.md new file mode 100644 index 000000000..384f780e7 --- /dev/null +++ b/challenge_1/java/jdfurlan/README.md @@ -0,0 +1,20 @@ +# ReverseAString +###### Java 8 + +### 1. Approach to Solving the problem + +Don't use built-in methods, just primitives and arrays + +### 2. How to compile and run this code + +``` +javac ReverseAString.java +java ReverseAString +``` + +### 3. How this program works + +Takes user input and converts to a character array +Track first and last values, store one in a temp variable, then swap +increment and decrement the positions and continue to swap. +For odd length strings it just leaves the middle value in place, since no swap needed diff --git a/challenge_1/java/jdfurlan/src/ReverseAString.java b/challenge_1/java/jdfurlan/src/ReverseAString.java new file mode 100644 index 000000000..37d31dc55 --- /dev/null +++ b/challenge_1/java/jdfurlan/src/ReverseAString.java @@ -0,0 +1,18 @@ + +import java.util.Scanner; + +public class ReverseAString { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char[] input = sc.next().toCharArray(); + int j = input.length - 1; + for (int i = 0; i < input.length / 2; i++, j--) { + char temp = input[j]; + input[j] = input[i]; + input[i] = temp; + } + System.out.println(String.valueOf(input)); + sc.close(); + } +} diff --git a/challenge_12/java/jdfurlan/README.md b/challenge_12/java/jdfurlan/README.md new file mode 100644 index 000000000..ff40996a4 --- /dev/null +++ b/challenge_12/java/jdfurlan/README.md @@ -0,0 +1,19 @@ +# Compression and Decompression 1 +###### Java 8 + +### 1. Approach to Solving the problem + +My first approach involved tracking the count of equal characters found in a row +and once characters changed, checking the number of equal characters and compressing if over the threshold + +### 2. How to compile and run this code + +``` +javac CompAndDecompOne.java +java CompAndDecompOne +``` + +### 3. How this program works + +Walk through strings and compress based on the threshold. Decompress based +on integer value of character used during compression. \ No newline at end of file diff --git a/challenge_12/java/jdfurlan/src/CompAndDecompOne.java b/challenge_12/java/jdfurlan/src/CompAndDecompOne.java new file mode 100644 index 000000000..bdf2e3f92 --- /dev/null +++ b/challenge_12/java/jdfurlan/src/CompAndDecompOne.java @@ -0,0 +1,133 @@ + +import java.util.List; +import java.util.Arrays; + +public class CompAndDecompOne { + // the compression threshold used to reduce string size. If too low it can + // actually increase the size of the string. Compression fails if threshold < 3 + // since the sequence `aa` would end up larger at `a#2` + private static final int THRESHOLD = 4; + + /** + * decompress walks through a compressed string and checks if it has found a + * '#' character, otherwise it just adds the character to the StringBuffer. + * If a '#' is found, it was determine the integer value following it, which + * could be 1->10 digits. Once the number is determined, that number - 1 of + * the last character will be added to the StringBuffer. The hardest part of + * this method was just checking for edge cases and indexOutOfBound errors. + * + * @param s + * the string to be decompressed + * @return decompressed version of s + */ + private static String decompress(String s) { + StringBuffer decompressed = new StringBuffer(); + StringBuffer subString = new StringBuffer(); + char last = 0; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '#') { + i++; + int num = Character.getNumericValue(s.charAt(i)); + if (i != s.length() - 1 && Character.isDigit(s.charAt(i + 1))) { + while (Character.isDigit(s.charAt(i)) && i < s.length() - 1) { + num = num * 10 + Character.getNumericValue(s.charAt(i + 1)); + i++; + } + } + for (int j = 1; j < num; j++) { + decompressed.append(last); + } + } else { + last = c; + decompressed.append(last); + + } + } + + return decompressed.toString(); + } + + /** + * compress walks through an uncompressed string and adds every character to + * a subString buffer until the character changes. Once we see a new + * character, if the size of our buffer is >= THRESHOLD, we must compress, + * else just append the buffer to our final compressedBuffer. + * + * @param s + * string to compress + * @return compressed version of s + */ + private static String compress(String s) { + StringBuffer compressedBuffer = new StringBuffer(); + StringBuffer subString = new StringBuffer(); + char last = s.charAt(0); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c != last || i == s.length() - 1) { + if (i == s.length() - 1) + subString.append(c); + if (subString.length() >= THRESHOLD) { + compressedBuffer.append(last + "#" + subString.length()); + } else { + compressedBuffer.append(subString.toString()); + } + subString = new StringBuffer(); + subString.append(c); + } else { + subString.append(c); + } + last = c; + } + return compressedBuffer.toString(); + + } + + /** + * Just an alternate version of the other compression that uses slightly + * less space. + * + * @param s + * string to compress + * @return compressed version of s + */ + private static String compressAlternate(String s) { + StringBuffer sb = new StringBuffer(); + char lastChar = s.charAt(0); + int count = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) != lastChar || i == s.length() - 1) { + if (i == s.length() - 1) + count++; + if (count >= THRESHOLD) { + sb.append(lastChar + "#" + count); + lastChar = s.charAt(i); + count = 1; + } else { + for (int j = 0; j < count; j++) { + sb.append(lastChar); + } + lastChar = s.charAt(i); + count = 1; + } + } else { + lastChar = s.charAt(i); + count++; + } + } + return sb.toString(); + } + + public static void main(String[] args) { + List list = Arrays.asList("aaaabbbcccaaaaccccccc", "abc", + "bbbfhdjwuwuwuufffffkdkkkkkddddiiiiiqqooodllal", "hhheeelllppp", "jjjjhhhhssssiiiiqqqqllllzzzzppppoooo", + "iiiiiiiiiiiiiiiiiiiiiiiiiiiii", "ggggtttkkkklllppppooouuuuuuuuuuuu"); + for (String s : list) { + String comp = compress(s); + System.out.println("Uncompressed: " + s); + System.out.println("Compressed: " + comp); + System.out.println("Decompressed: " + decompress(comp) + "\n"); + } + + } +} diff --git a/challenge_2/java/jdfurlan/README.md b/challenge_2/java/jdfurlan/README.md new file mode 100644 index 000000000..99773a1f2 --- /dev/null +++ b/challenge_2/java/jdfurlan/README.md @@ -0,0 +1,22 @@ +# SingleNumber +###### Java 8 + +### 1. Approach to Solving the problem + +Maps are usually good for storing unique values since there +can only be 1 of any given key in a map. A set is also good for this +but since we needed to track the occurence of a key, I decided to use map +and track occurance with the value + +### 2. How to compile and run this code + +``` +javac SingleNumber.java +java SingleNumber +``` + +### 3. How this program works + +If a key doesn't already exist in the map, put it in and set its value to 1 +Otherwise, key the value with associated key and increment by 1. +Next, interate through the keySet and check when the value == 1, then return and exit diff --git a/challenge_2/java/jdfurlan/src/SingleNumber.java b/challenge_2/java/jdfurlan/src/SingleNumber.java new file mode 100644 index 000000000..323f68153 --- /dev/null +++ b/challenge_2/java/jdfurlan/src/SingleNumber.java @@ -0,0 +1,25 @@ + +import java.util.HashMap; + +public class SingleNumber { + + public static void main(String[] args) { + String[] arr = { "2", "a", "l", "3", "l", "4", "k", "2", "3", "4", "a", "6", "c", "4", "m", "6", "m", "k", "9", + "10", "9", "8", "7", "8", "10", "7" }; + HashMap map = new HashMap(); + for (String s : arr) { + if (map.containsKey(s)) { + map.put(s, map.get(s) + 1); + } else { + map.put(s, 1); + } + } + for (String s : map.keySet()) { + if (map.get(s) == 1) { + System.out.println(s); + System.exit(0); + } + } + } + +} diff --git a/challenge_3/java/jdfurlan/README.md b/challenge_3/java/jdfurlan/README.md new file mode 100644 index 000000000..6a4ab4ba5 --- /dev/null +++ b/challenge_3/java/jdfurlan/README.md @@ -0,0 +1,22 @@ +# MajorityElement +###### Java 8 + +### 1. Approach to Solving the problem + +I used a map to make sure all equivalent elements were mapped to the same key +and tracked their frequencies with their value + +### 2. How to compile and run this code + +Compile the Node class first + +``` +javac MajorityElement.java +java MajorityElement +``` + +### 3. How this program works + +As we iterate through the list, if the map has never seen this element before, +place it in the map and initialize its frequency at 1. If we have seen it before, increment +its frequency, check if > n.length/2 and print if true, else update the frequency. diff --git a/challenge_3/java/jdfurlan/src/MajorityElement.java b/challenge_3/java/jdfurlan/src/MajorityElement.java new file mode 100644 index 000000000..16c9e8150 --- /dev/null +++ b/challenge_3/java/jdfurlan/src/MajorityElement.java @@ -0,0 +1,25 @@ + +import java.util.HashMap; + +public class MajorityElement { + + public static void main(String[] args) { + int[] nums = { 2, 2, 3, 7, 5, 7, 7, 7, 4, 7, 2, 7, 4, 5, 6, 7, 7, 8, 6, 7, 7, 8, 10, 12, 29, 30, 19, 10, 7, 7, + 7, 7, 7, 7, 7, 7, 7 }; + HashMap map = new HashMap(); + for (int n : nums) { + if (map.containsKey(n)) { + int f = map.get(n) + 1; + if (f > (nums.length / 2)) { + System.out.println(n); + System.exit(0); + } + map.put(n, f); + } else { + map.put(n, 1); + } + } + + } + +} diff --git a/challenge_4/java/jdfurlan/README.md b/challenge_4/java/jdfurlan/README.md new file mode 100644 index 000000000..9a1b59601 --- /dev/null +++ b/challenge_4/java/jdfurlan/README.md @@ -0,0 +1,24 @@ +# InvertBinaryTree +###### Java 8 + +### 1. Approach to Solving the problem + +Trees suck if you haven't messed with them in a while! +Swapping the values is relatively easy using an A-B-C swap approach. +The approach to print was to do a [Breadth-First Traversal](https://www.cs.bu.edu/teaching/c/tree/breadth-first/) + +### 2. How to compile and run this code + + +``` +javac InvertBinaryTree.java +java InvertBinaryTree +``` + +### 3. How this program works + +Hand the root node to the swap function. There, while the node is not null +you must take either child and store in a temp, then swap into the other child's place +then swap the child you didn't store into a temp with the temp child. +Call the swap function recursively with either child first, then the other child. +BFT through the tree to print values by level. diff --git a/challenge_4/java/jdfurlan/src/InvertBinaryTree.java b/challenge_4/java/jdfurlan/src/InvertBinaryTree.java new file mode 100644 index 000000000..4d23cd1f2 --- /dev/null +++ b/challenge_4/java/jdfurlan/src/InvertBinaryTree.java @@ -0,0 +1,54 @@ + +import java.util.LinkedList; +import java.util.Queue; + +public class InvertBinaryTree { + + public static void main(String[] args) { + Node root = new Node(4); + Node two = new Node(2); + Node seven = new Node(7); + Node three = new Node(3); + Node six = new Node(6); + Node nine = new Node(9); + Node one = new Node(1); + + root.left = two; + root.right = seven; + two.left = one; + two.right = three; + seven.left = six; + seven.right = nine; + + print(root); + invertBT(root); + System.out.println(); + print(root); + + + } + //breadth-first traversal using Queue interface + private static void print(Node root) { + Queue children = new LinkedList(); + children.add(root); + while (!children.isEmpty()) { + Node temp = children.poll(); + System.out.print(temp.data); + if (temp.left != null) + children.add(temp.left); + if (temp.right != null) + children.add(temp.right); + } + } + //basic swap. Once swapped, swap left subtree then right subtree until null + private static void invertBT(Node root) { + if (root == null) + return; + Node temp = root.right; + root.right = root.left; + root.left = temp; + invertBT(root.left); + invertBT(root.right); + } + +} diff --git a/challenge_4/java/jdfurlan/src/Node.java b/challenge_4/java/jdfurlan/src/Node.java new file mode 100644 index 000000000..7f0e653e5 --- /dev/null +++ b/challenge_4/java/jdfurlan/src/Node.java @@ -0,0 +1,11 @@ + +public class Node { + int data; + Node left; + Node right; + public Node(int data){ + this.data = data; + left = null; + right = null; + } +} diff --git a/challenge_5/java/jdfurlan/README.md b/challenge_5/java/jdfurlan/README.md new file mode 100644 index 000000000..e33b2293d --- /dev/null +++ b/challenge_5/java/jdfurlan/README.md @@ -0,0 +1,23 @@ +# Ranges +###### Java 8 + +### 1. Approach to Solving the problem + +I thought about various ways to compare values in a string. +Using a boolean array is a common solution for comparing characters. + +### 2. How to compile and run this code + + +``` +javac Ranges.java +java Ranges +``` + +### 3. How this program works + +Create a boolean array of size 128, the ASCII code limit +(this program wouldn't work for Unicode, too many chars) +Every char value maps to an index in the array, walk through string +s and put each boolean at the char index to true. Then walk through string +t and as soon as you reach a boolean that is false, we know that's our missing char! diff --git a/challenge_5/java/jdfurlan/src/FindTheDifference.java b/challenge_5/java/jdfurlan/src/FindTheDifference.java new file mode 100644 index 000000000..71ae89b77 --- /dev/null +++ b/challenge_5/java/jdfurlan/src/FindTheDifference.java @@ -0,0 +1,21 @@ + +public class FindTheDifference { + + public static void main(String[] args) { + String s = "abcd"; + String t = "dbeca"; + + boolean[] b = new boolean[128]; + for (int i = 0; i < s.length(); i++) { + b[s.charAt(i)] = true; + } + for (int i = 0; i < t.length(); i++) { + if (!b[t.charAt(i)]) { + System.out.println(t.charAt(i) + " found at index: " + i); + System.exit(0); + } + } + + } + +} diff --git a/challenge_6/java/jdfurlan/README.md b/challenge_6/java/jdfurlan/README.md new file mode 100644 index 000000000..8c878bd7f --- /dev/null +++ b/challenge_6/java/jdfurlan/README.md @@ -0,0 +1,26 @@ +# Ranges +###### Java 8 + +### 1. Approach to Solving the problem + +I very much didn't want to use any additional data structures to help +with traversing the array, and wanted to keep it as is. The reason an +array list is used is simply because I needed a dynamicly sized array +since there's no way to know beforehand how many ranges we will find. + +### 2. How to compile and run this code + +``` +javac Ranges.java +java Ranges +``` + +### 3. How this program works + +Assuming we have at least 2 values, we initialize the first range point +as the first value, and the last to 0. We then check that the next value is +equal to the first + 1, meaning incremental. If true, keep incrementing and +setting the last range point to the current value. To handle index out of bounds +I just break out of the loop when i == the length of the array - 1. The final check +is if last = 0, we know it was never updated and we have array of size 2 but the second +value isn't incremental from the first, so return empty list. diff --git a/challenge_6/java/jdfurlan/src/Ranges.java b/challenge_6/java/jdfurlan/src/Ranges.java new file mode 100644 index 000000000..413acbee3 --- /dev/null +++ b/challenge_6/java/jdfurlan/src/Ranges.java @@ -0,0 +1,30 @@ + +import java.util.ArrayList; + +public class Ranges { + + public static void main(String[] args) { + int[][] arr = { { 1, 2, 3, 4, 8, 9, 10, 12, 13, 14 }, { 1, 2, 3, 4, 9, 10, 15 }, { 1, 2 }, { 1 }, + { 1, 2, 3, 4, 5, 8, 9, 10}, { 1, 3 }}; + for(int[] a : arr){ + System.out.println(findRanges(a)); + } + } + + private static ArrayList findRanges(int[] arr) { + ArrayList res = new ArrayList(); + int first, last; + for (int i = 0; i < arr.length - 1; i++) { + first = arr[i]; + last = 0; + while (i < arr.length-1 && arr[i + 1] == arr[i] + 1) { + i++; + last = arr[i]; + } + if (last != 0) + res.add(first + "->" + last); + } + return res; + } + +} diff --git a/challenge_7/java/jdfurlan/README.md b/challenge_7/java/jdfurlan/README.md new file mode 100644 index 000000000..6ac223d70 --- /dev/null +++ b/challenge_7/java/jdfurlan/README.md @@ -0,0 +1,22 @@ +# Find the Missing Number +###### Java 8 + +### 1. Approach to Solving the problem + +I kept thinking about using different data structures which really annoying me! +I then though about the relationship between what the actual array would look like +without the missig number, and that's when I realized the only thing separating them +would be the sum of their values! + +### 2. How to compile and run this code + +``` +javac FindTheMissingNumber.java +java FindTheMissingNumber +``` + +### 3. How this program works + +Use "i" to track the incremental sum as if no values were missing. Then use +the same "i" to sum the index values of the array given. Compare the differences +and that's the missing number! \ No newline at end of file diff --git a/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java b/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java new file mode 100644 index 000000000..c66590dd8 --- /dev/null +++ b/challenge_7/java/jdfurlan/src/FindTheMissingNumber.java @@ -0,0 +1,20 @@ + +public class FindTheMissingNumber { + + public static void main(String[] args) { + int[] arr = { 6, 3, 4, 8, 1, 2, 0, 9, 10, 7 }; // missing is 5 + System.out.println(findMissingNum(arr)); + } + + private static int findMissingNum(int[] arr) { + int actualSum = 0; + int thisSum = 0; + int i; + for (i = 0; i < arr.length; i++) { + actualSum += i+1; + thisSum += arr[i]; + } + return actualSum - thisSum; + } + +} diff --git a/challenge_8/java/jdfurlan/src/RandomPointerLinkedList.java b/challenge_8/java/jdfurlan/src/RandomPointerLinkedList.java new file mode 100644 index 000000000..cd8b0fb80 --- /dev/null +++ b/challenge_8/java/jdfurlan/src/RandomPointerLinkedList.java @@ -0,0 +1,62 @@ + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Random; +/** + * + * @author jesse furlan + * + * THIS CODE IS INCOMPLETE + * This is simply my best effort to finish this challenge + * Logic error with assigning the nodes randomly. Will come back to this + * challenge at a later time. + * + */ +public class RandomPointerLinkedList { + public static void main(String[] args) { + ArrayList listNodes = new ArrayList(); + ArrayList data = new ArrayList(); + ListNode fifth = new ListNode("5", null, null); + ListNode fourth = new ListNode("4", fifth, null); + ListNode third = new ListNode("3", fourth, null); + ListNode second = new ListNode("2", third, null); + ListNode first = new ListNode("1", second, null); + listNodes.add(first); + listNodes.add(second); + listNodes.add(third); + listNodes.add(fourth); + listNodes.add(fifth); + HashMap map = new HashMap(); + for(ListNode n : listNodes){ + data.add(n.data); + map.put(n.data, n); + } + genRandomNodes(listNodes,map,data); + } + + private static ArrayList genRandomNodes(ArrayList listNodes, HashMap map, ArrayList data) { + Random r = new Random(); + + for(ListNode node : listNodes){ + int idx = r.nextInt(data.size()); + node.random = (ListNode) map.get(data.get(idx)); + data.remove(idx); + } + return null; + } + +} + +class ListNode { + ListNode next; + ListNode random; + String data; + + public ListNode(String data, ListNode next, ListNode random) { + this.next = next; + this.random = random; + this.data = data; + + } +} diff --git a/challenge_9/java/jdfurlan/README.md b/challenge_9/java/jdfurlan/README.md new file mode 100644 index 000000000..bfdac7e5e --- /dev/null +++ b/challenge_9/java/jdfurlan/README.md @@ -0,0 +1,27 @@ +# Squares +###### Java 8 + +### 1. Approach to Solving the problem + +I struggled with this problem for a while. Originally I made a slow solution +that basically traversed through the array until the number was > than current and placed it there. +This was slow and bad. slack member **zmiller91**'s solution is what I implemented, but only after I fully +understood his approach. It was very clean and elegant and I learned a lot from it. + +### 2. How to compile and run this code + +``` +javac Squares.java +java Squares +``` + +### 3. How this program works + +Add all negatives squared to position 0 of the negatives list (this is bad I believe +as it results in every value shifting at every insertion) and add all positives to their own list. +Then, start counting through both lists from 0, and while at least one list still has values (index < length) +continue to get the value and add it to the result after comparing it with the negative value at that index. +The way index out of bounds is avoided is by constantly checking if the indexs are < length of their +respective list. If it ever exceeds the length, set that value to MAX_VALUE so it will always be greater than the +positive or negative value it was compared with, essentially nullifying that index. Values are touched only once +for O(n); \ No newline at end of file diff --git a/challenge_9/java/jdfurlan/src/Squares.java b/challenge_9/java/jdfurlan/src/Squares.java new file mode 100644 index 000000000..89d6dc64c --- /dev/null +++ b/challenge_9/java/jdfurlan/src/Squares.java @@ -0,0 +1,50 @@ + +import java.util.ArrayList; + +public class Squares { + + public static void main(String[] args) { + int[] arr = {-9,4,9}; + + ArrayList myList = orderSquares(arr); + for (int i : myList) { + System.out.print(i + " "); + } + + } + + private static ArrayList orderSquares(int[] arr) { + ArrayList result = new ArrayList(); + ArrayList negatives = new ArrayList(); + ArrayList positives = new ArrayList(); + + for (int i : arr) { + if (i < 0) { + negatives.add(0, i * i); + } else { + positives.add(i * i); + } + } + int posIdx = 0; + int negIdx = 0; + + while (posIdx < positives.size() || negIdx < negatives.size()) { + + int p = (posIdx < positives.size()) ? positives.get(posIdx) : Integer.MAX_VALUE; + int n = (negIdx < negatives.size()) ? negatives.get(negIdx) : Integer.MAX_VALUE; + + if (p < n) { + result.add(p); + posIdx++; + + } else { + result.add(n); + negIdx++; + + } + } + + return result; + } + +}