Skip to content

Commit

Permalink
Added few more questions.
Browse files Browse the repository at this point in the history
  • Loading branch information
neerazz committed Nov 15, 2022
1 parent 880aa4c commit 926a500
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 20 deletions.
72 changes: 72 additions & 0 deletions Algorithms/greedy-algorithum/TextJustification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> fullJustify_rev4(String[] words, int max) {
int len = words.length;
List<TextRow> 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<String> 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<String> 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<String> fullJustify_rev3(String[] words, int max) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<Integer>> permute(int[] nums) {
Expand Down Expand Up @@ -74,4 +75,28 @@ public static List<List<Integer>> permute_elegant(int[] nums) {
permute(nums, nums.length);
return output;
}

public static List<List<Integer>> permute_elegant_2(int[] nums) {
int len = nums.length;
boolean[] taken = new boolean[len];
List<List<Integer>> output = new ArrayList<>();
backtrack(nums, taken, output, new LinkedList<>());
return output;
}

private static void backtrack(int[] nums, boolean[] taken, List<List<Integer>> output, LinkedList<Integer> 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();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Character> 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;
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions dataStructures/basicsAndMath/ChampagneTower.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 926a500

Please sign in to comment.