Edwin Wang's Blog

If there were no clouds, we should not enjoy the sun~~~ edwin-wang.com

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
Problem Statement for ClassScores

Problem Statement

    

A teacher has just finished grading the test papers for his class. To get an idea of how difficult the test was, he would now like to determine the most common score on the test. In statistics, this is called the "mode" of a set of data points. For instance, if the scores were {65, 70, 88, 70}, then the mode would be 70, since it appears twice while all others appear once.

Sometimes, in the case of a tie, the mode will be more than one number. For instance, if the scores were {88, 70, 65, 70, 88}, then the mode would be {70, 88}, since they both appear most frequently.

You are given a int[] scores. You are to return a int[] representing the mode of the set of scores. In the case of more than one number, they should be returned in increasing order.

 

Definition

    
Class: ClassScores
Method: findMode
Parameters: int[]
Returns: int[]
Method signature: int[] findMode(int[] scores)
(be sure your method is public)
    
 

Constraints

- scores will contain between 1 and 50 elements, inclusive.
- Each element of scores will be between 0 and 100, inclusive.
 

Examples

0)
    
{65, 70, 88, 70}
Returns: {70 }
The first example from the problem statement.
1)
    
{88, 70, 65, 70, 88}
Returns: {70, 88 }
The second example from the problem statement.
2)
    
{92, 56, 14, 73, 22, 38, 93, 45, 55}
Returns: {14, 22, 38, 45, 55, 56, 73, 92, 93 }
With no duplicates, all of the elements are the most frequent (appearing once each).

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)2005, TopCoder, Inc. All rights reserved.



 

public class ClassScores {

public int[] findMode(int[] scores) {
  
int[] count = new int[101];
  
for (int i = 0; i < scores.length; i++)
    count[scores[i]]
++;
  
for (int i = scores.length; i ≥ 1; i--{
    
int c = 0;
    
for (int j = 0; j ≤ 100; j++)
      
if (count[j] == i)
        c
++;
    
if (c > 0{
      
int p = 0;
      
int[] ret = new int[c];
      
for (int j = 0; j ≤ 100; j++)
        
if (count[j] == i) {
          ret[p] 
= j;
          p
++;
        }

      
return ret;
    }

  }

  
return new int[0];
}


}





Problem Statement for AutoLoan

Problem Statement

    

Auto dealerships frequently advertise tempting loan offers in order to make it easier for people to afford the "car of their dreams". A typical sales tactic is to show you various cars, and then talk in terms of what your monthly payment would be, to say nothing of how much you are actually paying for the car, how much interest you pay, or how long you have to make payments.

A typical auto loan is calculated using a fixed interest rate, and is set up so that you make the same monthly payment for a set period of time in order to fully pay off the balance. The balance of your loan starts out as the sticker price of the car. Each month, the monthly interest is added to your balance, and the amount of your payment is subtracted from your balance. (The payment is subtracted after the interest is added.) The monthly interest rate is 1/12 of the yearly interest rate. Thus, if your annual percentage rate is 12%, then 1% of the remaining balance would be charged as interest each month.

You have been checking out some of the cars at your local dealership, TopAuto. An excited salesman has just approached you, shouting about how you can have the car you are looking at for a payment of only monthlyPayment for only loanTerm months! You are to return a double indicating the annual percentage rate of the loan, assuming that the initial balance of the loan is price.

 

Definition

    
Class: AutoLoan
Method: interestRate
Parameters: double, double, int
Returns: double
Method signature: double interestRate(double price, double monthlyPayment, int loanTerm)
(be sure your method is public)
    
 

Notes

- Because of the way interest is compounded monthly, the actual interest accrued over the course of a year is not necessarily the same as (balance * yearly interest rate). In fact, it's usually more.
- In a real situation, information like this would typically need to be disclosed, but since you aren't at a point of signing any paperwork, the salesman has no legal obligation to tell you anything.
- The return value must be within 1e-9 absolute or relative error of the actual result.
 

Constraints

- price will be between 1 and 1000000, inclusive.
- monthlyPayment will be between 0 and price / 2, inclusive.
- loanTerm will be between 1 and 600, inclusive.
- The resulting interest rate will be between 0 and 100, inclusive.
 

Examples

0)
    
6800
100
68
Returns: 1.3322616182218813E-13
Noting that 68 payments of 100 equals the total price of 6800, so there is no interest.
1)
    
2000
510
4
Returns: 9.56205462458368
Here, we do pay a little interest. At 9.562% annual interest, that means each month we pay 0.7968% of the balance in interest. Our payment schedule looks like this:
Month | + Interest | - Payment | = Balance
------------------------------------------
      |            |           |  2000.00
   1  |     15.94  |   510.00  |  1505.94
   2  |     12.00  |   510.00  |  1007.94
   3  |      8.03  |   510.00  |   505.97
   4  |      4.03  |   510.00  |     0.00
2)
    
15000
364
48
Returns: 7.687856394581649
This is similar to what purchasing a new car with no money down might look like, if you make payments for 4 years.

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)2005, TopCoder, Inc. All rights reserved.




 

public class AutoLoan {

private double amort(double principal, double payment, int term, double interest) {
  
double m = interest / 1200;
  
if (principal * m > payment)
    
return 1;
  
for (int i = 0; i < term; i++)
    principal 
= principal * (1 + m) - payment;
  
return principal;
}


public double interestRate(double price, double monthlyPayment, int loanTerm) {
double ret = 0;
double inc = 1000000000;
while (inc ≥ 1.0E-18{
  
double d = amort(price, monthlyPayment, loanTerm, ret + inc);
  
if (d ≤ 0{
    ret 
+= inc;
  }

  inc 
/= 2.0;
}


return ret;

}


}




Problem Statement for MissileTarget

Problem Statement

    

You are working for a defense agency that is testing the accuracy of a new missile guidance system. As part of this effort, several missiles have been fired off. Each missile fired was programmed with the same target coordinates, although the actual points of impact vary.

Your task is to determine the "best fit" point to describe the location where the missiles actually landed. To determine how well a point describes the location, calculate the cartesian distance from the point to each of the landing points. Then, total the sum of the squares of these distances. The best fit point is the point that minimizes this sum.

You are given int[]s x and y, both containing the same number of elements, where the i-th element of x and the i-th element of y describe the coordinates of the i-th missile landing point. You are to return a int[] with exactly two elements, describing the coordinates of the lattice point (point with integral coordinates) that is closest to the "best fit" point. The first element should be the x-coordinate, and the second element should be the y-coordinate.

 

Definition

    
Class: MissileTarget
Method: bestFit
Parameters: int[], int[]
Returns: int[]
Method signature: int[] bestFit(int[] x, int[] y)
(be sure your method is public)
    
 

Notes

- The cartesian distance between two points (x1, y1) and (x2, y2) is defined as Sqrt((x2-x1)^2 + (y2-y1)^2).
- The return value must be within 1e-9 absolute or relative error of the actual result.
 

Constraints

- x will contain between 1 and 50 elements, inclusive.
- x and y will contain the same number of elements.
- Each element of x will be between -1000000 and 1000000, inclusive.
- Each element of y will be between -1000000 and 1000000, inclusive.
- The actual (possibly non-lattice) best fit point will be at least 1e-2 closer to the correct return value than to any other lattice point.
 

Examples

0)
    
{750, -500, -250}
{-1000, 500, 500}
Returns: {0, 0 }
These three impacts are all pretty close to the origin, and sure enough, the origin is the best fit point.
1)
    
{765}
{834}
Returns: {765, 834 }
With only one point, it is its own best fit.
2)
    
{100, 200}
{200, 400}
Returns: {150, 300 }
With only two points, the best fit is the midpoint between the two.
3)
    
{123456, -987654, 97531, -86420}
{14703, 25814, 36924, -47036}
Returns: {-213272, 7601 }
4)
    
{0, 5, 5, 6, 8, 8}
{0, 0, 0, 0, 0, 0}
Returns: {5, 0 }
In this case, notice that the actual best fit point possible is (5.333, 0). If we look at lattice points only, then our best fit is (6, 0), however, we are interested in the lattice point that is closest to the actual best fit point, so we return (5, 0).

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)2005, TopCoder, Inc. All rights reserved.



This is another problem that is fairly easily solved with a bit of grunt work to calculate out the desired values. Since we are looking for the lattice point that is closest to our best fit, our best bet is to first calculate the location of the actual best fit point (using floating point, that is), and then find the closest lattice point.

To find the best fit point, we one important observation: calculating the best fit x-coordinate and the best fit y-coordinate separately will give us our best fit point. Why? Since the scoring of a point as being best fit is based upon the sum of the squares of the distances from each of the points, we see that:

score = sum(d^2) = sum(sqrt((x - x0)^2 - (y - y0)^2)^2)
  = sum((x - x0)^2 + (y - y0)^2)
  = sum((x - x0)^2) + sum((y - y0)^2)

So, to minimize the score, it suffices to minimize each sum separately.

To minimize each sum, a ternary search works well. However, in this case, if you were inclined to do the mathematical gruntwork, then you found a nice shortcut. The average of the x-coordinates will give you the x-coordinate of the best fit point, and the same goes for the y-coordinates. (Why? Hint: Use calculus to prove where the minimum value is.)

Either way, once you have the location of the best fit point it's just simply a matter of finding the closest lattice point, and the easiest way to do this is by rounding. (Note the constraints were intended to prohibit the case where a point was equidistant from multiple lattice points.)

posted on 2005-08-12 10:31  laughterwym  阅读(550)  评论(0编辑  收藏  举报