Monday, March 28, 2016

Tower of Hanoi

import java.util.Stack;

public class TowerOfHanoi {

static int parseIndex = 1;

public static void main(String[] args) {
Stack<Integer> sourceTower = new Stack<>();
Stack<Integer> destinationTower = new Stack<>();
Stack<Integer> bufferTower = new Stack<>();
int n = 3;

for (int index = n; index > 0; index--)
sourceTower.add(index);

System.out.println("---before moving---");
System.out.println("sourceTower");
printStack(sourceTower);
System.out.println("destinationTower");
printStack(destinationTower);

moveDisks(n, sourceTower, destinationTower, bufferTower);

System.out.println("---after moving---");
System.out.println("sourceTower");
printStack(sourceTower);
System.out.println("destinationTower");
printStack(destinationTower);
}

public static void printStack(Stack<Integer> sourceTower) {
System.out.println(sourceTower.toString());
}

private static void moveDisks(int n, Stack<Integer> sourceTower, Stack<Integer> destinationTower,
Stack<Integer> bufferTower) {
if (n <= 0)
return;

moveDisks(n - 1, sourceTower, bufferTower, destinationTower);
System.out.println("----parse-----" + parseIndex++);
printStack(sourceTower);
printStack(destinationTower);
printStack(bufferTower);
moveTopDisc(sourceTower, destinationTower);
moveDisks(n - 1, bufferTower, destinationTower, sourceTower);
}

private static void moveTopDisc(Stack<Integer> sourceTower, Stack<Integer> destinationTower) {
int lastItem = sourceTower.pop();
destinationTower.add(lastItem);
}
}

Remove Duplicates from LinkedList in-place.


public class RemoveDuplicatesFromLinkedList {

public static void main(String[] args) {
Node head = new Node(0);
append(1, head);
append(2, head);
append(2, head);
append(3, head);
append(4, head);
append(4, head);
append(4, head);
append(5, head);
append(5, head);
append(1, head);

System.out.println("input");
printLinkedList(head);

removeDuplicates(head);

System.out.println("output");
printLinkedList(head);
}

public static void printLinkedList(Node head) {
Node tempHead = head;
while (tempHead != null) {
System.out.println(tempHead.data);
tempHead = tempHead.next;
}
}

public static void append(int item, Node head) {
Node newNode = new Node(item);
if (head == null) {
head = newNode;
return;
}
Node tempHead = head;
while (tempHead.next != null) {
tempHead = tempHead.next;
}
tempHead.next = newNode;
}


public static void removeDuplicates(Node head) {
if (head == null)
return;
Node previous = head;
Node current = previous.next;
while (current != null) {
Node runner = head;
while (runner != current) { // Check for earlier dups
if (runner.data == current.data) {
Node tmp = current.next; // remove current
previous.next = tmp;
current = tmp; // update current to next node
break; // all other dups have already been removed
}
runner = runner.next;
}
if (runner == current) { // current not updated - update now
previous = current;
current = current.next;
}
}
}

}

Sunday, March 27, 2016

Remove duplicates in a String -- perform in-place replacement


public class RemoveDuplicateCharsInString {

     public static void main (String[] args)
     {
          char[] input1 ="aabbcccdddd".toCharArray();
          char[] input2 ="abbcddaabbcc".toCharArray();
         
          removeDuplicateCharsInString(input1);
          removeDuplicateCharsInString(input2);
         
          System.out.println(input1);
          System.out.println(input2);
     }
     public static void removeDuplicateCharsInString(char[] inputArray)
     {
          if(inputArray == null)
               return;
         
          if(inputArray.length < 2)
               return;

          int originalArrayLength = inputArray.length;
         
          //assumed that we leave the first item in array as is.
          int newDuplicateRemovedArrayTail = 1;
         
          for(int i=1; i < inputArray.length; i++)
          {
               int j = 0;
               for(; j< newDuplicateRemovedArrayTail; j++)
               {
                     if( inputArray[j] == inputArray[i])
                     {
                          // the current char is exists in the new non-duplicate array. so Skip it
                          break;
                     }
               }
               if(j == newDuplicateRemovedArrayTail )
               {
                     //means the above search found no matching in the non-duplicate array.
                     inputArray[newDuplicateRemovedArrayTail++]= inputArray[i];        
               }
          }
          for(int k = newDuplicateRemovedArrayTail ; k < originalArrayLength; k++)
          {   
               //nullify remianing chars.
               inputArray[k]=0;
          }
     }
    
}

Tuesday, March 22, 2016

Find best profitable stock sale


Write an efficient function that takes stock Prices Yesterday and returns the best profit I could have made from 1 purchase and 1 sale of 1 ABC stock yesterday.

The indices are the time in minutes past trade opening time, which was 9:30am local time.
The values are the price in dollars of ABC stock at that time.
So if the stock cost $100 at 10:30am, stockPricesYesterday[60] = 100.

Solution#1:
public class Solution {
    public static int getMaxProfit (int[] yesterdayPrices) {
        int[] bestDeal= new int[]{-1,-1};
        int bestDealPrice =0;
        for(int i =0; i++; i <= yesterdayPrices.length - 1)
        {
            for (int j= i+1; j++; j <= yesterdayPrices.length)
            {
                if(bestDealPrice < (a[i]-a[j]))
                {
                    bestDeal[0]=i;
                    bestDeal[1]=j;
                    bestDealPrice=(a[i]-a[j]);
                }
            }
        }
        return bestDealPrice;
    }
    public static void main(String[] args) {
        // run your function through some test cases here
        // remember: debugging is half the battle!
        int[] stockPricesYesterday = new int[]{12, 6, 8, 4, 11, 9};
        System.out.println(getMaxProfit(stockPricesYesterday));
    }
}

Solution#2:
    public static int getMaxProfit (int[] yesterdayPrices) {
    int minAvailablePrice = yesterdayPrices[0];
    int maximumProfit = yesterdayPrices[1] - yesterdayPrices[0];

    for (int i = 1; i < stockPricesYesterday.length; i++) {
        int currentPrice = yesterdayPrices[i];

        int potentialProfit = currentPrice - minPrice;

        maximumProfit = Math.max(maximumProfit, potentialProfit);
        minAvailablePrice = Math.min(minAvailablePrice, currentPrice);
    }

    return maximumProfit;
}

Sunday, January 5, 2014

Adding Android Tools path to Mac Path permanently.


echo "export PATH=\"<SDK direcotory path>/sdk/tools:$PATH\"" > .bash_profile


echo "export PATH=\"/Android/adt-bundle-mac-x86_64-20130729/sdk/tools:$PATH\"" > .bash_profile
echo "export PATH=\"/Android/adt-bundle-mac-x86_64-20130729/sdk/platform-tools:$PATH\"" > .bash_profile

Monday, December 30, 2013

Mount rooted Android device system folder with read and write permission (rw)

mount -o remount,rw -t yaffs2  /system

or

mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system


We have to make sure we are having su permission before executing this command.

$adb shell
$su
$mount -o remount,rw -t yaffs2  /system
$<do what ever you want with super user permission>