├── .gitattributes ├── .gitignore ├── almostIncreasingSequence.java ├── areFollowingPatterns.java ├── climbingStairs.java ├── containsCloseNums.java ├── containsDuplicates.java ├── decodeString.java ├── findProfession.java ├── firstNonRepeatingCharacter.java ├── groupingDishes.java ├── hasPathWithGivenSum.java ├── isCryptSolution.java ├── isListPalindrome.java ├── isLucky.java ├── isSubTree.java ├── isTreeSymmetric.java ├── kthSmallestInBST.java ├── largestValuesInTreeRows.java ├── makeArrayConsecutive2.java ├── mergeTwoLinkedLists.java ├── possibleSums.java ├── removeKFromList.java ├── restoreBinaryTree.java ├── reverseNodesInKGroups.java ├── rotateImage.java ├── shapeArea.java ├── sudoku2.java ├── sumInRange.java ├── sumOfTwo.java ├── traverseTree.java ├── treeBottom.java └── zigzag.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows thumbnail cache files 2 | Thumbs.db 3 | ehthumbs.db 4 | ehthumbs_vista.db 5 | 6 | # Folder config file 7 | Desktop.ini 8 | 9 | # Recycle Bin used on file shares 10 | $RECYCLE.BIN/ 11 | 12 | # Windows Installer files 13 | *.cab 14 | *.msi 15 | *.msm 16 | *.msp 17 | 18 | # Windows shortcuts 19 | *.lnk 20 | 21 | # ========================= 22 | # Operating System Files 23 | # ========================= 24 | -------------------------------------------------------------------------------- /almostIncreasingSequence.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence 3 | by removing no more than one element from the array. 4 | */ 5 | 6 | boolean almostIncreasingSequence(int[] sequence) { 7 | for(int i = sequence.length-1; i >= 0; i--){ 8 | int test = 0; 9 | int numPrev = 0; 10 | int start = 1; 11 | if(i == 0){ 12 | numPrev = sequence[1]; 13 | start = 2; 14 | } 15 | else{ 16 | numPrev = sequence[0]; 17 | } 18 | for(int k = start; k < sequence.length; k++){ 19 | int next = sequence[k]; 20 | if(k != i){ 21 | if(numPrev >= next){ 22 | test++; 23 | } 24 | numPrev = sequence[k]; 25 | } 26 | } 27 | if(test == 0){ 28 | return true; 29 | } 30 | } 31 | return false; 32 | } 33 | -------------------------------------------------------------------------------- /areFollowingPatterns.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array strings, determine whether it follows the sequence given in the patterns array. 3 | In other words, there should be no i and j for which strings[i] = strings[j] 4 | and patterns[i] ? patterns[j] or for which strings[i] ? strings[j] and patterns[i] = patterns[j]. 5 | 6 | Example: 7 | 8 | For strings = ["cat", "dog", "dog"] and patterns = ["a", "b", "b"], the output should be 9 | areFollowingPatterns(strings, patterns) = true; 10 | For strings = ["cat", "dog", "doggy"] and patterns = ["a", "b", "b"], the output should be 11 | areFollowingPatterns(strings, patterns) = false. 12 | */ 13 | 14 | boolean areFollowingPatterns(String[] strings, String[] patterns) { 15 | Hashtable savedInfo = new Hashtable<>(); 16 | if(strings.length != patterns.length){ 17 | return false; 18 | } 19 | for(int i = 0; i < strings.length; i++){ 20 | if(savedInfo.containsKey(patterns[i])){ 21 | if(!savedInfo.get(patterns[i]).equals(strings[i])){ 22 | return false; 23 | } 24 | } 25 | else{ 26 | if(savedInfo.containsValue(strings[i])){ 27 | return false; 28 | } 29 | savedInfo.put(patterns[i], strings[i]); 30 | } 31 | } 32 | return true; 33 | } 34 | -------------------------------------------------------------------------------- /climbingStairs.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/climbing-stairs/description/ 2 | 3 | int[][] climbingStaircase(int n, int k) { 4 | ArrayList> answer = new ArrayList<>(); 5 | makeList(n, k, answer, new ArrayList()); 6 | int[][] returnAnsw = answer.stream().map(u->u.stream().mapToInt(i->i).toArray()).toArray(int[][]::new); 7 | return returnAnsw; 8 | } 9 | 10 | void makeList(int n, int k, ArrayList> answer, ArrayList steps){ 11 | if(n == 0){ 12 | ArrayList tmp = new ArrayList(steps); 13 | answer.add(tmp); 14 | } 15 | else{ 16 | for(int i = 1; i < k+1; i++){ 17 | if(i <= n){ 18 | steps.add(i); 19 | makeList(n-i, k, answer, steps); 20 | steps.remove(steps.size()-1); 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /containsCloseNums.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of integers nums and an integer k, determine whether there are two distinct indices i and j in the 3 | array where nums[i] = nums[j] and the absolute difference between i and j is less than or equal to k. 4 | */ 5 | 6 | boolean containsCloseNums(int[] nums, int k) { 7 | int one = 0; 8 | int two = 0; 9 | int distrance = 0; 10 | for(int i = 0; i < nums.length-1; i++){ 11 | one = nums[i]; 12 | for(int j = i+1; j < nums.length; j++){ 13 | if(nums[j] == one){ 14 | distrance = j-i; 15 | if(distrance <= k){ 16 | return true; 17 | } 18 | } 19 | } 20 | } 21 | return false; 22 | } 23 | -------------------------------------------------------------------------------- /containsDuplicates.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of integers, write a function that determines whether the array contains any duplicates. 3 | Your function should return true if any element appears at least twice in the array, and it should return false if every element is distinct. 4 | */ 5 | 6 | boolean containsDuplicates(int[] a) { 7 | HashSet mySet = new HashSet<>(); 8 | for(int i: a){ 9 | mySet.add(i); 10 | } 11 | return !(mySet.size() == a.length); 12 | } 13 | -------------------------------------------------------------------------------- /decodeString.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given an encoded string, return its corresponding decoded string. 3 | 4 | The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is repeated exactly k times. Note: k is guaranteed to be a positive integer. 5 | 6 | Note that your solution should have linear complexity because this is what you will be asked during an interview. 7 | 8 | For s = "4[ab]", the output should be 9 | decodeString(s) = "abababab"; 10 | 11 | For s = "2[b3[a]]", the output should be 12 | decodeString(s) = "baaabaaa"; 13 | 14 | For s = "z1[y]zzz2[abc]", the output should be 15 | decodeString(s) = "zyzzzabcabc". 16 | */ 17 | 18 | String decodeString(String s) { 19 | String solu = ""; 20 | char [] stringList = s.toCharArray(); 21 | boolean inBrackets = false; 22 | String inBra = ""; 23 | String sNum = ""; 24 | int num = 0; 25 | int numBrack = 0; 26 | for(int i = 0; i < stringList.length; i++){ 27 | if(!Character.isDigit(stringList[i]) && inBrackets == false && stringList[i] != ']' && stringList[i] != '['){ 28 | solu += stringList[i]; 29 | } 30 | if(Character.isDigit(stringList[i]) && inBrackets == false){ 31 | sNum += stringList[i]; 32 | } 33 | if(stringList[i] == ']'){ 34 | numBrack--; 35 | if(numBrack == 0){ 36 | num = Integer.parseInt(String.valueOf(sNum)); 37 | inBrackets = false; 38 | solu += multiplyString(num, inBra); 39 | inBra = ""; 40 | sNum = ""; 41 | } 42 | } 43 | if(inBrackets == true && i == stringList.length-1){ 44 | inBrackets = false; 45 | solu += multiplyString(num, inBra); 46 | inBra = ""; 47 | } 48 | if(inBrackets == true){ 49 | inBra += stringList[i]; 50 | } 51 | if(stringList[i] == '['){ 52 | numBrack++; 53 | inBrackets = true; 54 | } 55 | } 56 | return solu; 57 | } 58 | 59 | String multiplyString(int k, String a){ 60 | String result = ""; 61 | if(a.contains("]") || a.contains("[")){ 62 | a = decodeString(a); 63 | } 64 | for(int i = 0; i < k; i++){ 65 | result += a; 66 | } 67 | return result; 68 | } -------------------------------------------------------------------------------- /findProfession.java: -------------------------------------------------------------------------------- 1 | /* 2 | Consider a special family of Engineers and Doctors. This family has the following rules: 3 | 4 | Everybody has two children. 5 | The first child of an Engineer is an Engineer and the second child is a Doctor. 6 | The first child of a Doctor is a Doctor and the second child is an Engineer. 7 | All generations of Doctors and Engineers start with an Engineer. 8 | We can represent the situation using this diagram: 9 | 10 | E 11 | / \ 12 | E D 13 | / \ / \ 14 | E D D E 15 | / \ / \ / \ / \ 16 | E D D E D E E D 17 | Given the level and position of a person in the ancestor tree above, find the profession of the person. 18 | Note: in this tree first child is considered as left child, second - as right. 19 | */ 20 | 21 | String findProfession(int level, int pos) { 22 | if(level == 1){ 23 | return "Engineer"; 24 | } 25 | if(findProfession(level-1, (pos+1)/2) == "Doctor"){ 26 | if(pos%2 == 0){ 27 | return "Engineer"; 28 | } 29 | else{ 30 | return "Doctor"; 31 | } 32 | } 33 | String answer = "Engineer"; 34 | if(pos%2 == 0){ 35 | answer = "Doctor"; 36 | } 37 | return answer; 38 | } -------------------------------------------------------------------------------- /firstNonRepeatingCharacter.java: -------------------------------------------------------------------------------- 1 | /* 2 | Note: Write a solution that only iterates over the string once and uses O(1) additional memory, since this is what you would 3 | be asked to do during a real interview. 4 | 5 | Given a string s, find and return the first instance of a non-repeating character in it. If there is no such character, return '_'. 6 | */ 7 | 8 | char firstNotRepeatingCharacter(String s) { 9 | for(int i = 0; i < s.length(); i++){ 10 | if(s.indexOf(s.charAt(i)) ==s.lastIndexOf(s.charAt(i)) ){ 11 | return s.charAt(i); 12 | } 13 | } 14 | return '_'; 15 | } 16 | -------------------------------------------------------------------------------- /groupingDishes.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have a list of dishes. Each dish is associated with a list of ingredients used to prepare it. You want to group the dishes by ingredients, so that for each ingredient you'll be able to find all the dishes that contain it (if there are at least 2 such dishes). 3 | 4 | Return an array where each element is a list with the first element equal to the name of the ingredient and all of the other elements equal to the names of dishes that contain this ingredient. The dishes inside each list should be sorted lexicographically. The result array should be sorted lexicographically by the names of the ingredients in its elements. 5 | 6 | Example 7 | 8 | For 9 | dishes = [["Salad", "Tomato", "Cucumber", "Salad", "Sauce"], 10 | ["Pizza", "Tomato", "Sausage", "Sauce", "Dough"], 11 | ["Quesadilla", "Chicken", "Cheese", "Sauce"], 12 | ["Sandwich", "Salad", "Bread", "Tomato", "Cheese"]] 13 | the output should be 14 | groupingDishes(dishes) = [["Cheese", "Quesadilla", "Sandwich"], 15 | ["Salad", "Salad", "Sandwich"], 16 | ["Sauce", "Pizza", "Quesadilla", "Salad"], 17 | ["Tomato", "Pizza", "Salad", "Sandwich"]] 18 | For 19 | dishes = [["Pasta", "Tomato Sauce", "Onions", "Garlic"], 20 | ["Chicken Curry", "Chicken", "Curry Sauce"], 21 | ["Fried Rice", "Rice", "Onions", "Nuts"], 22 | ["Salad", "Spinach", "Nuts"], 23 | ["Sandwich", "Cheese", "Bread"], 24 | ["Quesadilla", "Chicken", "Cheese"]] 25 | the output should be 26 | groupingDishes(dishes) = [["Cheese", "Quesadilla", "Sandwich"], 27 | ["Chicken", "Chicken Curry", "Quesadilla"], 28 | ["Nuts", "Fried Rice", "Salad"], 29 | ["Onions", "Fried Rice", "Pasta"]] 30 | */ 31 | 32 | String[][] groupingDishes(String[][] dishes) { 33 | Hashtable hash1 = new Hashtable<>(); 34 | ArrayList> tmpList = new ArrayList>(); 35 | for(int i = 0; i < dishes.length; i++){ 36 | for(int l = 1; l < dishes[i].length; l++){ 37 | if(hash1.containsKey(dishes[i][l])){ 38 | int num = hash1.get(dishes[i][l]); 39 | num++; 40 | hash1.put(dishes[i][l], num); 41 | } 42 | else{ 43 | int one = 1; 44 | hash1.put(dishes[i][l], one); 45 | } 46 | } 47 | } 48 | Set keys = hash1.keySet(); 49 | int i = 0; 50 | Set sortKey = new TreeSet(keys); 51 | ArrayList tmp = new ArrayList<>(); 52 | for(String key: sortKey){ 53 | ArrayList empty = new ArrayList<>(); 54 | if(hash1.get(key) > 1){ 55 | tmp.add(key); 56 | tmpList.add(empty); 57 | i++; 58 | } 59 | } 60 | for(int m = 0; m < dishes.length; m++){ 61 | for(int l = 1; l < dishes[m].length; l++){ 62 | for(int k = 0; k < tmp.size(); k++){ 63 | if(dishes[m][l].equals(tmp.get(k))){ 64 | tmpList.get(k).add(dishes[m][0]); 65 | } 66 | } 67 | } 68 | } 69 | for(int q = 0; q < tmpList.size(); q++){ 70 | Collections.sort(tmpList.get(q)); 71 | tmpList.get(q).add(0, tmp.get(q)); 72 | } 73 | String[][] solution = new String[tmpList.size()][]; 74 | for (int j = 0; j < tmpList.size(); j++) { 75 | ArrayList row = tmpList.get(j); 76 | solution[j] = row.toArray(new String[row.size()]); 77 | } 78 | return solution; 79 | } 80 | -------------------------------------------------------------------------------- /hasPathWithGivenSum.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree t and an integer s, determine whether there is a root to leaf path in t such that the sum of vertex values equals s. 3 | */ 4 | 5 | // 6 | // Definition for binary tree: 7 | // class Tree { 8 | // Tree(T x) { 9 | // value = x; 10 | // } 11 | // T value; 12 | // Tree left; 13 | // Tree right; 14 | // } 15 | 16 | boolean hasPathWithGivenSum(Tree t, int s) { 17 | if(t == null){ 18 | return false; 19 | } 20 | return traverseTree(t, 0, s); 21 | } 22 | boolean checkB = false; 23 | boolean traverseTree(Tree tree, int sum, int check){ 24 | int tmpSum = sum; 25 | tmpSum += tree.value; 26 | if(checkB == true){ 27 | return true; 28 | } 29 | if(tmpSum == check && tree.left == null && tree.right == null){ 30 | checkB = true; 31 | return true; 32 | } 33 | if(tmpSum == check){ 34 | System.out.println(tree.value); 35 | } 36 | if(tree.left != null){ 37 | checkB = traverseTree(tree.left, tmpSum, check); 38 | } 39 | if(tree.right != null){ 40 | checkB = traverseTree(tree.right, tmpSum, check); 41 | } 42 | return checkB; 43 | } -------------------------------------------------------------------------------- /isCryptSolution.java: -------------------------------------------------------------------------------- 1 | /* 2 | A cryptarithm is a mathematical puzzle for which the goal is to find the correspondence between letters and digits, such that the given arithmetic equation consisting of letters holds true when the letters are converted to digits. 3 | 4 | You have an array of strings crypt, the cryptarithm, and an an array containing the mapping of letters and digits, solution. The array crypt will contain three non-empty strings that follow the structure: [word1, word2, word3], which should be interpreted as the word1 + word2 = word3 cryptarithm. 5 | 6 | If crypt, when it is decoded by replacing all of the letters in the cryptarithm with digits using the mapping in solution, becomes a valid arithmetic equation containing no numbers with leading zeroes, the answer is true. If it does not become a valid arithmetic solution, the answer is false. 7 | 8 | Example 9 | 10 | For crypt = ["SEND", "MORE", "MONEY"] and 11 | 12 | solution = [['O', '0'], 13 | ['M', '1'], 14 | ['Y', '2'], 15 | ['E', '5'], 16 | ['N', '6'], 17 | ['D', '7'], 18 | ['R', '8'], 19 | ['S', '9']] 20 | the output should be 21 | isCryptSolution(crypt, solution) = true. 22 | 23 | When you decrypt "SEND", "MORE", and "MONEY" using the mapping given in crypt, you get 9567 + 1085 = 10652 which is correct and a valid arithmetic equation. 24 | 25 | For crypt = ["TEN", "TWO", "ONE"] and 26 | 27 | solution = [['O', '1'], 28 | ['T', '0'], 29 | ['W', '9'], 30 | ['E', '5'], 31 | ['N', '4']] 32 | the output should be 33 | isCryptSolution(crypt, solution) = false. 34 | 35 | Even though 054 + 091 = 145, 054 and 091 both contain leading zeroes, meaning that this is not a valid solution. 36 | */ 37 | 38 | boolean isCryptSolution(String[] crypt, char[][] solution) { 39 | long first = 0; 40 | long second = 0; 41 | long third = 0; 42 | boolean sol = false; 43 | for(int i = 0; i < crypt.length; i++){ 44 | String tmp = ""; 45 | for(int j = 0; j < crypt[i].length(); j++){ 46 | for(int k = 0; k < solution.length; k++){ 47 | if(crypt[i].charAt(j) == solution[k][0]){ 48 | if(solution[k][1] == '0' && j == 0 && crypt[i].length() > 1){ 49 | return false; 50 | } 51 | tmp += solution[k][1]; 52 | } 53 | } 54 | } 55 | if(i == 0){ 56 | first = Long.parseLong(tmp); 57 | tmp = ""; 58 | } 59 | if(i == 1){ 60 | second = Long.parseLong(tmp); 61 | tmp = ""; 62 | } 63 | if(i == 2){ 64 | third = Long.parseLong(tmp); 65 | tmp = ""; 66 | } 67 | } 68 | if(first + second == third){ 69 | sol = true; 70 | } 71 | return sol; 72 | } 73 | -------------------------------------------------------------------------------- /isListPalindrome.java: -------------------------------------------------------------------------------- 1 | /* 2 | Note: Try to solve this task in O(n) time using O(1) additional space, where n is the number of elements in l, 3 | since this is what you'll be asked to do during an interview. 4 | 5 | Given a singly linked list of integers, determine whether or not it's a palindrome. 6 | */ 7 | 8 | // Definition for singly-linked list: 9 | // class ListNode { 10 | // ListNode(T x) { 11 | // value = x; 12 | // } 13 | // T value; 14 | // ListNode next; 15 | // } 16 | // 17 | boolean isListPalindrome(ListNode l) { 18 | List tmp = new ArrayList(); 19 | while(l != null){ 20 | tmp.add(l.value); 21 | l = l.next; 22 | } 23 | for(int i = 0; i < tmp.size()/2; i++){ 24 | Integer front = new Integer(tmp.get(i)); 25 | Integer back = new Integer(tmp.get(tmp.size()-i-1)); 26 | if(!front.equals(back)){ 27 | return false; 28 | } 29 | } 30 | return true; 31 | } 32 | -------------------------------------------------------------------------------- /isLucky.java: -------------------------------------------------------------------------------- 1 | /* 2 | Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half. 3 | 4 | Given a ticket number n, determine if it's lucky or not. 5 | */ 6 | boolean isLucky(int n) { 7 | String tmp = Integer.toString(n); 8 | String one = tmp.substring(0, tmp.length()/2); 9 | String two = tmp.substring(tmp.length()/2); 10 | int oneT = 0; 11 | int twoT = 0; 12 | for(int i = 0; i { 10 | // Tree(T x) { 11 | // value = x; 12 | // } 13 | // T value; 14 | // Tree left; 15 | // Tree right; 16 | // } 17 | 18 | boolean isSubtree(Tree t1, Tree t2) { 19 | return lookAtTree(t1, t2); 20 | } 21 | 22 | boolean lookAtTree(Tree t1, Tree t2){ 23 | if(t2 == null){ 24 | return true; 25 | } 26 | if(t1 == null){ 27 | return false; 28 | } 29 | if(checkTree(t1, t2)){ 30 | return true; 31 | } 32 | boolean check = (lookAtTree(t1.left, t2) || lookAtTree(t1.right, t2)); 33 | return check; 34 | } 35 | 36 | boolean checkTree(Tree t1, Tree t2){ 37 | if(t1 == null && t2 == null){ 38 | return true; 39 | } 40 | if(t1 == null || t2 == null){ 41 | return false; 42 | } 43 | boolean check = (t1.value.equals(t2.value) && checkTree(t1.left, t2.left) && checkTree(t1.right, t2.right)); 44 | return check; 45 | } -------------------------------------------------------------------------------- /isTreeSymmetric.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a binary tree t, determine whether it is symmetric around its center, i.e. each side mirrors the other. 3 | */ 4 | 5 | // 6 | // Definition for binary tree: 7 | // class Tree { 8 | // Tree(T x) { 9 | // value = x; 10 | // } 11 | // T value; 12 | // Tree left; 13 | // Tree right; 14 | // } 15 | 16 | boolean isTreeSymmetric(Tree t) { 17 | if(t == null){ 18 | return true; 19 | } 20 | if(t.left == null && t.right == null){ 21 | return true; 22 | } 23 | if(t.left != null && t.right == null){ 24 | return false; 25 | } 26 | if(t.left == null && t.right != null){ 27 | return false; 28 | } 29 | return recursiveCheck(t.right, t.left); 30 | } 31 | 32 | boolean recursiveCheck(Tree r, Tree l){ 33 | if(r == null || l == null){ 34 | return r == null && l == null; 35 | } 36 | return r.value.equals(l.value) && recursiveCheck(r.right, l.left) && recursiveCheck(r.left, l.right); 37 | } -------------------------------------------------------------------------------- /kthSmallestInBST.java: -------------------------------------------------------------------------------- 1 | /* 2 | Note: Your solution should have only one BST traversal and O(1) extra space complexity, since this is what you will be asked to accomplish in an interview. 3 | 4 | A tree is considered a binary search tree (BST) if for each of its nodes the following is true: 5 | 6 | The left subtree of a node contains only nodes with keys less than the node's key. 7 | The right subtree of a node contains only nodes with keys greater than the node's key. 8 | Both the left and the right subtrees must also be binary search trees. 9 | Given a binary search tree t, find the kth smallest element in it. 10 | 11 | Note that kth smallest element means kth element in increasing order. See examples for better understanding. 12 | 13 | */ 14 | 15 | // 16 | // Definition for binary tree: 17 | // class Tree { 18 | // Tree(T x) { 19 | // value = x; 20 | // } 21 | // T value; 22 | // Tree left; 23 | // Tree right; 24 | // } 25 | 26 | ArrayList treeList; 27 | int kthSmallestInBST(Tree t, int k) { 28 | treeList = new ArrayList<>(); 29 | makeList(t); 30 | return treeList.get(k-1); 31 | } 32 | 33 | void makeList(Tree t){ 34 | if(t.left != null){ 35 | makeList(t.left); 36 | } 37 | treeList.add(t.value); 38 | if(t.right != null){ 39 | makeList(t.right); 40 | } 41 | } -------------------------------------------------------------------------------- /largestValuesInTreeRows.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/find-largest-value-in-each-tree-row/description/ 2 | 3 | // 4 | // Definition for binary tree: 5 | // class Tree { 6 | // Tree(T x) { 7 | // value = x; 8 | // } 9 | // T value; 10 | // Tree left; 11 | // Tree right; 12 | // } 13 | 14 | List values = new ArrayList<>(); 15 | 16 | int[] largestValuesInTreeRows(Tree t) { 17 | List> firstNode = new ArrayList<>(); 18 | firstNode.add(t); 19 | if(t != null){ 20 | values.add(t.value); 21 | } 22 | getHighest(firstNode); 23 | int[] answer = new int[values.size()]; 24 | for(int i = 0; i < values.size(); i++){ 25 | answer[i] = values.get(i); 26 | } 27 | return answer; 28 | } 29 | 30 | void getHighest(List> nodes){ 31 | List tmpValues = new ArrayList<>(); 32 | List> newNodes = new ArrayList<>(); 33 | for(Tree t : nodes){ 34 | if(t != null){ 35 | if(t.left != null){ 36 | newNodes.add(t.left); 37 | Tree tmp = t.left; 38 | tmpValues.add(tmp.value); 39 | } 40 | if(t.right != null){ 41 | newNodes.add(t.right); 42 | Tree tmp = t.right; 43 | tmpValues.add((tmp.value)); 44 | } 45 | } 46 | } 47 | if(tmpValues.size() == 0){ 48 | return; 49 | } 50 | int highest = -10000; 51 | for(int val : tmpValues){ 52 | if(val > highest){ 53 | highest = val; 54 | } 55 | } 56 | values.add(highest); 57 | getHighest(newNodes); 58 | return; 59 | } -------------------------------------------------------------------------------- /makeArrayConsecutive2.java: -------------------------------------------------------------------------------- 1 | /* 2 | Ratiorg got statues of different sizes as a present from CodeMaster for his birthday, each statue having an non-negative integer size. 3 | Since he likes to make things perfect, he wants to arrange them from smallest to largest so that each statue will be bigger than the 4 | previous one exactly by 1. He may need some additional statues to be able to accomplish that. Help him figure out the minimum number 5 | of additional statues needed. 6 | */ 7 | 8 | int makeArrayConsecutive2(int[] statues) { 9 | int startLen = statues.length; 10 | int smallest = 21; 11 | int highest = 0; 12 | for(int i = 0; i < startLen; i++){ 13 | if(statues[i] > highest){ 14 | highest = statues[i]; 15 | } 16 | if(statues[i] < smallest){ 17 | smallest = statues[i]; 18 | } 19 | } 20 | return (highest - smallest) - (startLen-1); 21 | } 22 | -------------------------------------------------------------------------------- /mergeTwoLinkedLists.java: -------------------------------------------------------------------------------- 1 | /* 2 | Note: Your solution should have O(l1.length + l2.length) time complexity, since this is what you will be asked to accomplish in an interview. 3 | 4 | Given two singly linked lists sorted in non-decreasing order, your task is to merge them. In other words, return a singly linked list, also sorted in non-decreasing order, that contains the elements from both original lists. 5 | 6 | Example 7 | 8 | For l1 = [1, 2, 3] and l2 = [4, 5, 6], the output should be 9 | mergeTwoLinkedLists(l1, l2) = [1, 2, 3, 4, 5, 6]; 10 | For l1 = [1, 1, 2, 4] and l2 = [0, 3, 5], the output should be 11 | mergeTwoLinkedLists(l1, l2) = [0, 1, 1, 2, 3, 4, 5]. 12 | */ 13 | 14 | // Definition for singly-linked list: 15 | // class ListNode { 16 | // ListNode(T x) { 17 | // value = x; 18 | // } 19 | // T value; 20 | // ListNode next; 21 | // } 22 | // 23 | ListNode mergeTwoLinkedLists(ListNode l1, ListNode l2) { 24 | ListNode solu = new ListNode(0); 25 | ListNode head = solu; 26 | if(l1 == null && l2 != null){ 27 | return l2; 28 | } 29 | if(l1 != null && l2 == null){ 30 | return l1; 31 | } 32 | if(l1 == null && l2 == null){ 33 | return l1; 34 | } 35 | while(!(l1 == null && l2 == null)){ 36 | ListNode tmp = new ListNode(0); 37 | if(l1 != null && l2 == null){ 38 | solu.value = l1.value; 39 | if(l1.next != null){ 40 | solu.next = tmp; 41 | solu = solu.next; 42 | } 43 | l1 = l1.next; 44 | } 45 | else if(l1 == null && l2 != null){ 46 | solu.value = l2.value; 47 | if(l2.next != null){ 48 | solu.next = tmp; 49 | solu = solu.next; 50 | } 51 | l2 = l2.next; 52 | } 53 | else if(l1.value <= l2.value){ 54 | solu.value = l1.value; 55 | solu.next = tmp; 56 | solu = solu.next; 57 | l1 = l1.next; 58 | } 59 | else if(l1.value > l2.value){ 60 | solu.value = l2.value; 61 | solu.next = tmp; 62 | solu = solu.next; 63 | l2 = l2.next; 64 | } 65 | } 66 | return head; 67 | } 68 | -------------------------------------------------------------------------------- /possibleSums.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have a collection of coins, and you know the values of the coins and the quantity of each type of coin in it. You want to know how many distinct sums you can make from non-empty groupings of these coins. 3 | 4 | Example 5 | 6 | For coins = [10, 50, 100] and quantity = [1, 2, 1], the output should be 7 | possibleSums(coins, quantity) = 9. 8 | 9 | Here are all the possible sums: 10 | 11 | 50 = 50; 12 | 10 + 50 = 60; 13 | 50 + 100 = 150; 14 | 10 + 50 + 100 = 160; 15 | 50 + 50 = 100; 16 | 10 + 50 + 50 = 110; 17 | 50 + 50 + 100 = 200; 18 | 10 + 50 + 50 + 100 = 210; 19 | 10 = 10; 20 | 100 = 100; 21 | 10 + 100 = 110. 22 | As you can see, there are 9 distinct sums that can be created from non-empty groupings of your coins. 23 | */ 24 | 25 | int possibleSums(int[] coins, int[] quantity) { 26 | Set table = new HashSet<>(); 27 | table.add(0); 28 | for(int i = 0; i < quantity.length; i++){ 29 | List sums = new ArrayList(table); 30 | for(Integer k : sums){ 31 | for(int l = 1; l <= quantity[i]; l++){ 32 | table.add(k + l * coins[i]); 33 | } 34 | } 35 | } 36 | return table.size()-1; 37 | } -------------------------------------------------------------------------------- /removeKFromList.java: -------------------------------------------------------------------------------- 1 | /* 2 | Note: Try to solve this task in O(n) time using O(1) additional space, where n is the number of elements in the list, since this is what you'll be asked to do during an interview. 3 | 4 | Given a singly linked list of integers l and an integer k, remove all elements from list l that have a value equal to k. 5 | 6 | Example 7 | 8 | For l = [3, 1, 2, 3, 4, 5] and k = 3, the output should be 9 | removeKFromList(l, k) = [1, 2, 4, 5]; 10 | For l = [1, 2, 3, 4, 5, 6, 7] and k = 10, the output should be 11 | removeKFromList(l, k) = [1, 2, 3, 4, 5, 6, 7]. 12 | 13 | */ 14 | 15 | // Definition for singly-linked list: 16 | // class ListNode { 17 | // ListNode(T x) { 18 | // value = x; 19 | // } 20 | // T value; 21 | // ListNode next; 22 | // } 23 | // 24 | ListNode removeKFromList(ListNode l, int k) { 25 | ListNode tmp = new ListNode(0); 26 | ListNode prev = null; 27 | //tmp = null; 28 | ListNode head = tmp; 29 | while(l != null){ 30 | if(l.value != k){ 31 | tmp.next = l; 32 | tmp = tmp.next; 33 | tmp.value = l.value; 34 | prev = l; 35 | } 36 | if(l.next != null){ 37 | if(l.next.next == null && l.next.value == k){ 38 | tmp.next = null; 39 | } 40 | } 41 | l = l.next; 42 | } 43 | return head.next; 44 | } 45 | -------------------------------------------------------------------------------- /restoreBinaryTree.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/ 2 | 3 | // 4 | // Definition for binary tree: 5 | // class Tree { 6 | // Tree(T x) { 7 | // value = x; 8 | // } 9 | // T value; 10 | // Tree left; 11 | // Tree right; 12 | // } 13 | 14 | Tree restoreBinaryTree(int[] inorder, int[] preorder) { 15 | return makeTree(inorder, preorder); 16 | } 17 | 18 | Tree makeTree(int[] inorder, int[] preorder){ 19 | if(inorder.length == 0){ 20 | return null; 21 | } 22 | int value = preorder[0]; 23 | Tree node = new Tree(value); 24 | if(inorder.length == 1){ 25 | return node; 26 | } 27 | int sizeLeft = 0; 28 | int sizeRight = 0; 29 | boolean seen = false; 30 | for(int i = 0; i < inorder.length; i++){ 31 | if(inorder[i] == value){ 32 | seen = true; 33 | } 34 | else if(!seen){ 35 | sizeLeft++; 36 | } 37 | else{ 38 | sizeRight++; 39 | } 40 | } 41 | seen = false; 42 | int[] inOrderLeft = new int[sizeLeft]; 43 | int[] inOrderRight = new int[sizeRight]; 44 | int[] preOrderLeft = new int[sizeLeft]; 45 | int[] preOrderRight = new int[sizeRight]; 46 | int tmp = 0; 47 | for(int x = 0; x < inorder.length; x++){ 48 | if(inorder[x] == value){ 49 | seen = true; 50 | tmp = 0; 51 | } 52 | else if(!seen){ 53 | inOrderLeft[tmp] = inorder[x]; 54 | tmp++; 55 | } 56 | else{ 57 | inOrderRight[tmp] = inorder[x]; 58 | tmp++; 59 | } 60 | } 61 | tmp = 0; 62 | for(int j = 1; j < preorder.length; j++){ 63 | if(j < sizeLeft + 1){ 64 | preOrderLeft[tmp] = preorder[j]; 65 | tmp++; 66 | } 67 | if(j == sizeLeft + 1){ 68 | tmp = 0; 69 | } 70 | if(j >= sizeLeft + 1){ 71 | preOrderRight[tmp] = preorder[j]; 72 | tmp++; 73 | } 74 | 75 | } 76 | node.left = makeTree(inOrderLeft, preOrderLeft); 77 | node.right = makeTree(inOrderRight, preOrderRight); 78 | return node; 79 | } -------------------------------------------------------------------------------- /reverseNodesInKGroups.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/reverse-nodes-in-k-group/description/ 2 | 3 | // Definition for singly-linked list: 4 | // class ListNode { 5 | // ListNode(T x) { 6 | // value = x; 7 | // } 8 | // T value; 9 | // ListNode next; 10 | // } 11 | // 12 | 13 | ListNode reverseNodesInKGroups(ListNode l, int k) { 14 | if(l == null){ 15 | return l; 16 | } 17 | ListNode tmp = new ListNode(0); 18 | tmp.next = l; 19 | ListNode prev = tmp; 20 | int i = 0; 21 | ListNode x = l; 22 | while(x != null){ 23 | i++; 24 | if(i%k == 0){ 25 | prev = reverse(prev, x.next); 26 | x = prev.next; 27 | } 28 | else{ 29 | x = x.next; 30 | } 31 | } 32 | return tmp.next; 33 | } 34 | 35 | ListNode reverse(ListNode l, ListNode j){ 36 | ListNode tmp = l.next; 37 | ListNode curr = tmp.next; 38 | while(curr != j){ 39 | tmp.next = curr.next; 40 | curr.next = l.next; 41 | l.next = curr; 42 | curr = tmp.next; 43 | } 44 | 45 | return tmp; 46 | } -------------------------------------------------------------------------------- /rotateImage.java: -------------------------------------------------------------------------------- 1 | /* 2 | Note: Try to solve this task in-place (with O(1) additional memory), since this is what you'll be asked to do during an interview. 3 | 4 | You are given an n x n 2D matrix that represents an image. Rotate the image by 90 degrees (clockwise). 5 | 6 | Example 7 | 8 | For 9 | 10 | a = [[1, 2, 3], 11 | [4, 5, 6], 12 | [7, 8, 9]] 13 | the output should be 14 | 15 | rotateImage(a) = 16 | [[7, 4, 1], 17 | [8, 5, 2], 18 | [9, 6, 3]] 19 | */ 20 | 21 | int[][] rotateImage(int[][] a) { 22 | int[][] tmp = new int[a.length][a[0].length]; 23 | for(int i = 0; i < a.length; i++){ 24 | for(int j = 0; j < a[i].length; j++){ 25 | tmp[i][j] = a[(a[j].length-1)-j][i]; 26 | } 27 | } 28 | return tmp; 29 | } 30 | -------------------------------------------------------------------------------- /shapeArea.java: -------------------------------------------------------------------------------- 1 | /* 2 | Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n. 3 | 4 | A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below. 5 | 6 | 7 | 8 | Example 9 | 10 | For n = 2, the output should be 11 | shapeArea(n) = 5; 12 | For n = 3, the output should be 13 | shapeArea(n) = 13. 14 | */ 15 | 16 | int shapeArea(int n) { 17 | int num = 1; 18 | for(int i = 1; i <= n; i++){ 19 | num = num + (4 * (i-1)); 20 | } 21 | return num; 22 | } 23 | -------------------------------------------------------------------------------- /sudoku2.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntDuPar/Codesignal-Leetcode-Questions/d5b0d3e22273e78da48aaef77198c58ccd4194bf/sudoku2.java -------------------------------------------------------------------------------- /sumInRange.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AntDuPar/Codesignal-Leetcode-Questions/d5b0d3e22273e78da48aaef77198c58ccd4194bf/sumInRange.java -------------------------------------------------------------------------------- /sumOfTwo.java: -------------------------------------------------------------------------------- 1 | /* 2 | You have two integer arrays, a and b, and an integer target value v. Determine whether there is a pair of numbers, where one number is taken from a and the other from b, that can be added together to get a sum of v. Return true if such a pair exists, otherwise return false. 3 | 4 | Example 5 | 6 | For a = [1, 2, 3], b = [10, 20, 30, 40], and v = 42, the output should be 7 | sumOfTwo(a, b, v) = true. 8 | */ 9 | 10 | boolean sumOfTwo(int[] a, int[] b, int v) { 11 | HashSet mySet = new HashSet<>(); 12 | for(int i: a){ 13 | mySet.add(i); 14 | } 15 | for(int i: b){ 16 | if(mySet.contains(v-i)){ 17 | return true; 18 | } 19 | } 20 | return false; 21 | } 22 | -------------------------------------------------------------------------------- /traverseTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | Note: Try to solve this task without using recursion, since this is what you'll be asked to do during an interview. 3 | 4 | Given a binary tree of integers t, return its node values in the following format: 5 | 6 | The first element should be the value of the tree root; 7 | The next elements should be the values of the nodes at height 1 (i.e. the root children), ordered from the leftmost to the rightmost one; 8 | The elements after that should be the values of the nodes at height 2 (i.e. the children of the nodes at height 1) ordered in the same way; 9 | Etc. 10 | 11 | */ 12 | 13 | // 14 | // Definition for binary tree: 15 | // class Tree { 16 | // Tree(T x) { 17 | // value = x; 18 | // } 19 | // T value; 20 | // Tree left; 21 | // Tree right; 22 | // } 23 | 24 | int[] traverseTree(Tree t) { 25 | boolean done = false; 26 | List values = new ArrayList<>(); 27 | List nodes = new ArrayList<>(); 28 | if(t == null){ 29 | return new int[0]; 30 | } 31 | nodes.add(t); 32 | values.add(t.value); 33 | while (!done){ 34 | List nodesTMP = new ArrayList<>(); 35 | for(int i = 0; i < nodes.size(); i++){ 36 | if(nodes.get(i).left != null){ 37 | nodesTMP.add(nodes.get(i).left); 38 | Tree tmp = nodes.get(i).left; 39 | values.add(tmp.value); 40 | } 41 | if(nodes.get(i).right != null){ 42 | nodesTMP.add(nodes.get(i).right); 43 | Tree tmp2 = nodes.get(i).right; 44 | values.add(tmp2.value); 45 | } 46 | } 47 | if(nodesTMP.size() == 0){ 48 | done = true; 49 | } 50 | nodes = nodesTMP; 51 | } 52 | int vals[] = new int[values.size()]; 53 | for(int i = 0; i < values.size(); i++){ 54 | vals[i] = values.get(i); 55 | } 56 | return vals; 57 | } -------------------------------------------------------------------------------- /treeBottom.java: -------------------------------------------------------------------------------- 1 | /* 2 | You are given a recursive notation of a binary tree: each node of a tree is represented as a set of three elements: 3 | 4 | value of the node; 5 | left subtree; 6 | right subtree. 7 | So, a tree can be written as (value left_subtree right_subtree). It is guaranteed that 1 = value = 109. If a node doesn't exist then it is represented as an empty set: (). For example, here is a representation of a tree in the given picture: 8 | 9 | (2 (7 (2 () ()) (6 (5 () ()) (11 () ()))) (5 () (9 (4 () ()) ()))) 10 | 11 | 12 | Your task is to obtain a list of nodes, that are the most distant from the tree root, in the order from left to right. 13 | 14 | In the notation of a node its value and subtrees are separated by exactly one space character. 15 | 16 | Example 17 | 18 | For 19 | 20 | tree = "(2 (7 (2 () ()) (6 (5 () ()) (11 () ()))) (5 () (9 (4 () ()) ())))" 21 | the output should be 22 | treeBottom(tree) = [5, 11, 4]. 23 | */ 24 | 25 | int[] treeBottom(String tree) { 26 | int count = 0; 27 | int highest = 0; 28 | String[] treeList = tree.split(" "); 29 | List nodes = new ArrayList<>(); 30 | for(String c : treeList){ 31 | for(char d : c.toCharArray()){ 32 | if(d == '('){ 33 | count++; 34 | } 35 | if(d == ')'){ 36 | count--; 37 | } 38 | } 39 | String newS = c.replace("(", ""); 40 | newS = newS.replace(")", ""); 41 | newS = newS.replace(" ", ""); 42 | if(newS.length() > 0){ 43 | nodes.add(newS); 44 | nodes.add("" + count); 45 | if(count > highest){ 46 | highest = count; 47 | } 48 | } 49 | } 50 | List sol = new ArrayList<>(); 51 | for(int i = 0; i < nodes.size(); i+=2){ 52 | int countN = Integer.parseInt(nodes.get(i+1)); 53 | if(countN == highest){ 54 | sol.add(Integer.parseInt(nodes.get(i))); 55 | } 56 | } 57 | int[] solution = new int[sol.size()]; 58 | for(int i = 0; i < sol.size(); i++){ 59 | solution[i] = sol.get(i); 60 | } 61 | return solution; 62 | } -------------------------------------------------------------------------------- /zigzag.java: -------------------------------------------------------------------------------- 1 | /* 2 | A sequence of integers is called a zigzag sequence if each of its elements is either strictly less than all its neighbors or strictly greater than all its neighbors. For example, the sequence 4 2 3 1 5 3 is a zigzag, but 7 3 5 5 2 and 3 8 6 4 5 aren't. Sequence of length 1 is also a zigzag. 3 | 4 | For a given array of integers return the length of its longest contiguous sub-array that is a zigzag sequence. 5 | 6 | Example 7 | 8 | For a = [9, 8, 8, 5, 3, 5, 3, 2, 8, 6], the output should be 9 | zigzag(a) = 4. 10 | 11 | The longest zigzag sub-arrays are [5, 3, 5, 3] and [3, 2, 8, 6] and they both have length 4. 12 | 13 | For a = [4, 4], the output should be 14 | zigzag(a) = 1. 15 | 16 | The longest zigzag sub-array is [4] - it has only one element, which is strictly greater than all its neighbors (there are none of them). 17 | 18 | Input/Output 19 | 20 | [execution time limit] 3 seconds (java) 21 | 22 | [input] array.integer a 23 | 24 | Guaranteed constraints: 25 | 2 = a.length = 25, 26 | 0 = a[i] = 100. 27 | 28 | [output] integer 29 | */ 30 | 31 | int zigzag(int[] a) { 32 | int len = 0; 33 | int tmp = 0; 34 | boolean inC = false; 35 | for(int i = 0; i < a.length; i++){ 36 | if(i == 0 || !inC){ 37 | tmp++; 38 | inC = true; 39 | } 40 | if(i > 0 && i < a.length-1){ 41 | if(a[i] > a[i-1] && a[i] > a[i+1]){ 42 | tmp++; 43 | } 44 | else if(a[i] < a[i-1] && a[i] < a[i+1]){ 45 | tmp++; 46 | } 47 | else{ 48 | tmp++; 49 | if(tmp > len){ 50 | len = tmp; 51 | } 52 | tmp = 0; 53 | inC = false; 54 | } 55 | } 56 | } 57 | //edge case 58 | if(a.length == 2){ 59 | len++; 60 | } 61 | return len; 62 | } --------------------------------------------------------------------------------