diff --git a/Algorithms/greedy-algorithum/TextJustification.java b/Algorithms/greedy-algorithum/TextJustification.java index 4446474d..7d97348c 100644 --- a/Algorithms/greedy-algorithum/TextJustification.java +++ b/Algorithms/greedy-algorithum/TextJustification.java @@ -30,6 +30,78 @@ public static void main(String[] args) { System.out.println(fullJustify_rev3(new String[]{"What", "must", "be", "acknowledgment", "shall", "be"}, 16)); System.out.println(fullJustify_rev3(new String[]{"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"}, 20)); System.out.println(fullJustify_rev3(new String[]{"Science", "is", "what", "w", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"}, 20)); + + System.out.println("********************************** Solution 5 **********************************"); + System.out.println(fullJustify_rev4(new String[]{"This", "is", "an", "example", "of", "text", "justification."}, 16)); + System.out.println(fullJustify_rev4(new String[]{"What", "must", "be", "acknowledgment", "shall", "be"}, 16)); + System.out.println(fullJustify_rev4(new String[]{"Science", "is", "what", "we", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"}, 20)); + System.out.println(fullJustify_rev4(new String[]{"Science", "is", "what", "w", "understand", "well", "enough", "to", "explain", "to", "a", "computer.", "Art", "is", "everything", "else", "we", "do"}, 20)); + + } + + public static List fullJustify_rev4(String[] words, int max) { + int len = words.length; + List rows = new ArrayList<>(); + int i = 0; + while (i < len) { + TextRow curRow = new TextRow(max); + while (i < len && curRow.canInsert(words[i])) { + curRow.insert(words[i++]); + } + rows.add(curRow); + } + List result = new ArrayList<>(); + for (int j = 0; j < rows.size(); j++) { + TextRow row = rows.get(j); + if (j == rows.size() - 1) { + String curRow = String.join(" ", row.words); + result.add(curRow + " ".repeat(max - curRow.length())); + } else { + result.add(row.flat()); + } + } + return result; + } + + static class TextRow { + List words; + int chars, spaces, max; + + TextRow(int max) { + words = new ArrayList<>(); + this.max = max; + } + + boolean canInsert(String word) { + int curLen = chars + spaces + (chars > 0 ? 1 : 0); + return curLen + word.length() <= max; + } + + void insert(String word) { + words.add(word); + spaces += chars > 0 ? 1 : 0; + chars += word.length(); + } + + String flat() { + int wordsCount = words.size(); + int spaces = max - chars; + if (wordsCount == 1) { + return words.get(0) + " ".repeat(spaces); + } + int split = wordsCount - 1; + int each = spaces / split, rem = spaces % split; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < wordsCount - 1; i++) { + sb.append(words.get(i)); + sb.append(" ".repeat(each)); + if (rem-- > 0) { + sb.append(" "); + } + } + sb.append(words.get(wordsCount - 1)); + return sb.toString(); + } } public static List fullJustify_rev3(String[] words, int max) { diff --git a/Algorithms/subsets/Permutations.java b/Algorithms/recursion-backtracking/Permutations.java similarity index 67% rename from Algorithms/subsets/Permutations.java rename to Algorithms/recursion-backtracking/Permutations.java index 10798301..e1f2bc2f 100644 --- a/Algorithms/subsets/Permutations.java +++ b/Algorithms/recursion-backtracking/Permutations.java @@ -23,6 +23,7 @@ public class Permutations { public static void main(String[] args) { System.out.println(permute(new int[]{1, 2, 3})); System.out.println(permute_elegant(new int[]{1, 2, 3})); + System.out.println(permute_elegant_2(new int[]{1, 2, 3})); } public static List> permute(int[] nums) { @@ -74,4 +75,28 @@ public static List> permute_elegant(int[] nums) { permute(nums, nums.length); return output; } + + public static List> permute_elegant_2(int[] nums) { + int len = nums.length; + boolean[] taken = new boolean[len]; + List> output = new ArrayList<>(); + backtrack(nums, taken, output, new LinkedList<>()); + return output; + } + + private static void backtrack(int[] nums, boolean[] taken, List> output, LinkedList current) { + if (current.size() == nums.length) { + output.add(new ArrayList<>(current)); + } else { + for (int i = 0; i < nums.length; i++) { + if (!taken[i]) { + taken[i] = true; + current.add(nums[i]); + backtrack(nums, taken, output, current); + taken[i] = false; + current.removeLast(); + } + } + } + } } diff --git a/dataStructures/arraysAndString/LongestSubstringWithoutRepeating.java b/dataStructures/arraysAndString/LongestSubstringWithoutRepeating.java index fea6fbd2..af1d4bb0 100644 --- a/dataStructures/arraysAndString/LongestSubstringWithoutRepeating.java +++ b/dataStructures/arraysAndString/LongestSubstringWithoutRepeating.java @@ -28,27 +28,20 @@ public static void main(String[] args) { System.out.println(lengthOfLongestSubstring2("dvdf") + " should be [2]"); System.out.println(lengthOfLongestSubstring("pwwkew") + " should be [3]"); System.out.println(lengthOfLongestSubstring2("pwwkew") + " should be [3]"); + System.out.println(lengthOfLongestSubstring2("") + " should be [3]"); } public static int lengthOfLongestSubstring2(String s) { Set set = new HashSet<>(); - int max = Integer.MIN_VALUE, p1 = 0; + int max = 0, p1 = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); - if (set.contains(c)) { + while (p1 <= i && set.contains(c)) { // Then keep removing from the queue till you encounter the current char. - while (p1 <= i) { - char p1Char = s.charAt(p1++); - if (p1Char == c) { - break; - } else { - set.remove(p1Char); - } - } + set.remove(s.charAt(p1++)); } set.add(c); max = Math.max(max, set.size()); - // System.out.println(set); } return max; } @@ -60,25 +53,24 @@ private static int lengthOfLongestSubstring(String input) { char[] chars = input.toCharArray(); int max = 0, current = max; - String subString = null; + StringBuilder subString = null; - for (int i = 0; i < chars.length; i++) { - char currentChar = chars[i]; + for (char currentChar : chars) { String currentCharString = String.valueOf(currentChar); if (subString != null) { - int index = subString.lastIndexOf(currentChar); + int index = subString.toString().lastIndexOf(currentChar); if (index >= 0) { // max = current > max ? current : max; // current = current-(index +1); - subString = subString.substring(index + 1) + currentCharString; + subString = new StringBuilder(subString.substring(index + 1) + currentCharString); current = subString.length(); } else { - subString = subString + currentCharString; + subString.append(currentCharString); current++; } } else { - subString = currentCharString; + subString = new StringBuilder(currentCharString); current++; } max = Math.max(current, max); diff --git a/dataStructures/basicsAndMath/ChampagneTower.java b/dataStructures/basicsAndMath/ChampagneTower.java index dcb37931..8a683c4b 100644 --- a/dataStructures/basicsAndMath/ChampagneTower.java +++ b/dataStructures/basicsAndMath/ChampagneTower.java @@ -1,6 +1,6 @@ /** * Created on: Oct 28, 2020 - * Questions: https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3508/ + * Questions: https://leetcode.com/problems/champagne-tower/ */ public class ChampagneTower { @@ -11,7 +11,7 @@ public static void main(String[] args) { public static double champagneTower(int poured, int query_row, int query_glass) { double[][] A = new double[102][102]; - A[0][0] = (double) poured; + A[0][0] = poured; for (int r = 0; r <= query_row; ++r) { for (int c = 0; c <= r; ++c) { double q = (A[r][c] - 1.0) / 2.0;