`
superhack
  • 浏览: 32086 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

TCHS-3-250

阅读更多

Problem Statement

    

After selling goods all day, a salesman would like to determine the most desirable item in his inventory. You are given a String[] items, each element of which represents a single item that was sold during the day. Return the item that was sold the most number of times. In case of a tie, return the item that comes first alphabetically.

Definition

    
Class: BestSeller
Method: findBestSeller
Parameters: String[]
Returns: String
Method signature: String findBestSeller(String[] items)
(be sure your method is public)
    
 

Constraints

- items will contain between 1 and 50 elements, inclusive.
- Each element of items will contain between 1 and 50 characters, inclusive.
- Each element of items will contain only lowercase letters ('a'-'z').

Examples

0)  
    
{"table", "chair", "table", "table", "lamp", "door", "lamp", "table", "chair"}

Returns: "table"

The salesman sold four "table"s, two "chair"s, two "lamp"s, and one "door". The "table" is his best-selling item.
1)  
    
{"a", "a", "a", "b", "b", "b"}

Returns: "a"

There is a tie between "a" and "b", "a" is returned, because it comes first alphabetically.
2)  
    
{"icecream", "peanuts", "peanuts", "chocolate", "candy", "chocolate", "icecream", "apple"}

Returns: "chocolate"

The salesman sold two of each of these items: "icecream", "peanuts", and "chocolate". Since there is a tie, "chocolate" is returned because it comes first alphabetically. "apple" comes even earlier, but only tied items are considered.
3)  
    
{"soul"}

Returns: "soul"

 

import java.util.Arrays;

public class BestSeller {

	public static String findBestSeller(String[] items) {
		Arrays.sort(items);
		int max, maxi, count = 1;
		max = maxi = 0;
		for (int i = 1; i < items.length; i++) {
			if (!items[i].equals(items[i-1])) {
				if (count > max) {
					max = count;
					maxi = i - 1;
				}
				count = 1;
			} else
				count++;
		}
		return (count > max) ? items[items.length-1] : items[maxi];
	}

}

 

分享到:
评论

相关推荐

    javalruleetcode-Open-Source-Algorithms:快乐编码!

    TCHS-SRM-1 SRM - 算法单轮比赛 2. USACO - C++11 礼物1.cpp 骑车.cpp 测试.cpp 3.乌拉尔 - - C++11,Java 1.8 乌拉尔在线法官的可能解决方案 反向Root.cpp 总和文件 求和程序 最终排名.cpp 磁暴.cpp 磁暴.java 寂寞...

    唐亚辉文件关于算法,怎样去实现算法的

    - **应用国家和地区**:主要用于中国市场,是中国自主研发的3G标准之一。 #### CDMA2000 (码分多址2000) - **技术改进**:相较于早期的CDMA技术,CDMA2000提高了数据传输速率,并增强了系统的容量。 - **应用地区**...

    Bit Error Rate Testing with CMU300

    电路交换业务信道(TCHs)测试是BER测试的重要组成部分,主要用于评估语音和低速数据服务的质量。这一部分将详细介绍如何设置测试环境并执行测试。 #### 分组数据业务信道(PDTCHs)测试 随着数据传输速度的提高,...

    基站代维考试复习题汇总.doc

    TCHs(时隙分配)、TRXSIG(发射信号)和OMUSIG(操作维护信道)是需要配置的。 5. **电源模块**:DE34基站的公共设备直流电源由CSUA模块提供。 6. **GSM多址方式**:GSM系统采用FDMA(频分多址)和TDMA(时分多址...

    基站代维考试复习题Nokia设备.docx

    4. 配置ULTRASITE传输时,不需要配置EDAP(可能是指电子数据接入点),而需要配置TCHs(时隙信道)、TRXSIG(传输信号)和OMUSIG(操作维护信号)。 5. DE34基站的公共设备直流电源由CSUA模块提供,而非PWSB、PSUA...

    techies:Javascript 验证库。 为什么? 因为我写了一些非常糟糕的验证代码,并且想在学习的同时做一些更健壮的事情

    要告诉技术人员验证元素,请将“tchs”属性添加到元素。 &lt;input type="text" tchs=""&gt;&lt;/input&gt; 技术人员利用规则来验证元素。 验证是在每个元素的基础上完成的,并且根据所使用的元素进行不同的工作...

    TOPCODER 算法PPT1

    此外,TopCoder竞赛提供了丰富的奖金和机会,如TopCoder Open(TCO)、TopCoder Collegiate Challenge(TCCC)和TopCoder High School(TCHS)等,涵盖算法、设计、开发和组装等领域。TopCoder Studio则专注于网页...

Global site tag (gtag.js) - Google Analytics