├── Homework1 ├── ICanGuessTheDataStructure.java ├── SplittingNumbers.java └── War.java └── Homework2 ├── GoogleIsFeelingLucky.java └── PeskyPalindrome.java /Homework1/ICanGuessTheDataStructure.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Created by kdn251 on 2/10/17. 4 | */ 5 | 6 | import java.util.*; 7 | import java.io.*; 8 | 9 | public class Main { 10 | 11 | public static void main(String args[]) throws Exception { 12 | 13 | //initialize data structures 14 | Stack stack = new Stack(); 15 | Queue queue = new LinkedList(); 16 | //initialize max priority queue 17 | PriorityQueue priorityQueue = new PriorityQueue(Collections.reverseOrder()); 18 | 19 | //initialize buffered reader 20 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 21 | String line; 22 | 23 | //iterate through all test cases 24 | while ((line = br.readLine()) != null) { 25 | 26 | //initialize removals for each data structure 27 | int stackRemovals = 0; 28 | int queueRemovals = 0; 29 | int priorityQueueRemovals = 0; 30 | int totalRemovals = 0; 31 | 32 | //get number of test cases 33 | int numberOfCases = Integer.parseInt(line); 34 | 35 | //clear contents of data structures 36 | queue.clear(); 37 | priorityQueue.clear(); 38 | stack.clear(); 39 | 40 | //iterate over all test cases 41 | for (int i = 0; i < numberOfCases; i++) { 42 | 43 | String[] currentLineSplit = br.readLine().split(" "); 44 | int command = Integer.parseInt(currentLineSplit[0]); 45 | int number = Integer.parseInt(currentLineSplit[1]); 46 | 47 | //if command is 1, push number into all data structures 48 | if (command == 1) { 49 | 50 | stack.push(number); 51 | queue.add(number); 52 | priorityQueue.add(number); 53 | 54 | } else { 55 | 56 | //check which data structure to remove from and increment its removal count 57 | if (!stack.isEmpty() && stack.peek() == number && stackRemovals == totalRemovals) { 58 | 59 | stackRemovals++; 60 | stack.pop(); 61 | 62 | } 63 | 64 | if (!queue.isEmpty() && queue.peek() == number && queueRemovals == totalRemovals) { 65 | 66 | queueRemovals++; 67 | queue.remove(); 68 | 69 | } 70 | 71 | if (!priorityQueue.isEmpty() && priorityQueue.peek() == number && priorityQueueRemovals == totalRemovals) { 72 | 73 | priorityQueueRemovals++; 74 | priorityQueue.remove(); 75 | 76 | } 77 | 78 | totalRemovals++; 79 | 80 | 81 | } 82 | 83 | } 84 | 85 | //check all removal counts for each data structure vs. total removal count and print the appropriate data structure 86 | if ((stackRemovals == totalRemovals && queueRemovals == totalRemovals) || (stackRemovals == totalRemovals && stackRemovals == priorityQueueRemovals) || (queueRemovals == totalRemovals && priorityQueueRemovals == totalRemovals)) { 87 | 88 | System.out.println("not sure"); 89 | 90 | } else if (stackRemovals == totalRemovals) { 91 | 92 | System.out.println("stack"); 93 | 94 | } else if (queueRemovals == totalRemovals) { 95 | 96 | System.out.println("queue"); 97 | 98 | } else if (priorityQueueRemovals == totalRemovals) { 99 | 100 | System.out.println("priority queue"); 101 | 102 | } else { 103 | 104 | System.out.println("impossible"); 105 | 106 | } 107 | 108 | } 109 | 110 | 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /Homework1/SplittingNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Created by kdn251 on 2/10/17. 4 | */ 5 | 6 | import java.util.*; 7 | import java.io.*; 8 | 9 | public class Main { 10 | 11 | public static void main(String args[]) throws Exception { 12 | 13 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 14 | String line; 15 | 16 | while((line = br.readLine()) != null) { 17 | 18 | //read number 19 | int number = Integer.parseInt(line); 20 | 21 | //terminate if number is zero 22 | if(number == 0) break; 23 | 24 | //intialize variables 25 | int count = 0; 26 | int a = 0; 27 | int b = 0; 28 | 29 | while(number > 0) { 30 | 31 | 32 | //get lowest set bit 33 | int currentBit = number ^ (number & (number - 1)); 34 | 35 | 36 | //if count is even or a with current bit 37 | if(count % 2 == 0) { 38 | 39 | a |= currentBit; 40 | 41 | } 42 | 43 | //if count is odd or b with current bit 44 | else { 45 | 46 | b |= currentBit; 47 | 48 | } 49 | 50 | //increment count 51 | count++; 52 | 53 | //clear lowest set bit for next iteration 54 | number &= (number - 1); 55 | 56 | } 57 | 58 | //print a and b 59 | System.out.println(a + " " + b); 60 | 61 | } 62 | 63 | 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /Homework1/War.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kdn251/algorithmic-problem-solving/02cad9023c716ec64fe980ef8ff5bc8ca4c5ad62/Homework1/War.java -------------------------------------------------------------------------------- /Homework2/GoogleIsFeelingLucky.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Created by kdn251 on 1/30/17. 4 | */ 5 | 6 | import java.util.*; 7 | 8 | public class Main { 9 | 10 | public static void main(String args[]) { 11 | 12 | HashMap> map = new HashMap>(); 13 | 14 | Scanner sc = new Scanner(System.in); 15 | 16 | int testCases = sc.nextInt(); 17 | int max = Integer.MIN_VALUE; 18 | int caseCount = 1; 19 | 20 | for(int i = 0; i < testCases * 10; i++) { 21 | 22 | String website = sc.next(); 23 | int relevance = sc.nextInt(); 24 | 25 | if(i % 10 == 0 && i != 0) { 26 | 27 | List allCandidates = map.get(max); 28 | System.out.println("Case #" + caseCount + ":"); 29 | caseCount++; 30 | 31 | for(String s : allCandidates) { 32 | 33 | System.out.println(s); 34 | 35 | } 36 | 37 | map = new HashMap>(); 38 | max = Integer.MIN_VALUE; 39 | 40 | } 41 | 42 | if(map.containsKey(relevance)) { 43 | 44 | map.get(relevance).add(website); 45 | 46 | } 47 | 48 | if(!map.containsKey(relevance)) { 49 | 50 | List list = new ArrayList(); 51 | map.put(relevance, list); 52 | map.get(relevance).add(website); 53 | 54 | } 55 | 56 | if(relevance > max) { 57 | 58 | max = relevance; 59 | 60 | } 61 | 62 | } 63 | 64 | System.out.println("Case #" + caseCount + ":"); 65 | 66 | for(String s : map.get(max)) { 67 | 68 | System.out.println(s); 69 | 70 | } 71 | 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /Homework2/PeskyPalindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | 5 | public static void main(String args[]) { 6 | 7 | int x; 8 | 9 | Scanner sc = new Scanner(System.in); 10 | 11 | while(sc.hasNext()) { 12 | 13 | String currentString = sc.next(); 14 | 15 | List allSubstrings = generateSubstrings(currentString); 16 | 17 | int uniquePalindromes = findUniquePalindromes(allSubstrings); 18 | 19 | System.out.println("The string " + "'" + currentString + "'" + " contains " + uniquePalindromes + " palindromes."); 20 | 21 | } 22 | 23 | } 24 | 25 | public static List generateSubstrings(String s) { 26 | 27 | List allSubstrings = new ArrayList(); 28 | 29 | for(int i = 0; i < s.length(); i++) { 30 | 31 | for(int j = i + 1; j <= s.length(); j++) { 32 | 33 | String currentSubstring = s.substring(i, j); 34 | 35 | if(!allSubstrings.contains(currentSubstring)) { 36 | 37 | allSubstrings.add(currentSubstring); 38 | 39 | } 40 | 41 | } 42 | 43 | } 44 | 45 | return allSubstrings; 46 | 47 | } 48 | 49 | public static int findUniquePalindromes(List allSubstrings) { 50 | 51 | int totalUniquePalindromes = 0; 52 | 53 | for(String s : allSubstrings) { 54 | 55 | int left = 0; 56 | int right = s.length() - 1; 57 | 58 | boolean isPalindrome = true; 59 | 60 | while(left < right) { 61 | 62 | if(s.charAt(left) != s.charAt(right)) { 63 | 64 | isPalindrome = false; 65 | break; 66 | 67 | } 68 | 69 | left++; 70 | right--; 71 | 72 | } 73 | 74 | if(isPalindrome) { 75 | 76 | totalUniquePalindromes++; 77 | 78 | } 79 | 80 | } 81 | 82 | return totalUniquePalindromes; 83 | 84 | } 85 | 86 | 87 | 88 | 89 | 90 | } 91 | 92 | --------------------------------------------------------------------------------