【发布时间】:2022-12-20 01:55:17
【问题描述】:
培训课程将在接下来的 10 天内进行两次。有 N 名员工(编号从 0 到 N-1)愿意参加。每个员工都提供了他们能够参加培训的未来 10 天的列表。员工偏好表示为字符串数组。 N[K] 是一个由数字 (0-9) 组成的字符串,表示第 k 名员工能够出席的天数。
需要找出在两个预定日期中的至少一天内可以参加的最大员工人数。
例如
Given E = ["039", "4", "14", "32", "", "34", "7"], the answer is 5. It can be achieved for example by running training on days 3 and 4. This way employees number 0, 1, 2, 3 and 5 will attend the training.
Given E = ["801234567", "180234567", "0", "189234567", "891234567", "98", "9"], the answer is 7. It can be achieved for example by running training on days 0 and 9. This way employees all will attend the training.
Given E = ["5421", "245", "1452", "0345", "53", "345"], the answer is 6. It can be achieved for example by running training once on day 5. This way employees all will attend the training.
这是我未能解决的测试。
我试过这个,但它只适用于 1,2 个案例。任何人都可以分享解决它的任何技巧吗?
public int solution(String[] E) {
Map<String, Integer> daysCount = new HashMap<String, Integer>();
int n = E.length;
for (int i = 0; i < n; i++) {
String inp = E[i];
for (int j = 0; j < inp.length(); j++) {
char c = inp.charAt(j);
if (daysCount.containsKey(Character.toString(c))) {
daysCount.merge(Character.toString(c), 1, Integer::sum);
}
else {
daysCount.put(Character.toString(c), 1);
}
}
}
Map<String, Integer> topTen = daysCount.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(2)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
List<String> vals = new ArrayList<String>();
topTen.entrySet().forEach(entry -> {
vals.add(entry.getKey());
});
int out = 0;
StringBuilder sb = new StringBuilder();
for (int z = 0; z < vals.size(); z++) {
sb.append(vals.get(z));
}
for (int i = 0; i < n; i++) {
String inp = E[i];
if (inp.matches(".*[" + sb.toString() + "].*")) {
out++;
}
}
return out;
}
更新
我已经实施的是,计算所有员工天数偏好中的所有天数,并在一天中进行最大计数,然后检查该天存在于多少员工天数偏好中。
【问题讨论】:
-
你能放下你的算法?如果 A) 你实际上是什么,那会更容易看清通缉实施符合要求,并且 B) 您实施的与您想要实施的相匹配。
-
@Fildor,在我的问题中提到了更新
-
采取一个简单的失败案例并开始调试。
-
@MrSmith42,但我认为我的方法是错误的。也许任何人都可以建议解决这个问题
-
这看起来可以通过动态规划进行优化。