TopCoder 250 points 20-SRM 153 DIV 2 216.58/250 86.63%

Problem Statement

 When selling goods, it is important to know exactly how much it costs to acquire each item. A number of distributed costs, such as marketing often make this difficult, but not impossible. If a business can figure out how much an item costs, with some accuracy, then it can easily calculate the profit margins for the item. This information, combined with sales figures, can be used to determine which items are the most important to a business. In this problem you will be given the costs, prices, and number of sales for a number of items. Each element of costs represents the total costs accrued from selling a single item. The corresponding elements (ones with the same index) of prices and sales represent the prices at which single items are sold, and the number of sales of each item that have occurred in some time period, respectively. You are to return the name of the item (the corresponding element of items) that provides the business with the most profits. If there is a tie for the most profitable item, return the one that comes earliest in items (lowest index). If no item provides the business with positive profits you should return the empty String.

Definition

 
Class:MostProfitable
Method:bestItem
Parameters:int[], int[], int[], String[]
Returns:String
Method signature:String bestItem(int[] costs, int[] prices, int[] sales, String[] items)
(be sure your method is public)
 
 

Constraints

-costs, prices, sales, and items will all contain the same number of elements.
-costs, prices, sales, and items will contain between 1 and 50 elements, inclusive.
-Each element of costs and prices will be between 1 and 1,000,000, inclusive.
-Each element of sales will be between 0 and 1,000, inclusive.
-Each element of items will have between 1 and 50 characters, inclusive.
-Each character of each element of items will have ASCII value between 32 and 126, inclusive. (All of the characters that can be easily made with a regular keyboard.)

Examples

0) 
 
{100,120,150,1000}
{110,110,200,2000}
{20,100,50,3}
{"Video Card","256M Mem","CPU/Mobo combo","Complete machine"}
Returns: "Complete machine"
This business makes 200 on video cards, loses 1000 on memory, makes 2500 on CPU/Mobo combos, and makes 3000 on complete machines. Since 3000 is the highest, you should return "Complete machine".
1) 
 
{100}
{100}
{134}
{"Service, at cost"}
Returns: ""
The only product given is sold at cost, so there are no items which give positive profit.
2) 
 
{38,24}
{37,23}
{1000,643}
{"Letter","Postcard"}
Returns: ""
 
3) 
 
{10,10}
{11,12}
{2,1}
{"A","B"}
Returns: "A"
 

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.     

public class MostProfitable {



	public  String bestItem(int[] costs, int[] prices, int[] sales,
			String[] items) {
		int len = costs.length;
		int sum = 0;
		int tmp = 0;
		int index = -1;
		for (int i = 0; i < len; i++) {
			if (prices[i] <= costs[i])
				continue;
			else {
				tmp = (sales[i] * (prices[i] - costs[i]));
				if (tmp > sum) {
					sum = tmp;
					index = i;
				}
			}
		}
		if (index == -1)
			return "";
		else
			return items[index];

	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值