[虚拟机OA]The Perfect Team 完美团队

The School of Languages and Science teaches five subjects: Physics, Chemistry, Math, Botany, and Zoology. Each student is skilled in one subject. The skills of the students are described by string of named skills that consists of the letters p, c, m, b, and z only. Each character describes the skill of a student as follows:

• p —> Physics.
• c —> Chemistry.
• m -> Math.
• b —> Botany.
• z —> Zoology.

Your task is to determine the total number of different teams satisfying the following constraints:
• A team consists of a group of exactly five students.

• Each student is skilled in a different subject.

• A student may only be on one team.


For instance, if the skills string is "pcmbzpcmbz" then there are two possible teams that can be formed at one time: skills[0-4] and skills[5-9] for example. It is not important to determine permutations as we will always be limited to two teams given 10 students.


Function Description

Complete the function differentTeams in the editor below. The function must return an integer value representing the number of teams that can be formed given the

constraints.

differentTeams has the following parameter(s):

skills : a string where each position represents the skill of a student 

题意:

给定一个字符串s = "pcmbzpcmbz", 每个字符想象为一个课代表。
一个球队必须有5个人, 且5个人是不同科目的课代表。返回可以组成球队的个数。

思路:

用map记录每个char出现频率。
扫一遍map,频率最低的字符个数,决定了可以组成球队的个数。

代码:

 1     public int differentTeams(String skills) {
 2         // corner case
 3         if (skills == null || skills.length() == 0 || skills.length() % 5 != 0) return 0;
 4         int[] map = new int[26];
 5         for (char c : skills.toCharArray()) {
 6             map[c - 'a']++;
 7         }
 8         int result = Integer.MAX_VALUE;
 9         for (char c : skills.toCharArray()) {
10             result = Math.min(result, map[c - 'a']);
11 
12         }
13         return result;
14     }
15 
16     /* ---------------TEST CASES --------------- */
17     public static void main(String[] args) {
18         ThePerfectTeam test = new ThePerfectTeam();
19         System.out.println(test.differentTeams("pcmbzpcmbz"));
20     }

猜你喜欢

转载自www.cnblogs.com/liuliu5151/p/11508028.html