Skip to content

Commit

Permalink
DynamicProgramming Level1 added
Browse files Browse the repository at this point in the history
  • Loading branch information
ArunrajaShanmugavel committed Jun 7, 2012
1 parent 4ca7592 commit a885081
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 40 deletions.
Binary file modified bin/Topcoder.class
Binary file not shown.
156 changes: 116 additions & 40 deletions src/Topcoder.java
@@ -1,3 +1,6 @@
import java.util.Arrays;
import java.util.HashSet;


public class Topcoder {

Expand All @@ -6,7 +9,13 @@ public static void main(String[] s)
{
Topcoder tc = new Topcoder();
//System.out.println("Will all but 1st params add to 1st param-"+tc.buy(10,2,41,2,2));
tc.checkCharMatch();
//tc.checkCharMatch();
//System.out.println(tc.minDucks(new int[]{9,3,6,4}));
//System.out.println(tc.getMinimum("o??x",9,4));
//System.out.println(tc.train(new int[]{5,5}));
//System.out.println(Arrays.toString(tc.find(100000,new int[]{100000})));
System.out.println(tc.maxTurns(new int[] {55, 52, 61, 204, 207, 54, 60, 202, 57, 58, 53, 210, 51, 59, 209, 205, 208, 201, 206, 211, 203, 56}));

}

// recursion
Expand All @@ -20,7 +29,7 @@ public String buy(int price, int b1, int b2, int b3, int b4)
int sum=0;
for(int j=0;j<4;j++)
{
if((i & (1<<j))!=0) // determine add list based on true bits
if((i & (1<<j))!=0) // determine add value of array based on true bits of i index
{
sum += b[j];
}
Expand All @@ -47,9 +56,7 @@ int[] derSeq(int[] a,int n)
public int numTrucks(int numCrates, int loadSize)
{
if (numCrates <= loadSize)
{
return 1;
}
else
{
if(numCrates % 2 == 0)
Expand All @@ -59,61 +66,130 @@ public int numTrucks(int numCrates, int loadSize)
}
}

// return max of n after k swaps of elements
public int findMax(int n,int k)
//Greedy L1 problems

// pronounce numbers from A to B sequentially - Find: minimal numbers missed
int minDucks(int[] ducks)
{
if(k==0) return n;
else if (n==-1) return -1;
else
int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE;
for(int a : ducks)
{
return findMax (swapToPossibleMax(n),k-1);
if(a<min)
min = a;

if(a>max)
max = a;
}

return (max - (min-1)) - ducks.length; //http://community.topcoder.com/stat?c=problem_statement&pm=11631
}

int swapToPossibleMax(int n)
// Sorted Array Create from unsorted array by moving one value at a time - Find : Number of moves required on optimal solution for Worst case
int solve(int[] T)
{
char[] c = Integer.toString(n).toCharArray();
int m_idx = -1;
char max=0;

if(c.length==1)
return -1;
int moves = 0;
for(int i=0;i<(T.length/2);i++) // note: half the array comparison is enough
{
moves += T[T.length-i-1] - T[i];
}
return moves; // http://community.topcoder.com/stat?c=problem_statement&pm=11336
}

// Fill '?' in string with 'o' or 'x' to make palindrome with minCost
int getMinimum(String s, int oCost, int xCost)
{
char[] cArr = s.toCharArray();
int cost = 0;
int l = cArr.length;

//choosing the max elem other than first one
for(int i=1;i<c.length;i++)
for(int i=0;i<l/2;i++)
{
if(c[i]>max)
if (cArr[i] == '?' && cArr[l-i-1]=='?')
{
max = c[i];
m_idx = i;
cost += 2* ((oCost<xCost)?oCost:xCost);
}
else if(cArr[i] == '?' || cArr[l-i-1]=='?' )
{
cost += cArr[l-i-1]=='x'?xCost:oCost;
}
else if(cArr[i] != cArr[l-i-1])
return -1;
}

if(max > c[0])
if((l&1)==1)
{

// swap 1 st and max elem
char tc = c[0];
c[0] = c[m_idx];
c[m_idx] = tc;

cost += (oCost<xCost)?oCost:xCost;
}
return cost; //http://community.topcoder.com/stat?c=problem_statement&pm=11727
}

// Swap elements of the string such that - http://apps.topcoder.com/wiki/display/tc/SRM+521 http://community.topcoder.com/stat?c=problem_statement&pm=11567


// increase elems to common value
int train(int[] attributes)
{
int max = Integer.MIN_VALUE , difference = 0;
for(int i=0;i<attributes.length;i++)
{
max = attributes[i]>max ? attributes[i] : max;
}
else
System.out.println("max - "+max);

for(int i=0;i<attributes.length;i++)
{
// swap final 2 elems
char tc = c[c.length-2];
c[c.length-2] = c[c.length-1];
c[c.length-1] = tc;
difference += max - attributes[i];
}

if( c[0]==0)
return -1;
return difference; //http://community.topcoder.com/stat?c=problem_statement&pm=11279
}

// Easy http://community.topcoder.com/stat?c=problem_statement&pm=11151

// TheProgrammingContestDivTwo TotalProbTime T - requiredTime - T1,T2.. compute penalty(total time spent) & points (number of probs solved)
int[] find(int T, int[] requiredTime)
{
Arrays.sort(requiredTime); // Optimization to pick smallest ones first

int penalty = 0, points = 0, exp_time = 0;

return Integer.parseInt(s);
for(int i=0;i<requiredTime.length;i++)
{
//rem_time -= requiredTime[i];

if ((exp_time +requiredTime[i])<=T)
{
exp_time +=requiredTime[i] ; penalty += exp_time ; points ++;
}
}
return new int[]{points,penalty}; //http://community.topcoder.com/stat?c=problem_statement&pm=11358
}

public void checkCharMatch(){
System.out.println('1'+1);
/**
*
* Dynamic programming L1 problems
*
* learning - range & sequences problem
*
*/
int maxTurns(int[] cards)
{
Arrays.sort(cards);
int turns =0,i=0;

for(i=0;i<cards.length-1;i++)
{
if(cards[i]!=0)
turns++;

if(cards[i]+1==cards[i+1])
cards[i+1]=0;
}

if(cards[i]!=0)
turns++;

return turns; //http://community.topcoder.com/stat?c=problem_statement&pm=11341
}

}

0 comments on commit a885081

Please sign in to comment.