Friday, July 27, 2012

1.7 matrixSetZero

Write an algorithm such that if an element in an MxN matrix is 0, its entire row and
column is set to 0

My first thought to simply iterate through the matrix, and when it hit a zero, then set the entire row/col to zero. The problem, I quickly realized, is that this would cause the algorithm to make the entire matrix set to zero, because it will keep hitting zeroes.

Therefore, you need to track the zeros separate from the actual matrix. So we need a second matrix. But do we really need a second matrix? After all, all we need to know is that if there is at least one zero in that row, not the exact positions of the zeros in the row. So, I'm going to go for 2 arrays rather than 1 whole matrix.

Thursday, July 26, 2012

1.6 rotateMatrix

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees Can you do this in place?

So the only way I can think of doing it "in place" is if you save one spot. Let me explain. If you have "ABCD" and you want to shift everything forward ONE...you save the "D', shift it to "_ABC", then insert the D. Except this is one a one dimensional array, it's a two dimensional one.

We do this in layers. Say have we have a 4x4 matrix, first we start at "layer 1", where its just the outside border. Then we do layer "2", where its the inside 4 blocks. Then we're done. So we only do N/2.

Have not doublechecked this code yet.

1.5 replaceSpace

Write a method to replace all spaces in a string with ‘%20’

Tough thing about this method will be how to replace a single char with 3 chars. We don't want to have to increase the array size everytime we hit a space because that wouldn't be very memory efficient. We want to increase the array size once and be done with it. Therefore, we want to find out how many spaces are there to begin with.

PsuedoCode


Now that we have the number of spaces, we need to increase the size of the array by 2*spaces. Next, we want to keep track of where we are in the old and new array. That way we can correctly put things from the old position to the new, adjusted position.

Lastly,we want to start from the back and move backwards. Because the back is filled with "null" chars, we won't lose any information.

Wednesday, July 25, 2012

1.3 removeDuplicates



Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer  NOTE: One or two additional variables are fine. An extra copy of the array is no

It took me a while to understand the solution to this problem, not sure why.

If we ran the word "badboy" through this code, we would expect "badoy0" to return, where 0 is the end of unique characters. "ababab" would turn into "ab0bab." This is because we cannot create another array, so we must stick with the original string size.

First, we need to change the string input into a character array.
Next, we introduce a variable "tail" which keeps track of how far we have moved in the array. Tail position will start at the beginning of the array and move forward everytime we confirm that the current letter is not a duplicate. It DOES NOT move forward if we discover that the letter is a duplicate, that way we can overwrite the duplicate in the next iteration.

Code:

void removeDuplicates(char[] str)
   int tail = 1;
   for (int i=1; i< str.length ; i++) //we start at 1 because we know the first character has to be unique
           int j;//we need j outside of the for loop, so we must declare outside
           for (j=0; j<tail; j++ )
                   if (str[i] == str[j])
                          break;
            if ( j == tail ) //this means we never break-ed
                   str[tail] = str[i]
                   tail++ //tails is only incremented if there wasn't a duplicate
   str[tail] = 0; //add 0 to end of correct letters

this runtime is actually pretty bad, O(n^2). Not sure if there is a better way to do it if you aren't allow to create another string.

You could add error checking that if the string length is less than 3, just instantly stop.

Tuesday, July 24, 2012

PIE LinkedList Methds

public Class Node{
   public Node next;
   public int data;

public Node(int d){
this.data = d;
this.next = null;
}

public Class LinkedLists{
   public Node head;

   public LinkedLists(){ //constructor
     this.next = null;
    this.data = null;
}
  public Node AddToFront(int obj){
   Node newNode = new Node(obj);
   newNode.next = head;
   return newNode; //return new head
}

public Node Find(int data){
   Node next = head;
   while (next!=null && next.data !=data){
      next = next.next;
  }
  return next;
}

}

PIE Linked Lists

C++


public class Node {
   Node* next;
   object data;
}

public class LinkedList {
   Node* head;
}

Java

public class Node {
   public Node next;
   public object data;
}
public class LinkedList{
   public Node head;
}

Ruby (not sure about this)
Node = Struct.new(:next, :data)
list = Cell.new("head",nil)


1.2 Reverse a String


Given a string "abcd", write a function that would make it "dcba"

My idea was to basically swap the first and last element, then swap 2nd and 2nd to last element, etc...

psuedocode:

string Reverse(string input_string)
  int front = 0;
  int back = input_string.Length -1
  string reverse_string = new string(input_string.Length - 1)

while front < back
    reverse_string[front] = input_string[back]
    reverse_string[back] = input_string[front]
    front++
    back--
return reverse_string