Posts

Wednesday, September 27th

Image
 Tape compression Following a compression scheme for a string, where a letter is added to the string and a number signifies copies of the entire string, return the kth letter of the decoded string Brute force is a straight forward approach, but times out for larger encodings A key observation for an efficient solution is that for a string of length n multiplied any number of times, and an index k, the output letter will be the same as k%n  With this in mind, we can work backwards to find the answer To work backwards, we first find the size of the string at the end of the problem.  Iterate over the input string, if a character is a letter increase the size by 1, otherwise if it's a digit, multiply the length by the digit amount string decodeAtIndex ( string S , int K ) {           long totalsize = 0 ; int n = s . size (); for ( int i = 0 ; i < n; ++i) { if ( isdigit ( s [i])) totalsiz...

Monday, September 25th

Image
 Find the Non-Duplicate     Given 2 arrays, where 1 array is a jumble of the other plus an extra character, find the extra character Extra bubblegum, shared by Crookesmoor via Wikipedia  under the Creative Commons Attribution-Share Alike 4.0 International license.  No changes made. There is a similar problem to this (which I cannot find at the moment), except with numbers where we are to find a missing number.  A limit for that problem was no extra space. Here we are allowed extra space, so finding the difference in the 2 strings is pretty straightforward.  Still, we can do better We can utilize bitwise XOR.  Beginning with a char set equal to 0, we iterate through both arrays and XOR each character.  Whenever the same character is XOR'd twice, it is equal to zero.   Since all characters in string 1 appear in string 2, everything will XOR to zero except for the extra character in string 2, which when XOR'd will give us the character i...

Sunday, September 24th

Image
 Champagne Given a pyramid of champagne glasses and an amount poured into the top glass, determine how much champagne will be in a given glass in the pyramid This problem can be solved in a straightforward manner, where, for a given pour we will determine the amount of champagne in every glass in the pyramid , then return the results for the given glasses.  There are only 100 rows total so it is feasible. To begin, we initialize a 2D-array which will represent the pyramid.  We also add the total amount poured to the top glass double champagneTower ( int poured , int query_row , int query_glass ) {      vector<vector< double > > res ( 101 , vector < double >( 101 , 0.00 )); res [ 0 ][ 0 ] = poured;          ..     } Then we iterate from the first row down, and by the left glass to the right glass for ( int i = 0 ; i < 100 ; i++) { for ( int i = 0 ; i < 10...

Wednesday, September 20th

Image
 Reduce X to Zero Given an integer array and value x, use elements at the right and left of the array to reduce the x value to zero A collection of windows, by Daderot .  Shared under the Creative Commons Attribution-Share Alike 3.0 Unported License .  No changes made A key observation is that if elements at the ends of the array are used to sum up to a target value x , then elements in a contiguous subarray will equal the total_sum - x .  So we will look for the largest window equal to  total_sum - x Since all elements in the array are positive, then we can solve the problem using 2 pointers that form a sliding window. The general idea is to begin 2 pointers right and left at index 0.  Continue incrementing the right index by 1, adding up the elements it passes into the current_window  sum.  If the sum equals the target value, update the length.  Otherwise, if the sum is greater than the target sum, increment the left index until the window...

Tuesday, September 19th

Image
 Find the duplicate number Given an array of integers of length n+1, which contains numbers within the range [1,n], and contains on element that is duplicated at least once, find the duplicate element , considering use of only constant space and no changes to the array "How They Met Themselves" by Dante Rossetti, depicting doppelgängers The problem of finding duplicates is made difficult by the constraints of no modifications to the original array, and use of only constant extra space.  Still, despite these constraints there are still several ways to solve the problem.   Since yesterday's solution involved binary search, we will hone our BS skills by applying it to today's problem The general idea is based on the observation that without duplicates, the number of elements less than or equal an element is equal to the element itself.  E.g., in the collection {1,2,3,4}, the number of elements less than or equal to 2 is 2, less than or equal to 3 is 3, etc.   Howe...

Wednesday, September 13th

Image
 Candy Distribution Given a number of children, and a rating array that values each student, distribute the candy such every student gets at least 1 candy, and a child rated higher than their neighbors receives more candy HARIBO gummy bears, by Thomas Rosenau via Wikipedia .  Shared under the Creative Commons Attribution-Share Alike 2.5 Generic license.  No changes made. The idea is to keep an array that stores the number of candies each chid gets.  To populate the array, we make 2 passes over the ratings array, a pass in each direction, left-to-right and right-to-left First we initialize an array to store the candy distributions.  Since every kid gets at least 1 candy so we initialize to 1 vector< int > candies (n, 1 ); Then we make a pass of the ratings array left to right.  If a child has a rating higher than the neighbor to the left, set the child's rating to the left neighbor +1 for ( int i= 1 ; i<n; i++) { if ( ratings [i...

Monday, September 11th

Image
 Groups There are a given number of people, each of whom is assigned to a group.  The number identifier of the group is also its size.  The size of a group may be a multiple of its size, e.g. a group of 3 may have 6 people.  Return the  collection of all groups . Team USA Softball (a group).  Shared by USASoftball , via Wikipedia , distributed under the Creative Commons Attribution-Share Alike 3.0 Unported license.  No changes made. This problem is pretty straightforward.  The idea is to create a map (or a vector) keyed by the group, storing an array of individuals belonging to the group. First we create the vector-of-vectors to store our solution, then we initialize a vector of vectors that will store individuals according to their group numbers.  The size of the solution vector is n+1, because contrary to what the beginning of the problem statement said, the indices/group sizes go from 1 to n, according to the stated constraints. vect...