Friday, 28 December 2018

max area of rectangle.

Look at the picture below which pretty much shows the cases you need to consider.
image

Think of the heights in terms of number of increasing and decreasing values

1 3 6 8 7 2 4 1

For each rectangle, traverse each index from left to right and calculate the maximum available only when the height goes down. Compute the maximum of all sub calculations. When the next building is taller than the previous, add it to the stack (to be processed later).

Also notice that we need to extend building 5 backthrough building 4.

How do you know to stop at building 4? Easy, the next active area (coming from building 3) has a height lower than building 5's, so that area is still active (in other words it will go through building 5).

When the next building is shorter: pop off stack until you find a starting area with a smaller height (than the current building) or you empty the stack (meaning it goes all the way back through the first building).

Now push this building's height along with it's left position onto the stack. 
When you pop off the stack, this means you've found the right side of an area, so compute its area and see if it's the maximum.

Also when you find the "left wall" for the current building (meaning you found a smaller building in the stack or went all the way back to the first building), you need to remember that position in addition to the height of the current building so that when the current building is eventually popped off of the stack, you can properly compute it's area.

Notice how building 6 extends both backwards and forwards, such that when I get to building 8, I have to know that building 6's height extended all the way back through building 2.

Password checking optimize max, min with test cases.

1. Problem + Keyword in solution
2. Pseudocode
3. Stack trace
4. Code

Problem:

Determine the minimum number of changes needed to add password secure

Secure password has:

1. at least 6 characters
2. one uppercase letter
3. one lowercase letter
4. one number
5. one special case letter "!@#$%^&*()-+"

Solution:

If there is one character that matches any of the conditions, than don't increment the counter for non matching characters

compare the non matching characters with current size of password, if password size is less than 6, than you have to add at least 6-n where n is the current size of password. Dependent on if 6-n is bigger or counter is bigger.

Think of test cases and calculate solutions to optimize solution

1. size of pwd is 0
2. size of pwd is 1 or 2
3. size of pwd is 3 but all are non matching
4. size of pwd is 4 but all are matching the special characters

the equation that you come up with

Max of (6- (size of pwd), counter for non matching characters)

Monday, 24 December 2018

Divide nd Conuer Palindrome counting


Solution: find the middle element of the string. Check to see if rule 1 n  2 applies to the string expanding from the middle element and return 1 if found or 0 if not found. then recursively call the left and right substring without the middle element and add all elements the match into the counter. return the counter

                                                         vcdcd.   ( check if d, cdc, vcdcd are valid )    
                     vc (check v and _vc are valid)                                   cd (check d, _dc are valid)
                                (check c)                                                            (check d)