Tuesday, October 23, 2012

Chocolate Bar


This problem is from TopCoder SRM 556 - Div 2 - 250 ptr.

Problem Statement

     You just bought a very delicious chocolate bar from a local store. This chocolate bar consists of N squares, numbered 0 through N-1. All the squares are arranged in a single row. There is a letter carved on each square. You are given a string letters. The i-th character of letters denotes the letter carved on the i-th square (both indices are 0-based).

You want to share this delicious chocolate bar with your best friend. At first, you want to give him the whole bar, but then you remembered that your friend only likes a chocolate bar without repeated letters. Therefore, you want to remove zero or more squares from the beginning of the bar, and then zero or more squares from the end of the bar, in such way that the remaining bar will contain no repeated letters.

Return the maximum possible length of the remaining chocolate bar that contains no repeated letters. 

Examples

0)
    
"srm"
Returns: 3
You can give the whole chocolate bar as it contains no repeated letters.
1)
    
"dengklek"
Returns: 6
Remove two squares from the end of the bar.
2)
    
"haha"
Returns: 2
There are three possible ways:
  • Remove two squares from the beginning of the bar.
  • Remove two squares from the end of the bar.
  • Remove one square from the beginning of the bar and one square from the end of the bar.
3)
    
"www"
Returns: 1
4)
    
"thisisansrmbeforetopcoderopenfinals"
Returns: 9
 
My Solution :-
 
using System;
using System.Collections;
using System.Collections.Generic;
public class ChocolateBar
{
    public int maxLength(string letters)
    {
        int max = 0;
        for (int i = 0; i <= letters.Length - 1; i++)
        {
            for (int j = i; j <= letters.Length - 1; j++)
            {
                int c = GetCount(letters, i, j);
                if (c > max)
                    max = c;
            }
        }
        return max;
    }

    int GetCount(string letters, int strIdx, int lstIdx)
    {
        Dictionary<char, int> letterCount = new Dictionary<char, int>();
        for (int i = strIdx; i <= lstIdx; i++)
        {
            if (letterCount.ContainsKey(letters[i]))
            {
                return 0;
            }
            letterCount[letters[i]] = 1;
        }
        return lstIdx - strIdx + 1;
    }
} 
 

Monday, October 22, 2012

Great Fairy War

This problem is from TopCoder SRM 557 - Div 2 - 250 ptr.

Problem Statement

     You are a wizard. You are facing N fairies, numbered 0 through N-1. Your goal is to defeat all of them. You can only attack one fairy at a time. Moreover, you must fight the fairies in order: you can only attack fairy X+1 after you defeat fairy X. On the other hand, all fairies that have not been defeated yet will attack you all the time.


Each fairy has two characteristics: her damage per second (dps) and her amount of hit points. Your damage per second is 1. That is, you are able to reduce an opponent's hit points by 1 each second. In other words, if a fairy has H hit points, it takes you H seconds to defeat her.


You are given two vector <int>s, each of length N: dps and hp. For each i, dps[i] is the damage per second of fairy i, and hp[i] is her initial amount of hit points. We assume that your number of hit points is sufficiently large to avoid defeat when fighting the fairies. Compute and return the total number of hit points you'll lose during the fight. In other words, return the total amount of damage the attacking fairies will deal. 

Notes

- Damage per second (dps) of a person P is the rate at which P can deal damage to their opponents.

Constraints

- dps will contain between 1 and 30 elements, inclusive.
- dps and hp will contain the same number of elements.
- Each element in dps will be between 1 and 30, inclusive.
- Each element in hp will be between 1 and 30, inclusive.

Examples

0)
    
{1,2}
{3,4}
Returns: 17
It will take you 3 seconds to defeat fairy 0 and then 4 seconds to defeat fairy 1. During the first 3 seconds both fairies attack you, dealing 1+2 = 3 damage each second. During the next 4 seconds fairy 1 continues to attack you, dealing 2 damage each second. The total number of hit points you lose is 3*(1+2) + 4*2 = 9 + 8 = 17.
1)
    
{1,1,1,1,1,1,1,1,1,1}
{1,1,1,1,1,1,1,1,1,1}
Returns: 55
The answer is 10+9+...+3+2+1 = 55.
2)
    
{20,12,10,10,23,10}
{5,7,7,5,7,7}
Returns: 1767
3)
    
{5,7,7,5,7,7}
{20,12,10,10,23,10}
Returns: 1998
4)
    
{30,2,7,4,7,8,21,14,19,12}
{2,27,18,19,14,8,25,13,21,30}
Returns: 11029
5)
    
{1}
{1}
Returns: 1

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved

My Solution :-

using System;
using System.Collections;
public class GreatFairyWar
{
    public int minHP(int[] dps, int[] hp)
    {
        int tot = 0;
        for (int i = 0; i < dps.Length; i++)
            tot += dps[i];
        int ret = 0;
        for (int i = 0; i < hp.Length; i++)
        {
            ret += tot * hp[i];
            tot -= dps[i];
        }

        return ret;
    }
}


Tuesday, October 9, 2012

elementFromPoint DOM Method :-

elementFromPoint() is a method on "document"object in HTML.
It returns the HTML element at the given X & Y  positions.

For Example :-

1) Position a html element say a textarea at (10,10) as follows :-

<textarea style="position:absolute;left:10px;top:10px">

Following script will show aleart as follows:-

 var ele = document.elementFromPoint(11,11);
 alert(ele)