├── .idea ├── .gitignore ├── misc.xml ├── vcs.xml ├── cs32-interview-prep20.iml └── modules.xml ├── Workshop4_Recursion ├── problem1_sum_of_array_of_numbers.py ├── Warmup_.py ├── Warmup_.cpp ├── problem2_group_integers.py ├── problem3_climbing_stairs.py ├── problem3_climbing_stairs.cpp ├── problem1_sum_of_array_of_numbers.cpp ├── problem4_letter_combinations.py ├── problem2_group_integers.cpp └── problem4_letter_combinations.cpp ├── Workshop2_Linked Lists ├── Warmup_.py ├── Problem4_Linked List Cycle.cpp ├── Warmup_.cpp ├── Problem3_Merge_Two_Sorted_Lists.py ├── Problem2_Reverse Linked List.cpp ├── Problem1_Middle_of_Linked_Lists.py ├── Problem2_Reverse_Linked_Lists.py ├── Problem3_Merge Two Sorted Lists.cpp ├── Problem4_LinkedListCycle.py ├── Problem_1 Middle of the Linked List.cpp └── BonusProblem_Intersection of Two Linked Lists.cpp ├── Workshop1_Introduction ├── Extra1_To Lower Case_709.py ├── Problem1_Power of Two_231.cpp ├── Problem1_Power of Two_231.py ├── Extra1_To Lower Case_709.cpp ├── Problem2_Remove Vowels from a String_1119.cpp ├── Problem2_Remove Vowels from a String_1119.py ├── Problem3_Robot Return to Origin_657.py ├── Problem4_Move Zeroes_283.cpp ├── Problem3_Robot Return to Origin_657.cpp ├── Warmup_Fizz Buzz_412.py └── Warmup_Fizz Buzz_412.cpp ├── Workshop5_BigO ├── Problem3_Add_Digits.py ├── Problem2_Missing_Number.py ├── Problem4_Find_Peak_Element.py ├── Problem1_Valid_Anagram.py ├── Problem2_Missing_Number.cpp ├── Problem3_Add_Digits.cpp ├── Problem1_Valid_Anagram.cpp └── Problem4_Find_Peak_Element.cpp ├── Workshop3_Stacks + Queues ├── Problem4_Remove Outermost Parentheses_1021.py ├── Problem2_Implement Stack Using Queues_255.py ├── Problem3_Evaluate Postfix Expressions.py ├── Problem1_Backspace String Compare_844.py ├── Warmup_Valid Parentheses_20.cpp ├── Problem1_Backspace String Compare_844.cpp ├── Warmup_Valid Parentheses_20.py ├── Problem2_Implement Stack Using Queues_255.cpp ├── Problem4_Remove Outermost Parentheses_1021.cpp └── Problem3_Evaluate Postfix Expressions.cpp ├── Workshop6_Trees + Hash Tables ├── Problem3_Two Sum_1.py ├── Problem4_Intersection of Two Arrays_349.py ├── Problem2_Path_Sum_112.py ├── Problem2_Path Sum_112.cpp ├── Problem1_Sum of Left Leaves_404.cpp ├── Problem1_Sum_of_Left_Leaves_404.py ├── Problem4_Intersection of Two Arrays_349.cpp └── Problem3_Two Sum_1.cpp └── README.md /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml 3 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/cs32-interview-prep20.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Workshop4_Recursion/problem1_sum_of_array_of_numbers.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Sum of Array of Numbers 9 | """ 10 | 11 | # Given a vector of integers, use recursion to return the sum of 12 | # the integers in the vector. 13 | 14 | class Solution: 15 | def sumOfArray(self, arr) -> int: 16 | if len(arr) == 0: #base case 17 | return 0 18 | else: 19 | return arr[0] + self.sumOfArray(arr[1:]) #recursive case. Add first number and pass on the rest to the next recursive call. -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Warmup_.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def deleteNode(self, node): 9 | """ 10 | :type node: ListNode 11 | :rtype: void Do not return anything, modify node in-place instead. 12 | """ 13 | #we make a check if the list has less than 2 nodes. 14 | if node == None or node.next == None: 15 | return 16 | 17 | """ 18 | we set the current node's next's value. 19 | Then we make the current node point 20 | to the node's next's next node. 21 | """ 22 | node.val = node.next.val 23 | node.next = node.next.next 24 | 25 | return -------------------------------------------------------------------------------- /Workshop1_Introduction/Extra1_To Lower Case_709.py: -------------------------------------------------------------------------------- 1 | # UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 2 | 3 | # Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | # to be optimal in terms of runtime or memory usage, we are merely giving example 5 | # solutions to questions for learning purposes only 6 | 7 | # To Lower Case 8 | # Leetcode Problem 709 9 | # https://leetcode.com/problems/to-lower-case/ 10 | 11 | class Solution(object): 12 | def toLowerCase(self, str): 13 | """ 14 | :type str: str 15 | :rtype: str 16 | """ 17 | 18 | # Warning: You are about to enter a Python magic zone 19 | 20 | # Python has a built-in function that automatically 21 | # changes all the characters in a string to lowercase 22 | 23 | 24 | # Cue Python magic 25 | return str.lower() -------------------------------------------------------------------------------- /Workshop5_BigO/Problem3_Add_Digits.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Add Digits 9 | Leetcode Problem 258 10 | https://leetcode.com/problems/add-digits/ 11 | 12 | """ 13 | 14 | # Recursive Solution 15 | class Solution(object): 16 | def addDigits(self, num): 17 | """ 18 | :type num: int 19 | :rtype: int 20 | """ 21 | 22 | if num % 10 == num: return num 23 | 24 | numSum = 0 25 | while num % 10 != num: 26 | numSum += num % 10 27 | num = num / 10 28 | numSum += num 29 | 30 | return self.addDigits(numSum) 31 | -------------------------------------------------------------------------------- /Workshop4_Recursion/Warmup_.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Factorial 9 | """ 10 | 11 | # Given a non-negative integer n, return n! 12 | # Note: n! = n * (n - 1) * (n - 2) * ... * 2 * 1; 13 | # A special case if that 0! = 1 14 | 15 | class Solution: 16 | def factorial(self, n): 17 | if n ==0 or n == 1: #base case 18 | return 1 19 | else: 20 | # The factorial has a recursive definition. 21 | # Note that (n - 1)! = (n - 1) * (n - 2) * ... * 2 * 1. 22 | # We can then say that n! = n * (n - 1) * (n - 2) * ... * 2 * 1 23 | # = n * (n - 1)! 24 | return n * self.factorial(n-1) #recursive case/ -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Problem4_Linked List Cycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | 10 | class Solution { 11 | public: 12 | bool hasCycle(ListNode *head) { 13 | 14 | ListNode* ptr1 = head; //want both ptrs to start at the same place 15 | ListNode* ptr2 = head; 16 | 17 | while(ptr2 != NULL && ptr2->next != NULL){ //this is the tortoise and hare algorithm 18 | ptr1 = ptr1->next; 19 | ptr2 = (ptr2->next)->next; //ptr2 is always ahead of ptr1, and if ptr2 ever 20 | if(ptr1 == ptr2) //equals ptr1, then there is a cycle 21 | { 22 | return true; 23 | } 24 | } 25 | return false; //if there is no cycle, ptr2 exits the while loop and returns false, 26 | //no cycle in this linked list 27 | 28 | } 29 | }; -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Warmup_.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | //delete a node except for the tail, you can only start out 13 | //with the node youre given, 14 | //meaning you dont know/care for what is before it, cant use the traditional method unfortunately of a dummy node. 15 | //brute method is just to write over the value. 16 | if (node->next == NULL || node == NULL) { //check to see if its the tail just as a precaution and checks if the next item is the tail. 17 | return; 18 | } 19 | node->val = node->next->val; //set the value of the node to the next node 20 | node->next = node->next->next; //set the value of the node after to its suceeding node value. 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Problem3_Merge_Two_Sorted_Lists.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: 9 | dummy = ListNode(0) #create a dummy node to point to the head of the list. 10 | cur = dummy 11 | while l1 != None and l2 != None: #while we have not run to the end of either list. 12 | if l1.val < l2.val: #l1's node comes before l2's node 13 | cur.next = l1 14 | l1 = l1.next 15 | else: #l2's node comes before l1's node 16 | cur.next = l2 17 | l2 = l2.next 18 | cur = cur.next 19 | cur.next = l1 or l2 # the list that is not yet reached the end is appended to the end. 20 | 21 | return dummy.next # we return dummy.next because it is the first node in the combined list. -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Problem2_Reverse Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* reverseList(ListNode* head) { 12 | //since this is singly linked, only the next node is provided 13 | if (head == NULL) //check if the list is empty 14 | { 15 | return NULL; 16 | } 17 | ListNode* newHead = NULL; 18 | ListNode* curr = head; 19 | while (curr != NULL){ //while traversing the list 20 | ListNode* temp = curr->next; //we need a temporary node to store the next 21 | //as a singly linked list, we dont know the prev after saving to new 22 | curr->next = newHead; //the new current will now point to its "prev" 23 | newHead = curr; 24 | curr = temp; //move on to the next node 25 | } 26 | return newHead; //return new "head", last item head 27 | } 28 | }; -------------------------------------------------------------------------------- /Workshop4_Recursion/Warmup_.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Factorial 9 | */ 10 | 11 | // Given a non-negative integer n, return n! 12 | // Note: n! = n * (n - 1) * (n - 2) * ... * 2 * 1; 13 | // A special case if that 0! = 1 14 | class Solution { 15 | public: 16 | // We will use simple recursion to solve this problem. 17 | int factorial(int n) { 18 | // If n == 0 or n == 1, trivially, we return 1 19 | if(n == 0 || n == 1) { 20 | return 1; 21 | } 22 | 23 | // The factorial has a recursive definition. 24 | // Note that (n - 1)! = (n - 1) * (n - 2) * ... * 2 * 1. 25 | // We can then say that n! = n * (n - 1) * (n - 2) * ... * 2 * 1 26 | // = n * (n - 1)! 27 | else { 28 | return n * factorial(n - 1); 29 | } 30 | } 31 | }; -------------------------------------------------------------------------------- /Workshop1_Introduction/Problem1_Power of Two_231.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Power of Two 9 | Leetcode Problem 231 10 | https://leetcode.com/problems/power-of-two/ 11 | */ 12 | 13 | class Solution { 14 | public: 15 | bool isPowerOfTwo(int n) { 16 | 17 | // 0 is not a power of two and is an edge case that would cause 18 | // an infinite loop in our while loop below, so we handle it here 19 | if(n <= 0) 20 | return false; 21 | 22 | // While n is divisible by 2, divide n by 2 23 | while(n % 2 == 0) 24 | n /= 2; 25 | 26 | // If n is a power of 2, at one point the loop above will get to 27 | // a point where n is 2, and 2 / 2 = 1, and 1 % 2 != 0, so 28 | // we return true if n == 1 and false if n != 1 29 | return n == 1; 30 | } 31 | }; -------------------------------------------------------------------------------- /Workshop1_Introduction/Problem1_Power of Two_231.py: -------------------------------------------------------------------------------- 1 | # UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 2 | 3 | # Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | # to be optimal in terms of runtime or memory usage, we are merely giving example 5 | # solutions to questions for learning purposes only 6 | 7 | # Power of Two 8 | # Leetcode Problem 231 9 | # https://leetcode.com/problems/power-of-two/ 10 | 11 | class Solution(object): 12 | def isPowerOfTwo(self, n): 13 | """ 14 | :type n: int 15 | :rtype: bool 16 | """ 17 | 18 | # 0 is not a power of two and is an edge case that would cause an 19 | # infinite loop in our while loop below, so we handle it here 20 | if n == 0: return False 21 | 22 | # while n is divisible by 2, divide n by 2 23 | while n % 2 == 0: 24 | n /= 2 25 | 26 | # if n is a power of 2, at one point the loop above will have gotten 27 | # to a point where n is 2, and 2 / 2 = 1 and 1 % 2 != 0, so 28 | # return true if n == 1 and false if n != 1 29 | return n == 1 30 | -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Problem1_Middle_of_Linked_Lists.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def middleNode(self, head: ListNode) -> ListNode: 9 | #if the linked list is empty then return False because there is no cycle. 10 | if head == None: 11 | return None 12 | 13 | """We define two pointers. One that moves faster than the other.""" 14 | ptr1 = head 15 | ptr2 = head 16 | 17 | """One pointer moves from current node to next node. The other moves from 18 | current node to the next node's next node. 19 | """ 20 | 21 | #make sure neither run to the end. 22 | while ptr1 != None and ptr2 != None: 23 | ptr1 = ptr1.next 24 | if ptr1 != None: 25 | ptr1 = ptr1.next 26 | else: #ptr1 has reached the end of the list which means ptr2 is in the middle. 27 | break 28 | ptr2 = ptr2.next 29 | 30 | return ptr2 -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Problem2_Reverse_Linked_Lists.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def reverseList(self, head: ListNode) -> ListNode: 9 | 10 | #if the list has no nodes or one node it is already reversed 11 | if head == None or head.next == None: 12 | return head 13 | 14 | #we keep track of two nodes one that points to a given node while the other node 15 | prev = None 16 | curr = head 17 | 18 | #iterate through till the end of the list 19 | while curr != None: 20 | temp = curr.next #keep track of the current's node next node. 21 | curr.next = prev #set the current node's next to the prev node 22 | prev = curr #update the prev node to curr because it will be a "previous" node in the next iteration. 23 | curr = temp #curr is set to the next node because it will be a "current" node in the next iteration. 24 | 25 | #prev will be the new head. 26 | return prev -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Problem4_Remove Outermost Parentheses_1021.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Remove Outermost Parentheses 9 | Leetcode Problem 1021 10 | https://leetcode.com/problems/remove-outermost-parentheses/ 11 | 12 | """ 13 | 14 | class Solution(object): 15 | def removeOuterParentheses(self, S): 16 | """ 17 | :type S: str 18 | :rtype: str 19 | """ 20 | result = "" 21 | st = [] 22 | 23 | for curChar in S: 24 | if (len(st) == 0): 25 | st.append('(') 26 | elif (len(st) == 1 and curChar == ')'): 27 | st.pop() 28 | elif (curChar == '('): 29 | st.append('(') 30 | result += curChar 31 | else: 32 | st.pop() 33 | result += curChar 34 | 35 | return result 36 | -------------------------------------------------------------------------------- /Workshop1_Introduction/Extra1_To Lower Case_709.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | To Lower Case 9 | Leetcode Problem 709 10 | https://leetcode.com/problems/to-lower-case/ 11 | 12 | */ 13 | 14 | class Solution { 15 | public: 16 | // Since the problem doesn't give any restrictions 17 | // on functions we can use, we can use the tolower 18 | // function to simplify the problem 19 | string toLowerCase(string str) { 20 | // Our strategy is to construct a string that is identical 21 | // to s, but every character is lowercase 22 | string result = ""; 23 | 24 | for(int i = 0; i < str.size(); i++) 25 | // Add the lowercase character to the result 26 | result += tolower(str[i]); 27 | // Note: To use tolower, you need to include the 28 | // cctype library, which Leetcode already does for you 29 | 30 | return result; 31 | } 32 | }; -------------------------------------------------------------------------------- /Workshop6_Trees + Hash Tables/Problem3_Two Sum_1.py: -------------------------------------------------------------------------------- 1 | 2 | # UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | # Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | # to be optimal in terms of runtime or memory usage, we are merely giving example 6 | # solutions to questions for learning purposes only 7 | 8 | # Two Sum 9 | # Leetcode Problem 1 10 | # https://leetcode.com/problems/two-sum/ 11 | 12 | 13 | 14 | class Solution: 15 | def twoSum(self, nums: List[int], target: int) -> List[int]: 16 | #we are implementing a hash map using a dictionary, 17 | #we need to check if target - indice already exists. 18 | hashmap = {} #initialize hashmap 19 | for i, value in enumerate(nums): #enumerate makes a tuple that keeps track of the indice and the value of the list indice. 20 | temp = target - value #check the difference 21 | if temp not in hashmap: #if the difference is not in the hashmap, hash the current indice in 22 | hashmap[value] = i 23 | else: #we found our match! 24 | return [hashmap[temp],i] 25 | #return a list of two indices. 26 | 27 | -------------------------------------------------------------------------------- /Workshop6_Trees + Hash Tables/Problem4_Intersection of Two Arrays_349.py: -------------------------------------------------------------------------------- 1 | # UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 2 | 3 | # Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | # to be optimal in terms of runtime or memory usage, we are merely giving example 5 | # solutions to questions for learning purposes only 6 | 7 | # Intersection of Two Arrays 8 | # Leetcode Problem 349 9 | # https://leetcode.com/problems/intersection-of-two-arrays/ 10 | 11 | class Solution: 12 | def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: 13 | outputlist = [] #this is the return list that we are returning 14 | hashmap = {} #hash map for keeping track of nums1 15 | for i,val in enumerate(nums1): #add everyvalue into hashmap, non duplicates 16 | # because we dont care about the indices. 17 | if val not in hashmap: 18 | hashmap[val] = i 19 | for i,val in enumerate(nums2): #check values in nums2 20 | if val in hashmap and val not in outputlist: #if the value is in hashmap already 21 | #and our value is not already in output, then append 22 | outputlist.append(val) 23 | 24 | return outputlist #return outputlist -------------------------------------------------------------------------------- /Workshop1_Introduction/Problem2_Remove Vowels from a String_1119.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Remove Vowels from a String 9 | Leetcode Problem 1119 10 | https://leetcode.com/problems/remove-vowels-from-a-string/ 11 | */ 12 | 13 | class Solution { 14 | public: 15 | string removeVowels(string s) { 16 | // Our strategy is to construct a string that is identical 17 | // to s, without including the vowels 18 | string result = ""; 19 | 20 | for(int i = 0; i < s.size(); i++) { 21 | 22 | // Add the current character only if 23 | // it is not a vowel 24 | char cur = s[i]; 25 | if(!isVowel(cur)) 26 | result += s[i]; 27 | } 28 | 29 | return result; 30 | } 31 | private: 32 | // Checks if the value of c is a vowel 33 | bool isVowel(char c) { 34 | return c == 'a' || 35 | c == 'e' || 36 | c == 'i' || 37 | c == 'o' || 38 | c == 'u'; 39 | } 40 | }; 41 | -------------------------------------------------------------------------------- /Workshop5_BigO/Problem2_Missing_Number.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Backspace Valid Anagram 242 9 | Leetcode Problem 10 | https://leetcode.com/problems/backspace-string-compare/ 11 | 12 | """ 13 | 14 | """ 15 | Time Complexity: O(N) 16 | Space Complexity: O(1) 17 | """ 18 | class Solution: 19 | def missingNumber(self, nums: List[int]) -> int: 20 | n = len(nums) 21 | sum = 0 22 | for val in nums: 23 | sum += val 24 | 25 | real_sum = n * (n+1) / 2 26 | missing_num = real_sum - sum 27 | 28 | return missing_num 29 | 30 | 31 | """ 32 | Time Complexity: O(N) 33 | Space Complexity: O(N) 34 | """ 35 | class Solution: 36 | def missingNumber(self, nums: List[int]) -> int: 37 | numbers = {} #find all the numbers and store them in numbers 38 | for num in nums: 39 | numbers[num] = True 40 | 41 | for num in range(1, len(nums) + 1): #find the number from 1 to n which is not in numbers. 42 | if num not in numbers: 43 | return num -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Problem3_Merge Two Sorted Lists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 12 | if (l1 == NULL && l2 == NULL) 13 | { 14 | return NULL; 15 | } 16 | ListNode res(0); //dummy head 17 | ListNode* newList = &res; //newList will have a dummy head 18 | while(l1 != NULL && l2 != NULL){ 19 | if(l1->val <= l2->val) 20 | { 21 | newList->next = l1; //append the according values to the next part of the list 22 | l1 = l1->next; //iterate 23 | } 24 | else 25 | { 26 | newList->next = l2; 27 | l2 = l2->next; //iterate 28 | } 29 | newList = newList->next; //go to the end of the newList 30 | } 31 | //if we break out the while loop,either theres more l1 left, l2 left or none, plan accordingly. 32 | if (l1) 33 | { 34 | newList->next = l1; 35 | } 36 | else if (l2) 37 | { 38 | newList->next = l2; 39 | } 40 | return res.next; //return everything after dummy head 41 | } 42 | }; -------------------------------------------------------------------------------- /Workshop1_Introduction/Problem2_Remove Vowels from a String_1119.py: -------------------------------------------------------------------------------- 1 | # UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 2 | 3 | # Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | # to be optimal in terms of runtime or memory usage, we are merely giving example 5 | # solutions to questions for learning purposes only 6 | 7 | # Remove Vowels from a String 8 | # Leetcode Problem 1119 9 | # https://leetcode.com/problems/remove-vowels-from-a-string/ 10 | 11 | class Solution(object): 12 | def removeVowels(self, S): 13 | """ 14 | :type S: str 15 | :rtype: str 16 | """ 17 | # Warning: You are about to enter a Python magic zone 18 | 19 | # Python has a translate function that translates the ASCII value 20 | # of characters to another character when "translate" is called 21 | # See Example 2 of this tutorial: https://www.programiz.com/python-programming/methods/string/translate 22 | 23 | # In this translation map, we are mapping the ASCII values to None 24 | # so that translate will translate these characters to no character 25 | translation = { 26 | ord('a'): None, 27 | ord('e'): None, 28 | ord('i'): None, 29 | ord('o'): None, 30 | ord('u'): None 31 | } 32 | 33 | # Cue the Python magic 34 | return S.translate(translation) -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Problem4_LinkedListCycle.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode(object): 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution(object): 8 | def hasCycle(self, head): 9 | """ 10 | :type head: ListNode 11 | :rtype: bool 12 | """ 13 | #define our result variable 14 | result = False 15 | 16 | 17 | #if the linked list is empty then return False because there is no cycle. 18 | if head == None: 19 | return result 20 | 21 | """We define two pointers.""" 22 | ptr1 = head 23 | ptr2 = head 24 | 25 | """One pointer moves from current node to next node. The other moves from 26 | current node to the next node's next node. 27 | """ 28 | 29 | ptr1 = ptr1.next 30 | if ptr1 != None: 31 | ptr1 = ptr1.next 32 | ptr2 = ptr2.next 33 | 34 | #make sure neither run to the end. 35 | while ptr1 != None and ptr2 != None: 36 | if ptr1 == ptr2: #we have a cycle. 37 | result = True 38 | break 39 | else: 40 | ptr1 = ptr1.next #we have not yet discovered the linked list. 41 | if ptr1 != None: 42 | ptr1 = ptr1.next 43 | ptr2 = ptr2.next 44 | 45 | return result -------------------------------------------------------------------------------- /Workshop4_Recursion/problem2_group_integers.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Group Integers 9 | """ 10 | 11 | # Given a list of integers, return whether is it possible to 12 | # choose a group of some of the integers such that the group sums 13 | # to the given target 14 | 15 | class Solution: 16 | def groupIntegers(self, nums, target) -> bool: 17 | if len(nums) < 0: #just a random sanity check 18 | return False 19 | if len(nums) <= 0 and target != 0: #base case 1: we have run out of numbers to pick but we have not reached 0 20 | return False 21 | elif target == 0: #base case 2: we may or may not have run out of numbers to pick from but we have reached our target and found our group. 22 | return True 23 | else: 24 | #we could choose to pick the first number to be in our group. 25 | remaining_target = target - nums[0] 26 | remaining_list = nums[1:] 27 | #we could also not choose to pick the first number of the list in which case target will remain the same but we discard the first element. 28 | return self.groupIntegers(remaining_list, remaining_target) or self.groupIntegers(remaining_list, target) 29 | -------------------------------------------------------------------------------- /Workshop2_Linked Lists/Problem_1 Middle of the Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* middleNode(ListNode* head) { 12 | if (head == NULL){ //check if there is a head for the linked list 13 | return NULL; 14 | } 15 | 16 | ListNode* current = head; //set to check the length of the linked list 17 | int length = 1; 18 | 19 | while(current->next != NULL){ 20 | length++; //increase the length for every successive linked list node 21 | current = current->next; 22 | } 23 | /* 24 | if node % 2 == 0 that means theres an even number of nodes, but since we want the second middle, 25 | then we can just add one from the truncated int(since if you divide an int and it results a float, 26 | the compiler will truncate the number) 27 | 28 | if node % s != 0 that means theres an odd number of nodes, which means we still want to add one to 29 | the middle/ 30 | */ 31 | int middle = length / 2; 32 | middle++; 33 | ListNode* traverse = head; 34 | 35 | while(node-- > 1){ //traverse until the middle node 36 | traverse = traverse->next; 37 | } 38 | 39 | return traverse; //return the middle node 40 | } 41 | }; -------------------------------------------------------------------------------- /Workshop5_BigO/Problem4_Find_Peak_Element.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Find Peak Element 9 | Leetcode Problem 162 10 | https://leetcode.com/problems/find-peak-element/ 11 | 12 | """ 13 | 14 | """ 15 | Time Complexity: O(NlogN) 16 | Space Complexity: O(1) 17 | """ 18 | class Solution: 19 | def findPeakElement(self,nums): 20 | 21 | # Python uses a version of MergeSort to sort, so the sort has O(NlogN) 22 | # this finds the value of the peak value as the end of the sorted array 23 | peakVal = sorted(nums)[len(nums) - 1] 24 | 25 | # return the index of this maximum value 26 | for i in range(len(nums)): 27 | if nums[i] == peakVal: 28 | return i 29 | 30 | """ 31 | Time Complexity: O(N) 32 | Space Complexity: O(1) 33 | """ 34 | class Solution: 35 | def findPeakElement(self,nums): 36 | peak = -sys.maxsize - 1 # minimum possible integer 37 | 38 | # find the index of the maximum value in the array in O(N) 39 | for i in range(len(nums)): 40 | if nums[i] > peak: 41 | peak = nums[i] 42 | index = i 43 | 44 | # return the location of this index 45 | return index 46 | 47 | -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Problem2_Implement Stack Using Queues_255.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Implement Stack using Queues 9 | Leetcode Problem 225 10 | https://leetcode.com/problems/implement-stack-using-queues/ 11 | """ 12 | 13 | class MyStack(object): 14 | 15 | def __init__(self): 16 | """ 17 | Initialize your data structure here. 18 | """ 19 | self.q = [] 20 | 21 | 22 | def push(self, x): 23 | """ 24 | Push element x onto stack. 25 | :type x: int 26 | :rtype: None 27 | """ 28 | self.q.append(x) 29 | 30 | def pop(self): 31 | """ 32 | Removes the element on top of the stack and returns that element. 33 | :rtype: int 34 | """ 35 | top = self.q[len(self.q) - 1] 36 | self.q = self.q[:-1] 37 | return top 38 | 39 | def top(self): 40 | """ 41 | Get the top element. 42 | :rtype: int 43 | """ 44 | return self.q[len(self.q) - 1] 45 | 46 | 47 | def empty(self): 48 | """ 49 | Returns whether the stack is empty. 50 | :rtype: bool 51 | """ 52 | return len(self.q) == 0 53 | 54 | 55 | # Your MyStack object will be instantiated and called as such: 56 | # obj = MyStack() 57 | # obj.push(x) 58 | # param_2 = obj.pop() 59 | # param_3 = obj.top() 60 | # param_4 = obj.empty() 61 | -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Problem3_Evaluate Postfix Expressions.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Evaluate Postfix Expressions 9 | Geeks4Geeks: https://www.geeksforgeeks.org/stack-set-4-evaluation-postfix-expression/ 10 | Solution also based off of Geeks4Geeks Solution 11 | 12 | """ 13 | 14 | # The main function that converts given infix expression 15 | # to postfix expression 16 | def evaluatePostfix(exp): 17 | 18 | # Initialize the empty stack 19 | numStack = [] 20 | 21 | # Iterate over the expression for conversion 22 | for i in exp: 23 | 24 | # If the scanned character is an operand 25 | # (number here) push it to the stack 26 | if i.isdigit(): 27 | numStack.append(int(i)) 28 | 29 | # If the scanned character is an operator, 30 | # pop two elements from stack and apply it. 31 | else: 32 | val1 = numStack.pop() 33 | val2 = numStack.pop() 34 | if i == '+': numStack.append(val2 + val1) 35 | elif i == '-': numStack.append(val2 - val1) 36 | elif i == '*': numStack.append(val2 * val1) 37 | elif i == '/': numStack.append(val2 / val1) 38 | 39 | # These evaluations can also be handled using 40 | # the Python eval function 41 | # numStack.append(str(eval(val2 + i + val1))) 42 | 43 | return numStack.pop() -------------------------------------------------------------------------------- /Workshop4_Recursion/problem3_climbing_stairs.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Climbing Stairs 9 | Leetcode Problem 70 10 | https://leetcode.com/problems/climbing-stairs/ 11 | """ 12 | 13 | # Suppose you are climbing a set of stairs that has n steps. 14 | # Every step you take, you can climb up either one step or two steps. 15 | # Return the number of distinct ways you can climb up the stairs. 16 | 17 | class Solution: 18 | def climbStairs(self, int n) -> int: 19 | if(n < 0): 20 | # bad input case 21 | return 0 22 | elif(n == 1): #base case 23 | # If there is only one step, trivially, we can only climb up 24 | # the step by taking one step. 25 | return 1 26 | elif(n == 2): #base case 27 | # If there are only two steps, trivially, we can only climb up 28 | # the step by taking one step + one step, or by taking 2 steps. 29 | return 2 30 | else: 31 | # Now suppose we have a set of N steps. We have two choices: 32 | # Climb 1 step, or climb 2 steps. If we climb 1, then we 33 | # still have N-1 steps to go. If we climb 2, then we still 34 | # have N-2 steps to go. If we assume we already know how to 35 | # calculate climbStairs(N-1) and climbStairs(N-2), then we 36 | # are done. 37 | return self.climbStairs(n-1) + self.climbStairs(n-2) -------------------------------------------------------------------------------- /Workshop1_Introduction/Problem3_Robot Return to Origin_657.py: -------------------------------------------------------------------------------- 1 | # UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 2 | 3 | # Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | # to be optimal in terms of runtime or memory usage, we are merely giving example 5 | # solutions to questions for learning purposes only 6 | 7 | # Remove Robot Return to Origin 8 | # Leetcode Problem 657 9 | # https://leetcode.com/problems/robot-return-to-origin/ 10 | 11 | class Solution(object): 12 | def judgeCircle(self, moves): 13 | """ 14 | :type moves: str 15 | :rtype: bool 16 | """ 17 | 18 | # our strategy is to perform the moves on x and y coordinates 19 | # then check at the end if these x and y coordinates went back 20 | # to their starting poitn of (0,0) 21 | 22 | x = 0 23 | y = 0 24 | 25 | # traverse through every character (current move) in moves 26 | for curMove in moves: 27 | 28 | # left moves are indicated by a decrease in the x-coordinate 29 | if curMove == "L": 30 | x -= 1 31 | 32 | # right moves are indicated by an increase in the x-coordinate 33 | elif curMove == "R": 34 | x += 1 35 | 36 | # up moves are indicated by an increase in the y-coordinate 37 | elif curMove == "U": 38 | y += 1 39 | 40 | # down moves are indicated by a decrease in the y-coordinate 41 | elif curMove == "D": 42 | y -= 1 43 | 44 | # return true if the robots position after all of the moves is still (0,0) 45 | return x == 0 and y == 0 -------------------------------------------------------------------------------- /Workshop4_Recursion/problem3_climbing_stairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Climbing Stairs 9 | Leetcode Problem 70 10 | https://leetcode.com/problems/climbing-stairs/ 11 | */ 12 | 13 | // Suppose you are climbing a set of stairs that has n steps. 14 | // Every step you take, you can climb up either one step or two steps. 15 | // Return the number of distinct ways you can climb up the stairs. 16 | class Solution { 17 | public: 18 | // We will use simple recursion to solve this problem. 19 | int climbStairs(int n) { 20 | // If there is only one step, trivially, we can only climb up 21 | // the step by taking one step. 22 | if(n == 1) { 23 | return 1; 24 | } 25 | 26 | // If there are only two steps, trivially, we can only climb up 27 | // the step by taking one step + one step, or by taking 2 steps. 28 | else if(n == 2) { 29 | return 2; 30 | } 31 | 32 | // Now suppose we have a set of N steps. We have two choices: 33 | // Climb 1 step, or climb 2 steps. If we climb 1, then we 34 | // still have N-1 steps to go. If we climb 2, then we still 35 | // have N-2 steps to go. If we assume we already know how to 36 | // calculate climbStairs(N-1) and climbStairs(N-2), then we 37 | // are done. 38 | else { 39 | return climbStairs(n - 1) + climbStairs(n - 2); 40 | } 41 | } 42 | }; -------------------------------------------------------------------------------- /Workshop1_Introduction/Problem4_Move Zeroes_283.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | To Lower Case 9 | Leetcode Problem 283 10 | https://leetcode.com/problems/move-zeroes/ 11 | 12 | */ 13 | 14 | class Solution { 15 | public: 16 | // Our general strategy will be to put all non-zero 17 | // values at the front of the vector, and then 18 | // put 0's in for the rest of the spaces 19 | void moveZeroes(vector& nums) { 20 | // This value keeps track of the vector index 21 | // that we want to put our next non-zero value 22 | int index = 0; 23 | 24 | for(int i = 0; i < nums.size(); i++) { 25 | // When we encounter a non-zero value, we 26 | // will place it at nums[index], and then 27 | // increment index, because nums[index+1] is 28 | // the place we want to put our next 29 | // non-zero number 30 | if(nums[i] != 0) 31 | nums[index++] = nums[i]; 32 | // Note: This is just shorthand for 33 | // nums[index] = nums[i]; index++; 34 | } 35 | 36 | // We have already gone though the entire vector 37 | // and placed all non-zero values where they should 38 | // be. Now, we just have to make sure that the values 39 | // at nums[index] and onwards are all 0 40 | for(int i = index; i < nums.size(); i++) { 41 | nums[i] = 0; 42 | } 43 | } 44 | }; -------------------------------------------------------------------------------- /Workshop1_Introduction/Problem3_Robot Return to Origin_657.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Remove Robot Return to Origin 9 | Leetcode Problem 657 10 | https://leetcode.com/problems/robot-return-to-origin/ 11 | */ 12 | 13 | class Solution { 14 | public: 15 | bool judgeCircle(string moves) { 16 | 17 | // Our strategy is to keep track of our x and y 18 | // coordinates. We will start at (0, 0) 19 | int x = 0; 20 | int y = 0; 21 | 22 | // Iterating through the moves string so that 23 | // we compute each move one at a time 24 | for(int i = 0; i < moves.size(); i++) { 25 | 26 | char cur = moves[i]; 27 | 28 | // 'L': Decrement x 29 | if(cur == 'L') 30 | x--; 31 | // 'R': Increment x 32 | else if(cur =='R') 33 | x++; 34 | // 'U': Increment y 35 | else if(cur == 'U') 36 | y++; 37 | // 'D': Decrement y 38 | else if(cur == 'D') 39 | y--; 40 | } 41 | 42 | // Return true if we are back at (0, 0) 43 | return x == 0 && y == 0; 44 | 45 | // Note: Our choice x and y is arbitrary. As long 46 | // as you keep 'L' as the opposite of 'R', and 'U' 47 | // as the opposite of 'D', any choice of starting 48 | // position and increment/decrement number would work 49 | // as long as you are consistent. 50 | } 51 | }; -------------------------------------------------------------------------------- /Workshop1_Introduction/Warmup_Fizz Buzz_412.py: -------------------------------------------------------------------------------- 1 | # UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 2 | 3 | # Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | # to be optimal in terms of runtime or memory usage, we are merely giving example 5 | # solutions to questions for learning purposes only 6 | 7 | # Fizz Buzz 8 | # Leetcode Problem 412 9 | # https://leetcode.com/problems/fizz-buzz/ 10 | 11 | class Solution(object): 12 | def fizzBuzz(self, n): 13 | """ 14 | :type n: int 15 | :rtype: List[str] 16 | """ 17 | # initialize an empty array to store the output we wish to return 18 | ret = [] 19 | 20 | # we want our values outputted to start at 1 and go up to n 21 | # Python range(x,y) values include x, and go up to (but don't include) y 22 | for i in range(1, n + 1): 23 | 24 | # Note: we know mathematically that if a % b == 0, then a is divisible by b 25 | 26 | # if our current value i is divisible by both 3 and 5, add "FizzBuzz" to the output 27 | if i % 3 == 0 and i % 5 == 0: 28 | ret.append("FizzBuzz") 29 | 30 | # otherwise, if our current value i is divisible by 3, add "Fizz" to the output 31 | elif i % 3 == 0: 32 | ret.append("Fizz") 33 | 34 | # otherwise, if our current value i is divisible by 5, add "Buzz" to the output 35 | elif i % 5 == 0: 36 | ret.append("Buzz") 37 | 38 | # at this point we know i is not divisible by 3, 5, or both, so we add the integer (as a string) to the output 39 | else: 40 | ret.append(str(i)) 41 | 42 | # return our array of outputs 43 | return ret -------------------------------------------------------------------------------- /Workshop4_Recursion/problem1_sum_of_array_of_numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Sum of Array of Numbers 9 | */ 10 | 11 | // A vector is an STL data structure 12 | // that is basically like an array, 13 | // but you have access to the size() 14 | // function! 15 | 16 | // Given a vector of integers, use recursion to return the sum of 17 | // the integers in the vector. 18 | class Solution { 19 | public: 20 | // To simplify the problem, we will create a helper function that 21 | // returns the sum of a vector starting at a given index. 22 | int sumOfArrayHelper(vector nums, int index) { 23 | // Our base case is when the given index is out of bounds, in which 24 | // we return 0. We are assuming that the index will go out of bounds 25 | // in the positive direction. 26 | if(index >= nums.size()) { 27 | return 0; 28 | } 29 | 30 | // Now, assume that our function works when you are given a higher 31 | // index. In other words, assume we already know the answer to 32 | // sumOfArrayHelper(nums, index + 1). In order to calculate 33 | // sumOfArrayHelper(nums, index), all we need is add nums[index] 34 | // to sumOfArrayHelper(nums, index + 1). 35 | else { 36 | return nums[index] + sumOfArrayHelper(nums, index + 1); 37 | } 38 | } 39 | 40 | // Now, all we need to do is to call sumOfArrayHelper with index 0 41 | // to get our answer. 42 | int sumOfArray(vector nums) { 43 | return sumOfArrayHelper(nums, 0); 44 | } 45 | }; 46 | -------------------------------------------------------------------------------- /Workshop2_Linked Lists/BonusProblem_Intersection of Two Linked Lists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 12 | //you guys havent learned hash tables just yet so I brute forced , 13 | // you can use hash tables if youve taken CS32 already! 14 | int lA = 1,lB = 1; 15 | if (headA == NULL || headB == NULL) 16 | { 17 | return NULL; 18 | } 19 | 20 | ListNode *tempA = headA, *tempB = headB; 21 | 22 | while(tempA->next != NULL) //get the length of the branch A 23 | { 24 | lA++; 25 | tempA = tempA->next; 26 | } 27 | while(tempB->next != NULL) //get the length of the branch B 28 | { 29 | lB++; 30 | tempB = tempB->next; 31 | } 32 | if (tempB != tempA){ 33 | return NULL; //if they dont end at the same node, then you know theres no intersection 34 | } 35 | int skip = abs(lA - lB); //this is to see if one branch is longer than the other 36 | //if one branch is longer, than we can skip "skip" number of values as it wont converge 37 | //before then 38 | 39 | if(lA > lB) //if the length of branch A is longer, go from there 40 | { 41 | while(skip-- > 0) 42 | { 43 | headA = headA->next; 44 | //skip--; 45 | } 46 | } 47 | else if (lB > lA){ // if the length of branch B is longer, go from there 48 | while(skip-- > 0) 49 | { 50 | headB = headB->next; 51 | //skip--; 52 | } 53 | } 54 | //no else as that is if lA and lB are the same length 55 | 56 | while(headB != headA) 57 | { 58 | headA = headA->next; 59 | headB = headB->next; 60 | } 61 | return headA; 62 | } 63 | }; -------------------------------------------------------------------------------- /Workshop5_BigO/Problem1_Valid_Anagram.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Backspace Valid Anagram 242 9 | Leetcode Problem 10 | https://leetcode.com/problems/backspace-string-compare/ 11 | 12 | """ 13 | 14 | """ 15 | Time Complexity: O(N) 16 | Space Complexity: O(1) because our hash table will always have 26 keys as there are 26 characters in the english alphabet. 17 | """ 18 | class Solution: 19 | def isAnagram(self, s: str, t: str) -> bool: 20 | letters = {} #initialize a dictionary that will contain the counts of all the characters. 21 | for c in "abcdefghijklmnopqrstuvwxyz": 22 | letters[c] = 0 23 | 24 | for c in s: 25 | if c in letters: #we update the letter counts in the letters map. 26 | letters[c] += 1 27 | 28 | for c in t: #we update the letter counts in the letters map by decrementing already seen values. 29 | if c in letters: 30 | letters[c] -= 1 31 | 32 | for letter in letters: #this is a check if we have the same letters in both string s and t and same number of characters of each letter 33 | if letters[letter] != 0: 34 | return False 35 | 36 | return True 37 | 38 | 39 | """ 40 | Time Complexity: O(NlongN) 41 | Space Complexity: O(N) 42 | """ 43 | class Solution: 44 | def isAnagram(self, s: str, t: str) -> bool: 45 | if len(s) != len(t): 46 | return False 47 | ls = [] #list of characters for s 48 | for c in s: 49 | ls.append(c) 50 | lt = [] 51 | for c in t: 52 | lt.append(c) 53 | ls = sorted(ls) 54 | lt = sorted(lt) 55 | for i in range(len(s)): 56 | if ls[i] != lt[i]: 57 | return False 58 | return True 59 | -------------------------------------------------------------------------------- /Workshop5_BigO/Problem2_Missing_Number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | to be optimal in terms of runtime or memory usage, we are merely giving example 5 | solutions to questions for learning purposes only 6 | 268 Missing Number 7 | */ 8 | 9 | class Solution { 10 | public: 11 | int missingNumber(vector& nums) { 12 | //this has good run time, but horrible space complexity, can you think why? 13 | int size = nums.size() + 1; 14 | vector complete(size,0); 15 | for(int i = 0; i < size; i++) //we are making an additional array that will 16 | //contain the missing number 17 | { 18 | complete[i] = i; 19 | } 20 | //we will sum up both arrays, complete will always have the bigger sum, 21 | //subtract nums from complete and we will get our missing number. 22 | return accumulate(complete.begin(),complete.end(),0)- accumulate(nums.begin(),nums.end(),0); 23 | } //this implementation did not hold constant space complexity as it will always 24 | //have to create an additionaly O(N) cost array to compare. 25 | }; 26 | 27 | //I think we can do better though! 28 | 29 | //I saw this on leetcode and I thought it was super neat, 30 | //the XOR method, if you dont know what XOR is, I would read up on bitwise operations 31 | //in short, every number in your computer has a bit representation 32 | //XOR means that it will only output 1 if the two inputs are 0 and 1. so 0 and 0, 1 and 1 33 | //will not output 1. 34 | 35 | 36 | class Solution { 37 | public: 38 | int missingNumber(vector& nums){ 39 | int size = nums.size(); 40 | int missing = nums.size(); //if we initialize missing to n, then everything that 41 | //gets XORed will either be 0(as missing will be comparing with the number itself) 42 | //or the number that we are missing! 43 | for(int i = 0; i < size; i++) 44 | { 45 | missing ^= i; 46 | missing ^= nums[i]; 47 | } 48 | return missing; 49 | //this implementation is fast(bitwise operators are very fast) 50 | //and always has constant space complexity as we are not implementing more variables 51 | //for an increasing n. 52 | } 53 | }; -------------------------------------------------------------------------------- /Workshop4_Recursion/problem4_letter_combinations.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Letter Combinations of a Phone Number 9 | Leetcode Problem 17 10 | https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 11 | """ 12 | 13 | class Solution: 14 | def __init__(self): 15 | self.map = { 16 | '2': ['a', 'b', 'c'], 17 | '3': ['d', 'e', 'f'], 18 | '4': ['g', 'h', 'i'], 19 | '5': ['j', 'k', 'l'], 20 | '6': ['m', 'n', 'o'], 21 | '7': ['p', 'q', 'r', 's'], 22 | '8': ['t', 'u', 'v'], 23 | '9': ['w', 'x', 'y', 'z'] 24 | } #initialize a map to store our number -> letters mapping 25 | self.result = {} #define a class result map that will store the combinations encountered thus far. 26 | #We can make quick checks to see if a combination we obtaind has already been accounted for (check for duplicates). 27 | 28 | def letterCombinations(self, digits: str) -> List[str]: 29 | if digits == "": #handle bad input. 30 | return [] 31 | self.letterCombinationsHelper(digits, "") #call our helper. 32 | res = [] #we take our result map and add all the keys into the result list because leetcode wants our result to be in a list. 33 | for key in self.result: 34 | res.append(key) 35 | return res 36 | 37 | def letterCombinationsHelper(self, digits, comb): #we define a helper function to help us with the recursion 38 | if digits == "": #base case. We have no digits left to consider. if the combination is not already present in our result map then we add it to our results. 39 | if comb not in self.result: 40 | self.result[comb] = True 41 | else: 42 | for i in range(len(self.map[digits[0]])): #we take the first digit and consider all possible combinations by iterating through all the letters that digit maps to. 43 | self.letterCombinationsHelper(digits[1:], comb+self.map[digits[0]][i]) 44 | 45 | -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Problem1_Backspace String Compare_844.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Backspace String Compare 9 | Leetcode Problem 844 10 | https://leetcode.com/problems/backspace-string-compare/ 11 | 12 | """ 13 | 14 | class Solution(object): 15 | def backspaceCompare(self, S, T): 16 | """ 17 | :type S: str 18 | :type T: str 19 | :rtype: bool 20 | """ 21 | 22 | # The strategy to solving this problem is to use a character stack. 23 | # Whenever you come across a '#', you pop a character off the stack, 24 | # if possible. When you do this for all characters in a string, the 25 | # stack contains the characters of the final typed string. 26 | 27 | # This function uses the character stack to determine the final typed string 28 | def applyBackspace(word): 29 | 30 | # initialize stack 31 | stack = [] 32 | 33 | # traverse through each character in the string 34 | for i in range(len(word)): 35 | 36 | # if the current character is not a backspace, 37 | # add the current character to the stack 38 | if word[i] != '#': 39 | stack.append(word[i]) 40 | 41 | # otherwise, if the current character is a backspace 42 | # and the stack is not empty, 43 | # pop an element off the stack 44 | else: 45 | if len(stack) > 0: stack.pop() 46 | 47 | # Stacks in Python are represented simply by lists, 48 | # so you can join the elements in the list together 49 | # for a more thorough explanation of the join function 50 | # see: https://www.geeksforgeeks.org/join-function-python/ 51 | 52 | return "".join(stack) 53 | 54 | # Return true if S and T are the same once backspaces are applied 55 | # and false if their results of the applyBackspace function are not equal 56 | return applyBackspace(S) == applyBackspace(T) -------------------------------------------------------------------------------- /Workshop1_Introduction/Warmup_Fizz Buzz_412.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Fizz Buzz 9 | Leetcode Problem 412 10 | https://leetcode.com/problems/fizz-buzz/ 11 | */ 12 | 13 | class Solution { 14 | public: 15 | // Note: Vectors represent arrays that can change in size. 16 | // To use vectors, you need to include the vector library, 17 | // which Leetcode already does for you 18 | vector fizzBuzz(int n) { 19 | // Initialize an empty vector to store the output we wish to return 20 | vector result; 21 | 22 | // We want our vector to represent numbers from 1 to n (inclusive) 23 | for(int i = 1; i <= n; i++) { 24 | // Note: We know that if a % b == 0, then a is divisible by b 25 | 26 | // If our current value i is divisible by both 3 and 5, 27 | // Add "FizzBuzz" to the vector 28 | if(i % 3 == 0 && i % 5 == 0) 29 | // Note: vector::push_back("FizzBuzz") adds a new element 30 | // at the end of the vector, after its current last element. 31 | // "FizzBuzz" is copied (or moved) to the new element 32 | result.push_back("FizzBuzz"); 33 | 34 | // Otherwise, if our current value i is divisible by 3, 35 | // Add "Fizz" to the output 36 | else if(i % 3 == 0) 37 | result.push_back("Fizz"); 38 | 39 | // Otherwise, if our current value i is divisible by 5, 40 | // Add "Buzz" to the output 41 | else if(i % 5 == 0) 42 | result.push_back("Buzz"); 43 | 44 | // At this point we know i is not divisible by 3, 5, or both, 45 | // So we add the integer (as a string) to the output 46 | else 47 | // to_string(i) returns a string with the representation of i 48 | // Note: To use it, you need to include the string library, 49 | // which Leetcode already does for you 50 | result.push_back(to_string(i)); 51 | } 52 | return result; 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /Workshop6_Trees + Hash Tables/Problem2_Path_Sum_112.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Path Sum 9 | Leetcode Problem 112 10 | https://leetcode.com/problems/path-sum/ 11 | """ 12 | 13 | # Definition for a binary tree node. 14 | # class TreeNode: 15 | # def __init__(self, x): 16 | # self.val = x 17 | # self.left = None 18 | # self.right = None 19 | 20 | """ 21 | Given a binary tree and a sum, determine if the tree has a root-to-leaf path 22 | such that adding up all the values along the path equals the given sum. 23 | Note: A leaf is a node with no children. 24 | """ 25 | 26 | class Solution: 27 | def hasPathSum(self, root: TreeNode, sum: int) -> bool: 28 | """ 29 | Base case: If the root is NULL, then 30 | there are no nodes that can add up to 31 | the given sum, so we return false. 32 | """ 33 | 34 | if root == None: 35 | return False 36 | 37 | left = root.left 38 | right = root.right 39 | val = root.val 40 | 41 | """ 42 | The root is a leaf if its left and right 43 | subtree are both NULL. We can then check if 44 | the leaf value equals the given sum. If this 45 | passes, we can return true, because the root 46 | to leaf path (which is just the root itself) 47 | equals the sum. 48 | """ 49 | if left == None and right == None and val == sum: 50 | return True 51 | """ 52 | Recursive case: All possible root to leaf paths must go through 53 | the root, so to check if a root to leaf path exists that adds 54 | up to the given sum, it suffices to check if there is a left 55 | or right subroot to leaf path that adds up to sum minus the value 56 | of the root node. 57 | """ 58 | return self.hasPathSum(left, sum - val) or self.hasPathSum(right, sum - val) 59 | 60 | """ 61 | Time Complexity Analysis: 62 | We examine each node at most twice: once if is ever the root of an examined 63 | subtree, and once if it is ever the child of the a subtree's root. If there are 64 | n nodes, then we do at most 2n examinations, meaning our runtime is O(n). 65 | """ -------------------------------------------------------------------------------- /Workshop6_Trees + Hash Tables/Problem2_Path Sum_112.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Path Sum 9 | Leetcode Problem 112 10 | https://leetcode.com/problems/path-sum/ 11 | */ 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 20 | * }; 21 | */ 22 | 23 | // Given a binary tree and a sum, determine if the tree has a root-to-leaf path 24 | // such that adding up all the values along the path equals the given sum. 25 | 26 | // Note: A leaf is a node with no children. 27 | 28 | class Solution { 29 | public: 30 | bool hasPathSum(TreeNode* root, int sum) { 31 | // Base case: If the root is NULL, then 32 | // there are no nodes that can add up to 33 | // the given sum, so we return false. 34 | if(root == NULL) 35 | return false; 36 | 37 | TreeNode* left = root->left; 38 | TreeNode* right = root->right; 39 | int val = root->val; 40 | 41 | // The root is a leaf if its left and right 42 | // subtree are both NULL. We can then check if 43 | // the leaf value equals the given sum. If this 44 | // passes, we can return true, because the root 45 | // to leaf path (which is just the root itself) 46 | // equals the sum. 47 | if(left == NULL && right == NULL && val == sum) 48 | return true; 49 | 50 | // Recursive case: All possible root to leaf paths must go through 51 | // the root, so to check if a root to leaf path exists that adds 52 | // up to the given sum, it suffices to check if there is a left 53 | // or right subroot to leaf path that adds up to sum minus the value 54 | // of the root node. 55 | return hasPathSum(left, sum - val) || hasPathSum(right, sum - val); 56 | } 57 | }; 58 | 59 | // Time Complexity Analysis: 60 | // We examine each node at most twice: once if is ever the root of an examined 61 | // subtree, and once if it is ever the child of the a subtree's root. If there are 62 | // n nodes, then we do at most 2n examinations, meaning our runtime is O(n). -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Warmup_Valid Parentheses_20.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Valid Parentheses 9 | Leetcode Problem 20 10 | https://leetcode.com/problems/valid-parentheses/ 11 | */ 12 | 13 | class Solution { 14 | public: 15 | 16 | // The strategy for this problem is to use a character stack. 17 | // We traverse the string from left to right, putting open 18 | // parenthesis into the stack, and attemping to match closing 19 | // parenthesis with the top of the stack. 20 | bool isValid(string s) { 21 | stack stack; 22 | for(int i = 0; i < s.size(); i++) { 23 | char cur = s[i]; 24 | 25 | // If the current character is an open parenthesis, 26 | // push it onto the stack 27 | if(cur == '(' || cur == '{' || cur == '[') { 28 | stack.push(cur); 29 | } 30 | 31 | // If the current character is ')', first check 32 | // if the stack is empty. If it is, then ')' can't 33 | // be matched with anything. Next, check if the 34 | // top of the stack is '('. It it isn't, then ')' 35 | // is matched with the wrong open parenthesis. 36 | // If it is, then we have a match, and we can 37 | // remove the open parenthesis from the stack. 38 | else if(cur == ')') { 39 | if(stack.empty() || stack.top() != '(') 40 | return false; 41 | else 42 | stack.pop(); 43 | } 44 | 45 | // Follow a similar process for '}' 46 | else if(cur == '}') { 47 | if(stack.empty() || stack.top() != '{') 48 | return false; 49 | else 50 | stack.pop(); 51 | } 52 | 53 | // Follow a similar process for ']' 54 | else if(cur == ']') { 55 | if(stack.empty() || stack.top() != '[') 56 | return false; 57 | else 58 | stack.pop(); 59 | } 60 | } 61 | 62 | // Finally, we must check if the stack is empty at the end. 63 | // It is wasn't then we would have extra open parenthesis 64 | // that aren't matched with any closing parenthesis. 65 | return stack.empty(); 66 | } 67 | }; -------------------------------------------------------------------------------- /Workshop5_BigO/Problem3_Add_Digits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | to be optimal in terms of runtime or memory usage, we are merely giving example 5 | solutions to questions for learning purposes only 6 | Leetcode 258 Add Digits 7 | */ 8 | 9 | class Solution { 10 | public: 11 | int addDigits(int num) { 12 | //lets implement this recursively 13 | vector total; 14 | if(num < 10) 15 | { 16 | return num; //if the num is less than 10, we good! also 17 | //can serve as our base case if we need it to be 18 | } 19 | while(num > 0) 20 | { 21 | total.push_back(num%10); //get the individual digits of num 22 | num = num / 10; //dont forget to divide 23 | } 24 | //by now num = 0 in order to exit the while loop 25 | for(int i = 0; i < total.size(); i++) 26 | { 27 | num += total[i]; //let num = the digits we pushed back in the vector 28 | } 29 | return addDigits(num); //add recursively until the function returns 30 | //something smaller than 10. 31 | } 32 | //unfortunately, we all know that recursion is probably not our safest bet 33 | //if run time is important to us. 34 | //bad space complexity too because of all the additional copies of vector total 35 | //that we have to keep track 36 | //but this is easy to implement. 37 | 38 | }; 39 | 40 | //how about we skip the stack and just have an int keeping track of sum? 41 | class Solution { 42 | public: 43 | int addDigits(int num) { 44 | if(num<10) 45 | { 46 | return num; 47 | } 48 | int sum = 0; 49 | while(num) 50 | { 51 | sum+=num%10; 52 | num/=10; 53 | } 54 | return addDigits(sum); 55 | } 56 | //so we get rid of the additional space needed! yes, that means that 57 | //space complexity for this implementation is O(1). 58 | //time complexity would be n still. 59 | //bad news: theres a way of doing this in O(1) time and space complexity. 60 | 61 | }; 62 | 63 | 64 | //onto the next solution 65 | 66 | //i recommend reading this nice writeup by alokgupta 67 | //this has time complexity and space complexity of 1. 68 | // I still don't really understand this solution but since you're reading this, 69 | // I recommend going to : https://leetcode.com/problems/add-digits/discuss/501117/O(1)-time-and-space-complexity-floorcongruenceiterativerecursive-approach 70 | //hopefully it blew your mind like it did with mine. 71 | 72 | class Solution { 73 | public: 74 | int addDigits(int num) { 75 | return 1 + (num-1)%9; 76 | } 77 | }; -------------------------------------------------------------------------------- /Workshop5_BigO/Problem1_Valid_Anagram.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | to be optimal in terms of runtime or memory usage, we are merely giving example 5 | solutions to questions for learning purposes only 6 | 242 Valid Anagram 7 | */ 8 | 9 | //bad solution, didn't even realize as this was the first solution that popped up 10 | //in my head. 11 | class Solution { 12 | public: 13 | bool isAnagram(std::string s, std::string t) { 14 | if (s.empty() && t.empty()) //check if either of them are empty 15 | { 16 | return true; 17 | } 18 | if(s.empty() || t.empty()) 19 | { 20 | return false; 21 | } 22 | if (s.length() != t.length()) //dont even have to check if we dont have the same length 23 | { 24 | return false; 25 | } 26 | std::unordered_multiset ums1; 27 | for (int i = 0; i < s.length(); i++) 28 | { 29 | ums1.insert(s[i]); //insert into the hash map 30 | } 31 | for (int i = 0; i < t.length(); i++) 32 | { 33 | char val = t[i]; 34 | if (ums1.end() == ums1.find(val)) 35 | { 36 | return false; //if we cant find the character in the hashmap 37 | //then it is not an anagram 38 | } 39 | ums1.erase(ums1.find(val)); //remove the character for the next iteration 40 | } 41 | return true; // if we exit and havent returned false, then the two string are 42 | //anagrams of each other 43 | } 44 | }; 45 | 46 | class Solution { 47 | public: 48 | bool isAnagram(std::string s,std::string t) 49 | { 50 | //stl has a built in function that will sort this string for you 51 | //do you know which sorting algorithm stl sort() uses? brownie points 52 | //for you if you do :) 53 | sort(s.begin(),s.end()); 54 | sort(t.begin(),t.end()); 55 | return s == t; 56 | //this is good, but I think we can do even better. 57 | } 58 | }; 59 | 60 | class Solution { 61 | public: 62 | bool isAnagram(std::string s,std::string t) 63 | { 64 | //since we know there is only lowercase letters in the anagram, 65 | //we can initialize 2 vectors of size 26 and keep track of the count 66 | //and then compare if theyre the same after 67 | if(s.length() != t.length()) 68 | { 69 | return false; 70 | } 71 | vector s_count(26,0), t_count(26,0); //the () inside means to initialize 72 | //in our case we are initializing a vector of size 26 to 0s. 73 | 74 | int s_length = s.length(); //define outside so the for loop wont continously 75 | //call the length function 76 | for(int i = 0; i < s_length; i++) 77 | { 78 | s_count[s[i] - 'a']++; //increment the count for that particular bin 79 | t_count[t[i] - 'a']++; //ditto 80 | } 81 | return s_count == t_count; //if they end up the same, return true. 82 | } 83 | }; 84 | 85 | 86 | -------------------------------------------------------------------------------- /Workshop6_Trees + Hash Tables/Problem1_Sum of Left Leaves_404.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Sum of Left Leaves 9 | Leetcode Problem 404 10 | https://leetcode.com/problems/sum-of-left-leaves/ 11 | */ 12 | 13 | /** 14 | * Definition for a binary tree node. 15 | * struct TreeNode { 16 | * int val; 17 | * TreeNode *left; 18 | * TreeNode *right; 19 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 20 | * }; 21 | */ 22 | 23 | // Find the sum of all left leaves in a given binary tree. 24 | class Solution { 25 | private: 26 | // First, we define a helper function isLeaf, which determines 27 | // whether the given node is a leaf. We define a leaf to be a 28 | // node with no children. We also define NULL to not be a leaf. 29 | bool isLeaf(TreeNode* node) { 30 | if(node == NULL) 31 | return false; 32 | else 33 | return node->left == NULL && node->right == NULL; 34 | } 35 | public: 36 | int sumOfLeftLeaves(TreeNode* root) { 37 | // Base case: If the root is NULL, then there aren't any 38 | // left leaves to sum up. Therefore, we return 0. 39 | if(root == NULL) 40 | return 0; 41 | 42 | // Recursive case: If the left node exists, we check if 43 | // it is a leaf (both of these checks are done in the 44 | // isLeaf function). If it is, then we add that node's value 45 | // to our result. To find all the other left leaves, we look 46 | // in the right subtree. Note that the root cannot be a left 47 | // leaf because it is not the left parent of any node. 48 | else if(isLeaf(root->left)) 49 | return root->left->val + sumOfLeftLeaves(root->right); 50 | 51 | // Recursive case: If the left node doesn't exist or the left node 52 | // is not a leaf, then we want to search both the left and right 53 | // subtrees for possible left leaves. Again, note that the root connot 54 | // be a left leaf because it is not the left parent of any node. 55 | else 56 | return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); 57 | } 58 | }; 59 | 60 | // Time Complexity Analysis: 61 | // We examine each node at most three times: once if is ever the root of 62 | // an examined subtree once if it is ever the left subroot of an examined 63 | // subtree, and once if it is ever the child of a left subroot. If there are 64 | // n nodes, then we do at most 3n examinations, meaning our runtime is O(n). -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Problem1_Backspace String Compare_844.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Backspace String Compare 9 | Leetcode Problem 844 10 | https://leetcode.com/problems/backspace-string-compare/ 11 | */ 12 | 13 | class Solution { 14 | public: 15 | // The strategy to solving this problem is to use a character stack. 16 | // Whenever you come across a '#', you pop a character off the stack, 17 | // if possible. When you do this for all characters in a string, the 18 | // stack contains the characters of the final typed string. 19 | bool backspaceCompare(string S, string T) { 20 | 21 | // Initialize a stack for S 22 | // and a stack for T 23 | stack stackS; 24 | stack stackT; 25 | 26 | // For each character in S, if you come across a #, you pop 27 | // from the stack if it isn't empty. Otherwise, you add the 28 | // character to the stack. 29 | for(int i = 0; i < S.size(); i++) { 30 | if(S[i] != '#') 31 | stackS.push(S[i]); 32 | else if(!stackS.empty()) 33 | stackS.pop(); 34 | } 35 | 36 | // Follow the same progress for T 37 | for(int i = 0; i < T.size(); i++) { 38 | if(T[i] != '#') 39 | stackT.push(T[i]); 40 | else if(!stackT.empty()) 41 | stackT.pop(); 42 | } 43 | 44 | // At this point, the characters in each stack represents 45 | // the final typed string of each string. To simplify the 46 | // rest of the algorithm, we will check if the stacks are 47 | // the same size. If they aren't, then the final typed 48 | // strings can't be equal. 49 | if(stackS.size() != stackT.size()) 50 | return false; 51 | 52 | // When the algorithm reaches here, we know the stacks are 53 | // the same size, so now all we have to do is to compare 54 | // the contents of the stack. We can do this by continuing 55 | // to take the top of the stack, and comparing those characters 56 | // We continue to do this when both stacks become empty, which 57 | // will happen at the same time. 58 | while(!stackS.empty()) { 59 | char curS = stackS.top(); 60 | char curT = stackT.top(); 61 | if(curS != curT) 62 | return false; 63 | stackS.pop(); 64 | stackT.pop(); 65 | } 66 | 67 | return true; 68 | } 69 | }; -------------------------------------------------------------------------------- /Workshop4_Recursion/problem2_group_integers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Group Integers 9 | */ 10 | 11 | // A vector is an STL data structure 12 | // that is basically like an array, 13 | // but you have access to the size() 14 | // function! 15 | 16 | // Given a vector of integers, return whether is it possible to 17 | // choose a group of some of the integers such that the group sums 18 | // to the given target 19 | class Solution { 20 | public: 21 | // To simplify the problem, we will create a helper function that 22 | // returns whether it is possible to choose a group of some of the 23 | // integers at or after a given index such that the group sums to 24 | // the given target. 25 | bool groupSumHelper(vector nums, int target, int index) { 26 | // If target == 0, we can choose no elements, which by default 27 | // sums up to 0. 28 | if(target == 0) { 29 | return true; 30 | } 31 | 32 | // when the given index is out of bounds, we can't choose any 33 | // elements that add up to target (since target != 0), so we 34 | // return false. We are assuming that the index will go out 35 | // of bounds in the positive direction. 36 | else if(index >= nums.size()) { 37 | return false; 38 | } 39 | 40 | // If the nums[index] (the first element starting at index) equals 41 | // the target, then we can choose just nums[index] to sum up to the 42 | // target. Therefore, we return true. 43 | else if(nums[index] == target) { 44 | return true; 45 | } 46 | 47 | // In all other cases, we have to check the rest of the vector. 48 | // We have two choices. 49 | else { 50 | return 51 | 52 | // If nums[index] is in the group that sums 53 | // up to the target, we have to check if there is a group in the 54 | // rest of the vector that sums up to target - nums[index]. 55 | groupSumHelper(nums, target - nums[index], index + 1) || 56 | 57 | // If nums[index] is not in the group, then we just check if there 58 | // if a group in the rest of the vector that sums up to target. 59 | groupSumHelper(nums, target, index + 1); 60 | } 61 | } 62 | 63 | // Now, all we need to do is to call groupSumHelper with index 0 64 | // to get our answer. 65 | bool groupSum(vector nums, int target) { 66 | return groupSumHelper(nums, target, 0); 67 | } 68 | }; -------------------------------------------------------------------------------- /Workshop5_BigO/Problem4_Find_Peak_Element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | Disclaimer: This is not the only valid solution and we do not claim our solutions 4 | to be optimal in terms of runtime or memory usage, we are merely giving example 5 | solutions to questions for learning purposes only 6 | Leetcode 162: Find Peak Element 7 | */ 8 | 9 | class Solution { 10 | public: 11 | int findPeakElement(vector& nums) { 12 | if (nums.size() == 1) //we dont need to check if there is only one element 13 | //in the vector 14 | { 15 | return 0; //return index 0. 16 | } 17 | for (auto it = nums.begin(); it != nums.end()-1; it++) //end the index before 18 | //because we are comparing to the index right after and will 19 | //go out of bound if we dont. 20 | { 21 | if(*it > *(it+1)) //if current is greater, it is a peak, return first peak 22 | { 23 | return it - nums.begin(); //this will return an int instead of the iterator 24 | } 25 | } 26 | return nums.size()-1; //if we break out the loop, then that means that,the last 27 | //thing is the greatest. 28 | } 29 | //space complexity is constant time because we are not creating any 30 | //run time is O(N) because its possible that we dont reach the peak until 31 | //the very end of the vector 32 | }; 33 | 34 | //the previous solution had a time complexity of O(N), but we can do even better. 35 | 36 | class Solution { 37 | public: 38 | int findPeakElement(vector& nums){ 39 | if(nums.size() == 1) 40 | { 41 | return 0; 42 | } 43 | int start = 0; 44 | int end = nums.size() - 1; 45 | while(start < end){ 46 | int mid = start + (end-start) / 2; //split down the half and break it into 47 | //smaller sub arrays traversing. 48 | if(mid == start) //if our middle is the start, then we have 49 | //exhausted all options, and will break out and return start 50 | break; 51 | if(nums[mid] > nums[mid-1]) //if nums[mid] is greater than the next index, 52 | //then it is a possibility that it is a local maximum. 53 | { 54 | start = mid; //set the next start iteration to the current middle 55 | } 56 | else 57 | { 58 | end = mid; //set the next end iteration to the current middle, 59 | //we no longer check the later half of the array. 60 | } 61 | } 62 | return start; //if we break out, then the first element is the greatest. 63 | } 64 | //Time complexity is log(n) average case, worst case would be the list is sorted in 65 | //descending order, can you guess what the time complexity for worst case is? 66 | }; -------------------------------------------------------------------------------- /Workshop6_Trees + Hash Tables/Problem1_Sum_of_Left_Leaves_404.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Sum of Left Leaves 9 | Leetcode Problem 404 10 | https://leetcode.com/problems/sum-of-left-leaves/ 11 | """ 12 | 13 | """ 14 | Definition for a binary tree node. 15 | class TreeNode: 16 | def __init__(self, x): 17 | self.val = x 18 | self.left = None 19 | self.right = None 20 | """ 21 | class Solution: 22 | def isLeaf(self, root: TreeNode) -> bool: 23 | """ 24 | First, we define a helper function isLeaf, which determines 25 | whether the given node is a leaf. We define a leaf to be a 26 | node with no children. We also define NULL to not be a leaf. 27 | """ 28 | if root == None: 29 | return False 30 | else: 31 | return root.left == None and root.right == None 32 | 33 | def sumOfLeftLeaves(self, root: TreeNode) -> int: 34 | if root == None: 35 | """ 36 | Base case: If the root is NULL, then there aren't any 37 | left leaves to sum up. Therefore, we return 0. 38 | """ 39 | return 0 40 | elif self.isLeaf(root.left): 41 | """ 42 | Recursive case: If the left node exists, we check if 43 | it is a leaf (both of these checks are done in the 44 | isLeaf function). If it is, then we add that node's value 45 | to our result. To find all the other left leaves, we look 46 | in the right subtree. Note that the root cannot be a left 47 | leaf because it is not the left parent of any node. 48 | """ 49 | return root.left.val + self.sumOfLeftLeaves(root.right) 50 | else: 51 | """ 52 | Recursive case: If the left node doesn't exist or the left node 53 | is not a leaf, then we want to search both the left and right 54 | subtrees for possible left leaves. Again, note that the root connot 55 | be a left leaf because it is not the left parent of any node. 56 | """ 57 | return self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right) 58 | 59 | """ 60 | Time Complexity Analysis: 61 | We examine each node at most three times: once if is ever the root of 62 | an examined subtree once if it is ever the left subroot of an examined 63 | subtree, and once if it is ever the child of a left subroot. If there are 64 | n nodes, then we do at most 3n examinations, meaning our runtime is O(n). 65 | """ -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Warmup_Valid Parentheses_20.py: -------------------------------------------------------------------------------- 1 | """ 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Valid Parentheses 9 | Leetcode Problem 20 10 | https://leetcode.com/problems/valid-parentheses/ 11 | 12 | """ 13 | 14 | class Solution(object): 15 | def isValid(self, s): 16 | """ 17 | :type s: str 18 | :rtype: bool 19 | """ 20 | 21 | # The strategy for this problem is to traverse through 22 | # the the string and push open parentheses onto the stack 23 | # and then pop them off the stack once we enocounter their 24 | # closed parenthesis counterpart. 25 | 26 | # We create a dictionary mapping closed parentheses to 27 | # their equivalent open parentheses so we can check if 28 | # what we popped off of the stack corresponds to the 29 | # closed parenthesis we currently have 30 | par = {")":"(","}":"{","]":"["} 31 | stack = [] 32 | 33 | # Traverse through all the characters in s 34 | # An assumption we make here based on the prompt of 35 | # the question is that there are only parentheses in 36 | # this string 37 | for char in s: 38 | 39 | # Since our dictionary has closed parenthesis keys, 40 | # we know that if the current character is not in 41 | # the dictionary, it's an open parenthesis 42 | if char not in par: 43 | 44 | # Add open parentheses to the stack 45 | stack.append(char) 46 | 47 | # Otherwise, we know it's a closed parenthesis 48 | else: 49 | 50 | # If the stack is empty, this means we have a 51 | # closed parenthesis without any open parentheses 52 | # before it, so we know this is invalid 53 | if len(stack) == 0: return False 54 | 55 | # Otherwise, if the top of the stack constains the 56 | # open counterpart to your current closed parenthesis, 57 | # then you have a valid pair, so you can pop the open 58 | # parenthesis off the stack 59 | elif stack[-1] == par[char]: 60 | # (arr[-1] in Python gives you the last element in a list, 61 | # which is the top of the stack) 62 | stack.pop() 63 | 64 | # Otherwise, this is not the correct pairing of open and closed 65 | # parentheses, so the string has invalid parentheses 66 | else: return False 67 | 68 | # Return True if the stack is empty (all parentheses come in pairs) 69 | # and False if there are left over open parentheses without a match 70 | return len(stack) == 0 -------------------------------------------------------------------------------- /Workshop6_Trees + Hash Tables/Problem4_Intersection of Two Arrays_349.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Intersection of Two Arrays 9 | Leetcode Problem 349 10 | https://leetcode.com/problems/intersection-of-two-arrays/ 11 | */ 12 | 13 | // Given two arrays, write a function to compute their intersection. 14 | // Note: Each element in the result must be unique. The result can be in any order. 15 | class Solution { 16 | public: 17 | // Our strategy is to add the values of nums1 to a set. Next, we 18 | // add the values of nums2 into a different set only if the value 19 | // appears in the first set. Then, we turn this new set into a vector. 20 | vector intersection(vector& nums1, vector& nums2) { 21 | // In order to take advantage of the efficiency of 22 | // hashing, we will use an unordered set (implemented 23 | // by hash tables) over a set (implemented by trees) 24 | 25 | // Add the values of nums1 to set 26 | unordered_set set; 27 | for(int i = 0; i < nums1.size(); i++) 28 | // Use the insert function to insert 29 | // values into the set 30 | set.insert(nums1[i]); 31 | 32 | // Add the values of nums2 into intersection 33 | // only if the value appears in set 34 | unordered_set intersection; 35 | for(int i = 0; i < nums2.size(); i++) { 36 | // Note: The set.find(x) only returns 37 | // set.end() only if x isn't in the set 38 | if(set.find(nums2[i]) != set.end()) 39 | intersection.insert(nums2[i]); 40 | } 41 | 42 | vector result; 43 | // To iterate through an unordered_set, use an iterator 44 | for(auto i = intersection.begin(); i != intersection.end(); i++) 45 | result.push_back(*i); 46 | return result; 47 | } 48 | }; 49 | 50 | // Time Complexity Analysis: 51 | // We loop through nums1 all the way, and then loop through nums2 all the way. 52 | // Then, we loop through the intersection of nums1 and nums2 all the way to turn 53 | // the unordered_set into a vector. Suppose that nums1.size() = m and 54 | // nums2.size() = n. We know that the intersection of nums1 and nums2 is at least 55 | // min(m, n). Therefore, we can say that the time complexity of this algorithm 56 | // is O(m + n). 57 | 58 | // Space Complexity Analysis: 59 | // Suppose that nums1.size() = m and nums2.size() = n. We have a unordered_set of 60 | // at most size m to store the unique values of nums1. We construct an intersection 61 | // using the unordered_set as a filter, so intersection must have a size of at 62 | // most m as well. We also know that the vector result is the same size as 63 | // the intersection. Therefore, we have that the space complexity of this 64 | // algorithm is O(m). -------------------------------------------------------------------------------- /Workshop6_Trees + Hash Tables/Problem3_Two Sum_1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Two Sum 9 | Leetcode Problem 1 10 | https://leetcode.com/problems/two-sum/ 11 | */ 12 | 13 | // Given an array of integers, return indices of the two numbers such that 14 | // they add up to a specific target. 15 | // You may assume that each input would have exactly one solution, and you 16 | // may not use the same element twice. 17 | class Solution { 18 | public: 19 | // Our strategy is to map each value x to its index in the 20 | // vector. While we do this, we check if the map contains 21 | // the key y so that target = x + y 22 | vector twoSum(vector& nums, int target) { 23 | // In order to take advantage of the efficiency of 24 | // hashing, we will use an unordered map (implemented 25 | // by hash tables) over a map (implemented by trees) 26 | unordered_map map; 27 | 28 | // We will store our solution indexes here 29 | vector solutions; 30 | 31 | for(int i = 0; i < nums.size(); i++) { 32 | // Given value x, we are looking for y 33 | // such that t = x + y. In other words, 34 | // y = t - x 35 | int complement = target - nums[i]; 36 | 37 | // Given a value, if we find the complement 38 | // in the map, then we find the two indexes 39 | // i and j such that nums[i] + nums[j] = target 40 | if(map.find(complement) != map.end()) { 41 | // Add the current index, as well as the 42 | // index of the complement value to the 43 | // solution. By our implementation, we 44 | // mapped the complement to its index. 45 | // Then, we return the solution 46 | solutions.push_back(i); 47 | solutions.push_back(map[complement]); 48 | return solutions; 49 | } 50 | 51 | // If we don't find the complement, then 52 | // we just map the current value to the 53 | // current index. A future value may be 54 | // the complement to this value. 55 | map[nums[i]] = i; 56 | } 57 | 58 | // We are guaranteed to find a solution, but C++ doesn't 59 | // know this. Therefore, we must return a vector 60 | // at the end of the loop, even if this code never runs 61 | return solutions; 62 | } 63 | }; 64 | 65 | // Time Complexity Analysis: 66 | // We examine each value in nums at most once. If nums.size() = n, then we 67 | // will do at most n examinations. Therefore, the time complexity is O(n). 68 | 69 | // Space Complexity Analysis: 70 | // We attempt to put each value in an unordered_map. If nums.size() = n, 71 | // then in the worst case, we add all n elements into the unordered_map. 72 | // Therefore, the space complexity is O(n). -------------------------------------------------------------------------------- /Workshop4_Recursion/problem4_letter_combinations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Letter Combinations of a Phone Number 9 | Leetcode Problem 17 10 | https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 11 | */ 12 | 13 | // A vector is an STL data structure 14 | // that is basically like an array, 15 | // but you have access to the size() 16 | // function! Also, push_back(x) acts 17 | // like push(x) of a stack. 18 | 19 | // We will use simple recursion to solve this problem. 20 | class Solution { 21 | private: 22 | // We will define a helper function that takes in 23 | // a character and returns the vector of the letters 24 | // the character can map to. 25 | vector map(char digit) { 26 | switch(digit) { 27 | case '2': return {"a", "b", "c"}; 28 | case '3': return {"d", "e", "f"}; 29 | case '4': return {"g", "h", "i"}; 30 | case '5': return {"j", "k", "l"}; 31 | case '6': return {"m", "n", "o"}; 32 | case '7': return {"p", "q", "r", "s"}; 33 | case '8': return {"t", "u", "v"}; 34 | case '9': return {"w", "x", "y", "z"}; 35 | default: return {}; 36 | } 37 | } 38 | public: 39 | vector letterCombinations(string digits) { 40 | // If we are given no digits, then we can't make and letter 41 | // combinations. Therefore, we return the empty vector. 42 | if(digits.size() == 0) { 43 | return {}; 44 | } 45 | 46 | // If we are given only one digit, then we simply return 47 | // a vector of all the letters that digit maps to. 48 | else if(digits.size() == 1) { 49 | return map(digits[0]); 50 | } 51 | 52 | // Assume that we are passed a string of length N, but we can 53 | // compute letterCombinations for a string of length N-1. 54 | // We can compute letterCombinations for a string of length N 55 | // by getting a vector of letters the first character can map 56 | // to, and then adding that to the letterCombinations of the 57 | // rest of the characters. 58 | else { 59 | // We will construct our vector inside the variable result. 60 | vector result; 61 | 62 | // heads represent the vector of letters the first character 63 | // can map to. 64 | vector heads = map(digits[0]); 65 | 66 | // tails represents letterCombinations on the rest of the characters 67 | // Note that digits.substr(1, digits.size() - 1) returns digits without 68 | // the first character. 69 | vector tails = letterCombinations(digits.substr(1, digits.size() - 1)); 70 | 71 | // We will use a nested for-loop to construct our result. 72 | // This is because we want to add every letter in heads 73 | // to the beginning of each letter combination in tails. 74 | for(int i = 0; i < heads.size(); i++) { 75 | for(int j = 0; j < tails.size(); j++) { 76 | result.push_back(heads[i] + tails[j]); 77 | } 78 | } 79 | 80 | return result; 81 | } 82 | } 83 | }; -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Problem2_Implement Stack Using Queues_255.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Implement Stack using Queues 9 | Leetcode Problem 225 10 | https://leetcode.com/problems/implement-stack-using-queues/ 11 | */ 12 | 13 | class MyStack { 14 | private: 15 | queue q; 16 | public: 17 | // Notice how we did not define a constructor. This is because 18 | // we don't need a constructor to initialize anything, so the 19 | // default constructor (when no constructor is defined) 20 | // will do just fine in this case! 21 | 22 | /** Push element x onto stack. */ 23 | void push(int x) { 24 | // A queue's enqueue and a stack's push is essentially 25 | // the same thing, so we can just enqueue x into q. 26 | q.push(x); 27 | } 28 | 29 | /** Removes the element on top of the stack and returns that element. */ 30 | int pop() { 31 | // We want to get the last-in element and remove it from the queue. 32 | // We can only remove the first-in element, so to remove the 33 | // last-in element, we have to remove all elements that came 34 | // "before" the last-in element. 35 | 36 | // However, we want to save all the elements in the queue except the 37 | // last-in one, so when we pop those items, we have to add them back 38 | // into the queue. Note that this maintains the order of the elements. 39 | 40 | int cur; 41 | 42 | // For each element in the queue except the last-in one, we dequeue 43 | // it from the queue, and then put it back into the queue. 44 | for(int i = 0; i < q.size() - 1; i++) { 45 | cur = q.front(); 46 | q.pop(); 47 | q.push(cur); 48 | } 49 | 50 | // At this point in the algorithm, the last-in element is now 51 | // the first-in element. Since we have a queue, we can easily 52 | // remove this element. 53 | cur = q.front(); 54 | q.pop(); 55 | 56 | // Read the directions carefully: Even though the C++ STL stack 57 | // doesn't return a value, we're asked to return the popped 58 | // element in this problem! 59 | return cur; 60 | } 61 | 62 | /** Get the top element. */ 63 | int top() { 64 | // We could follow a similar algorithm that we used for the 65 | // pop function, HOWEVER, because the pop function returns 66 | // the popped element (which is on the top of the stack), 67 | // we can come up with a simpler algorithm. 68 | 69 | // Pop the last-in element, and save it 70 | int top = pop(); 71 | 72 | // Put the last-in element back into the queue 73 | // If we imagine our queue as a stack, we put 74 | // the element at the top of the stack. 75 | push(top); 76 | 77 | // Essentially, we popped off an element off the 78 | // top of the stack, and then put it back in 79 | // where it originally was. 80 | return top; 81 | } 82 | 83 | /** Returns whether the stack is empty. */ 84 | bool empty() { 85 | // Checking if a queue is empty is essentially the same 86 | // as checking if a stack is empty, so we can just check 87 | // if q is empty. 88 | return q.empty(); 89 | } 90 | }; -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Problem4_Remove Outermost Parentheses_1021.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Leetcode Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Remove Outermost Parentheses 9 | Leetcode Problem 1021 10 | https://leetcode.com/problems/remove-outermost-parentheses/ 11 | */ 12 | 13 | class Solution { 14 | public: 15 | // Surprise! This solution actually doesn't require a 16 | // stack or a queue! However, it uses stack-like ideas, 17 | // so it's fair game as a stack/queue problem! 18 | 19 | // The strategy is to keep a counter when looping through the 20 | // string. Every time we see '(', we increment the counter. 21 | // Every time we see ')', we decrement the counter. This 22 | // is analogous to pushing into a stack whenever we see '(', 23 | // and popping the stack whenever we see ')'. 24 | 25 | // However, there are two special cases. If we see '(' and 26 | // our count is 0 (a.k.a the stack is empty), this is an 27 | // outer '(' and won't be included in the output. If we 28 | // see ')' and our count is 1 (a.k.a the stack has one 29 | // element), this is an outer ')' and also won't be 30 | // included in the output. These are special cases because 31 | // these operations either start with a count of 0, or end 32 | // with a count of 0. 33 | 34 | string removeOuterParentheses(string S) { 35 | int count = 0; 36 | string result = ""; 37 | for(int i = 0; i < S.size(); i++) { 38 | 39 | // Case 1: We see '(' and our count is 0. 40 | // This is an outer parenthesis and won't 41 | // go into our solution. Note that since 42 | // we're guaranteed to be given a valid 43 | // string, every time the count if 0, 44 | // we're guaranteed that S[i] == '('. 45 | if(count == 0) { 46 | count++; 47 | } 48 | 49 | // Case 2: We see ')' and our count is 1. 50 | // This is an outer parenthesis and won't 51 | // go into our solution. 52 | else if(count == 1 && S[i] == ')') { 53 | count--; 54 | } 55 | 56 | // Case 3: We see '(' and our count isn't 0. 57 | // This is an inner parenthesis and will 58 | // go into our solution. 59 | else if(S[i] == '(') { 60 | count++; 61 | result += S[i]; 62 | } 63 | 64 | // Case 4: We see ')' and our count isn't 1. 65 | // This is an inner parenthesis and will 66 | // go into our solution. 67 | else /* S[i] == ')' */ { 68 | count--; 69 | result += S[i]; 70 | } 71 | } 72 | return result; 73 | } 74 | 75 | // Here is the stack solution. It's not as efficient as 76 | // the previous solution, but it's a good exercise to 77 | // compare this algorithm with our solution. 78 | 79 | string alsoRemoveOuterParentheses(string S) { 80 | stack st; 81 | string result = ""; 82 | for(int i = 0; i < S.size(); i++) { 83 | if(st.empty()) { 84 | st.push('('); 85 | } 86 | else if(st.size() == 1 && S[i] == ')') { 87 | st.pop(); 88 | } 89 | else if(S[i] == '(') { 90 | st.push('('); 91 | result += S[i]; 92 | } 93 | else { 94 | st.pop(); 95 | result += S[i]; 96 | } 97 | } 98 | return result; 99 | } 100 | 101 | }; -------------------------------------------------------------------------------- /Workshop3_Stacks + Queues/Problem3_Evaluate Postfix Expressions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | UCLA ACM ICPC: Interview Track Problem Solutions 3 | 4 | Disclaimer: This is not the only valid solution and we do not claim our solutions 5 | to be optimal in terms of runtime or memory usage, we are merely giving example 6 | solutions to questions for learning purposes only 7 | 8 | Evaluate Postfix Expressions 9 | Geeks4Geeks: https://www.geeksforgeeks.org/stack-set-4-evaluation-postfix-expression/ 10 | */ 11 | 12 | // Return the value of the postfix expression. Assume 13 | // that the only characters that can appear in the C-string are 14 | // '0', '1', ..., '8', '9', '+', '-', '*', and '/', 15 | // and assume that the given postfix expression is valid. 16 | class Solution { 17 | public: 18 | 19 | // Did you pay attention in lecture to the evaluating postfix 20 | // expressions algorithm? If yes, good for you! Skip the explanation 21 | // to the implementation. If not, it's your lucky day! 22 | 23 | // In postfix notation, the operator comes AFTER the operands, so 24 | // instead of a + b, we have a b +. Computers like postfix notation 25 | // because all postfix expressions can be expressed without parenthesis, 26 | // and the algorithm for evaluating postfix is relatively simple! 27 | 28 | // Traverse the tokens (either an operand or an operator) from left to 29 | // right. If it's an operand, push it into your stack. If it's an operator, 30 | // pop two operands from your stack, apply the operator on those 31 | // two operands, and then push the result onto the stack. At the end of 32 | // the traveral, there will be one value in the stack, and that value is 33 | // your answer! 34 | 35 | // Here is an example run of the algorithm: 36 | 37 | // 3 2 + 6 * 38 | // 3: Push onto stack: [3] 39 | // 2: Push onto stack: [3, 2] 40 | // +: 41 | // Pop off 3 and 2: [] 42 | // Apply 3 + 2: 5 43 | // Push result onto stack: [5] 44 | // 6: Push onto stack: [5, 6] 45 | // *: 46 | // Pop off 5 and 6: [] 47 | // Apply 5 * 6: 30 48 | // Push result onto stack: [30] 49 | // We are done, and our result is 30 50 | 51 | int evaluatePostfix(char[] exp) { 52 | 53 | stack st; 54 | 55 | // Scan all characters one by one 56 | // Recall: C-strings end with '\0' 57 | for (int i = 0; exp[i] != '\0'; i++) { 58 | 59 | // If the scanned character is an operand, push it to the stack. 60 | // Recall: If a char is '0', '1', ..., '8', '9', then you can get 61 | // its integer value by doing char - '0' 62 | if (exp[i] - '0' >= 0 && exp[i] - '0' <= 9) 63 | st.push(exp[i] - '0'); 64 | 65 | // If the scanned character is an operator, pop two 66 | // elements from stack and apply the operator 67 | else { 68 | 69 | // Pop two operands from your stack 70 | int val1 = st.top(); st.pop(); 71 | int val2 = st.top(); st.pop(); 72 | 73 | // Apply the operator on those two operands, 74 | // and then push the result onto the stack 75 | switch (exp[i]) { 76 | case '+': st.push(val2 + val1); break; 77 | case '-': st.push(val2 - val1); break; 78 | case '*': st.push(val2 * val1); break; 79 | case '/': st.push(val2 / val1); break; 80 | } 81 | // Note: Order matters! The second value you 82 | // pop off the stack should be on the left- 83 | // hand side of the operator, and the first 84 | // value should be on the right-hand side 85 | } 86 | } 87 | 88 | // We are done, and our result is 89 | // the one value in the stack 90 | return st.top(); 91 | } 92 | 93 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CS 32 Technical Interview Prep Workshop 2 | Presented by UCLA ACM ICPC 3 | Event Details: https://tinyurl.com/cs32-icpc 4 | Questions: https://tinyurl.com/ask-ucla-icpc 5 | ICPC Facebook Group: https://www.facebook.com/groups/ucla.icpc/ 6 | Feedback Survey: https://forms.gle/JJAmgrR93vtdSURy7 7 | 8 | ### Schedule: 9 | * Workshop 1 (Week 2): Introduction to Technical Interviews 10 | * Workshop 2 (Week 3): Linked Lists 11 | * Workshop 3 (Week 5): Stacks and Queues 12 | * Workshop 4 (Week 6): Recursion 13 | * Workshop 5 (Week 7): Big-O and STLs 14 | * Workshop 6 (Week 8): ~~Trees and Binary Search~~ No meeting this week Winter 2020 15 | * Workshop 7 (Week 9): Trees and Hash Tables 16 | 17 | ## Workshop 1: Introduction to Technical Interviews 18 | Slides: https://docs.google.com/presentation/d/1SEaFGIBbvp7M_9ow1_6bcuTDRzenfgPmmCLPLU3_hAc/edit?usp=sharing 19 | 20 | Warmup Problem: Fizz Buzz 21 | * Leetcode Problem 412: https://leetcode.com/problems/fizz-buzz/ 22 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Warmup_Fizz%20Buzz_412.cpp 23 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Warmup_Fizz%20Buzz_412.py 24 | 25 | Problem 1: Power of Two 26 | * Leetcode Problem 231: https://leetcode.com/problems/power-of-two/ 27 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Problem1_Power%20of%20Two_231.cpp 28 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Problem1_Power%20of%20Two_231.py 29 | 30 | Problem 2: Remove Vowels from a String 31 | * Leetcode Problem 1119: https://leetcode.com/problems/remove-vowels-from-a-string/ 32 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Problem2_Remove%20Vowels%20from%20a%20String_1119.cpp 33 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Problem2_Remove%20Vowels%20from%20a%20String_1119.py 34 | 35 | Problem 3: Robot Return to Origin 36 | * Leetcode Problem 657: https://leetcode.com/problems/robot-return-to-origin/ 37 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Problem3_Robot%20Return%20to%20Origin_657.cpp 38 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Problem3_Robot%20Return%20to%20Origin_657.py 39 | 40 | Problem 4: To Lower Case 41 | * Leetcode Problem 709: https://leetcode.com/problems/to-lower-case/ 42 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Problem4_To%20Lower%20Case_709.cpp 43 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop1_Introduction/Problem4_To%20Lower%20Case_709.py 44 | 45 | Additional Problems: 46 | * Leetcode Problem 344: https://leetcode.com/problems/reverse-string/ 47 | * Leetcode Problem 283: https://leetcode.com/problems/move-zeroes/ 48 | * Leetcode Problem 7: https://leetcode.com/problems/reverse-integer/ 49 | * Leetcode Problem 66: https://leetcode.com/problems/plus-one/ 50 | 51 | ## Workshop 2: Linked Lists 52 | Slides: https://docs.google.com/presentation/d/1JLyHwHtVZiEKCivv0K1txKbsF-P8bZCe8CkCZjhHiAM/edit 53 | 54 | Warmup Problem: Delete Node in a Linked List 55 | * Leetcode Problem 237: https://leetcode.com/problems/delete-node-in-a-linked-list/ 56 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Warmup_.cpp 57 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Warmup_.py 58 | 59 | Problem 1: Middle of the Linked List 60 | * Leetcode Problem 876: https://leetcode.com/problems/middle-of-the-linked-list 61 | * C++ Solution: 62 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Problem1_Middle_of_Linked_Lists.py 63 | 64 | Problem 2: Reverse Linked List 65 | * Leetcode Problem 206: https://leetcode.com/problems/reverse-linked-list 66 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Problem2_Reverse%20Linked%20List.cpp 67 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Problem2_Reverse_Linked_Lists.py 68 | 69 | Problem 3: Merge Two Sorted Lists 70 | * Leetcode Problem 21: https://leetcode.com/problems/merge-two-sorted-lists/ 71 | * C++ Solution: 72 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Problem3_Merge_Two_Sorted_Lists.py 73 | 74 | Problem 4: Linked List Cycle 75 | * Leetcode Problem 141: https://leetcode.com/problems/linked-list-cycle 76 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Problem1_Linked%20List%20Cycle.cpp 77 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Problem4_LinkedListCycle.py 78 | 79 | Bonus Problem: Intersection of Two Linked Lists 80 | * Leetcode Problem 160: https://leetcode.com/problems/intersection-of-two-linked-lists 81 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop2_Linked%20Lists/Problem3_Intersection%20of%20Two%20Linked%20Lists 82 | * Python Solution: 83 | 84 | Additional Problems: 85 | * Leetcode Problem 83: https://leetcode.com/problems/remove-duplicates-from-sorted-list 86 | * Leetcode Problem 203: https://leetcode.com/problems/remove-linked-list-elements/ 87 | * Leetcode Problem 1290: https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer 88 | * Leetcode Problem 234: https://leetcode.com/problems/palindrome-linked-list 89 | 90 | ## Workshop 3: Stacks and Queues 91 | Slides: https://docs.google.com/presentation/d/1T6h2FOzrU48AzzI5K8gE9X0Rk7FPJTT6EUKUByBMgpw/edit?usp=sharing 92 | 93 | Warmup Problem: Valid Parentheses 94 | * Leetcode Problem 20: https://leetcode.com/problems/valid-parentheses/ 95 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Warmup_Valid%20Parentheses_20.cpp 96 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Warmup_Valid%20Parentheses_20.py 97 | 98 | Problem 1: Backspace String Compare 99 | * Leetcode Problem 844: https://leetcode.com/problems/backspace-string-compare/ 100 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Problem1_Backspace%20String%20Compare_844.cpp 101 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Problem1_Backspace%20String%20Compare_844.py 102 | 103 | Problem 2: Implement Stack Using Queues 104 | * Leetcode Problem 225: https://leetcode.com/problems/implement-stack-using-queues/ 105 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Problem2_Implement%20Stack%20Using%20Queues_255.cpp 106 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Problem2_Implement%20Stack%20Using%20Queues_255.py 107 | 108 | Problem 3: Evaluate Postfix Expressions 109 | * Geeks for Geeks Problem: https://www.geeksforgeeks.org/stack-set-4-evaluation-postfix-expression/ 110 | * CS 32 Solution: https://docs.google.com/presentation/d/1gMBk2Km7qNUcvF-4pSBIyK-ynjQbHsQv/edit#slide=id.p16 111 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Problem3_Evaluate%20Postfix%20Expressions.cpp 112 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Problem3_Evaluate%20Postfix%20Expressions.py 113 | 114 | Problem 4: Remove Outermost Parentheses 115 | * Leetcode Problem 1021: https://leetcode.com/problems/remove-outermost-parentheses/ 116 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Problem3_Evaluate%20Postfix%20Expressions.cpp 117 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop3_Stacks%20%2B%20Queues/Problem4_Remove%20Outermost%20Parentheses_1021.py 118 | 119 | ## Workshop 4: Recursion 120 | Slides: https://docs.google.com/presentation/d/1Cs-cDp8teGPEopOy-AiCJJ7pDy4U2L6SyRBh_K_3-BI/edit?usp=sharing 121 | 122 | Warmup Problem: Factorial 123 | * Geeks for Geeks problem: https://www.geeksforgeeks.org/program-for-factorial-of-a-number/ 124 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/Warmup_.cpp 125 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/Warmup_.py 126 | 127 | Problem 1: Sum Of Array Of Numbers Using Recursion. 128 | * Geeks for Geeks: https://www.geeksforgeeks.org/sum-array-elements-using-recursion/ 129 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/problem1_sum_of_array_of_numbers.cpp 130 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/problem1_sum_of_array_of_numbers.py 131 | 132 | Problem 2: Group Integers 133 | * CodingBat: https://codingbat.com/prob/p145416 134 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/problem2_group_integers.cpp 135 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/problem2_group_integers.py 136 | 137 | Problem 3: Climbing Stairs. 138 | * Leetcode: https://leetcode.com/problems/climbing-stairs/ 139 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/problem3_climbing_stairs.cpp 140 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/problem3_climbing_stairs.py 141 | 142 | Problem 4: Letter Combinations. 143 | * Leetcode Problem 17: https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 144 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/problem4_letter_combinations.cpp 145 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop4_Recursion/problem4_letter_combinations.py 146 | 147 | 148 | ## Workshop 5: Big-O 149 | Slides: https://docs.google.com/presentation/d/1j1nIrwrgjOxo5cS73hW8agERTAz2f7qLRAjSZE88MNg/edit#slide=id.g7e66b61697_1_24 150 | Kahoot: https://play.kahoot.it/v2/lobby?quizId=e7d7d6ba-b368-41fa-8fc9-3c41f950ee29 151 | 152 | Problem 1: Valid Anagram 153 | * Leetcode Problem 242: https://leetcode.com/problems/valid-anagram/ 154 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop5_BigO/Problem1_Valid_Anagram.cpp 155 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop5_BigO/Problem1_Valid_Anagram.py 156 | 157 | Problem 2: Missing Number 158 | * Leetcode Problem 268: https://leetcode.com/problems/missing-number/ 159 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop5_BigO/Problem2_Missing_Number.cpp 160 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop5_BigO/Problem2_Missing_Number.py 161 | 162 | Problem 3: Add Digits 163 | * Leetcode Problem 258: https://leetcode.com/problems/add-digits/ 164 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop5_BigO/Problem3_Add_Digits.cpp 165 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop5_BigO/Problem3_Add_Digits.py 166 | 167 | Problem 4: Find Peak Element 168 | * Leetcode Problem 162: https://leetcode.com/problems/find-peak-element/ 169 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop5_BigO/Problem4_Find_Peak_Element.cpp 170 | * Python Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop5_BigO/Problem4_Find_Peak_Element.py 171 | 172 | ## Workshop 6: Trees and Hash Tables 173 | Slides: https://docs.google.com/presentation/d/1jknXhuq3UFEp7bR-nkYsR8GbIeF1UHBmoN3rTohBTc0/edit?usp=sharing 174 | 175 | Problem 1: Sum of Left Leaves 176 | * Leetcode Problem 404: https://leetcode.com/problems/sum-of-left-leaves/ 177 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop6_Trees%20%2B%20Hash%20Tables/Problem1_Sum%20of%20Left%20Leaves_404.cpp 178 | * Python Solution:https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop6_Trees%20%2B%20Hash%20Tables/Problem1_Sum_of_Left_Leaves_404.py 179 | 180 | Problem 2: Path Sum 181 | * Leetcode Problem 112: https://leetcode.com/problems/path-sum/ 182 | * C++ Solution:https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop6_Trees%20%2B%20Hash%20Tables/Problem2_Path%20Sum_112.cpp 183 | * Python Solution:https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop6_Trees%20%2B%20Hash%20Tables/Problem2_Path_Sum_112.py 184 | 185 | Problem 3: Two Sum 186 | * Leetcode Problem 1: https://leetcode.com/problems/two-sum/ 187 | * C++ Solution:https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop6_Trees%20%2B%20Hash%20Tables/Problem3_Two%20Sum_1.cpp 188 | * Python Solution:https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop6_Trees%20%2B%20Hash%20Tables/Problem3_Two%20Sum_1.py 189 | 190 | Problem 4: Intersection of Two Arrays 191 | * Leetcode Problem 349: https://leetcode.com/problems/intersection-of-two-arrays/ 192 | * C++ Solution: https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop6_Trees%20%2B%20Hash%20Tables/Problem4_Intersection%20of%20Two%20Arrays_349.cpp 193 | * Python Solution:https://github.com/uclaacm/cs32-interview-prep20/blob/master/Workshop6_Trees%20%2B%20Hash%20Tables/Problem4_Intersection%20of%20Two%20Arrays_349.py 194 | --------------------------------------------------------------------------------