【求助】PDD笔试第二题
起床晚了10:20才发现有笔试
第一题15分钟ak
第二题做了一个半小时,一开始的做法时间复杂度爆了,只过24%,改进的代码如下,样例能够全过,自己也测试了别的数据,为什么实际用例送我大0蛋啊真的红温了
package pdd_2;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main_1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int group = in.nextInt();
int[] res = new int[group];
for (int i = 0; i < group; i++) {
int count = in.nextInt();
int[] nums = new int[count];
double avg = 0;
for (int j = 0; j < count; j++) {
nums[j] = in.nextInt();
avg = avg + (double) nums[j];
}
avg = avg / count;
Arrays.sort(nums);
Set> list = new HashSet<>();
Set> have = new HashSet<>();
getRes(nums, 0, nums.length - 1, avg, list, have);
res[i] = list.size();
}
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
public static void getRes(int[] nums, int indexStart, int indexEnd, double avg, Set> list, Set> have) {
if (indexStart == nums.length || indexEnd == -1 || indexStart >= indexEnd) {
return;
}
Set set = new HashSet<>();
set.add(indexStart);
set.add(indexEnd);
if (have.contains(set)) {
return;
}
have.add(set);
if (nums[indexStart] + nums[indexEnd] == avg * 2) {
list.add(set);
int x = 1;
while (indexStart + x < indexEnd && nums[indexStart + x] == nums[indexStart]) {
x++;
}
int y = 1;
while (indexStart < indexEnd - y && nums[indexEnd - y] == nums[indexEnd]) {
y++;
}
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
Set s = new HashSet<>();
s.add(indexStart + i);
s.add(indexEnd - j);
if (s.size() == 2) {
list.add(s);
}
}
}
getRes(nums, indexStart + x, indexEnd - y, avg, list, have);
}
if (nums[indexStart] + nums[indexEnd] > avg * 2) {
getRes(nums, indexStart, indexEnd - 1, avg, list, have);
} else {
getRes(nums, indexStart + 1, indexEnd, avg, list, have);
}
}
}
第一题15分钟ak
第二题做了一个半小时,一开始的做法时间复杂度爆了,只过24%,改进的代码如下,样例能够全过,自己也测试了别的数据,为什么实际用例送我大0蛋啊真的红温了
package pdd_2;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main_1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int group = in.nextInt();
int[] res = new int[group];
for (int i = 0; i < group; i++) {
int count = in.nextInt();
int[] nums = new int[count];
double avg = 0;
for (int j = 0; j < count; j++) {
nums[j] = in.nextInt();
avg = avg + (double) nums[j];
}
avg = avg / count;
Arrays.sort(nums);
Set
Set
getRes(nums, 0, nums.length - 1, avg, list, have);
res[i] = list.size();
}
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
public static void getRes(int[] nums, int indexStart, int indexEnd, double avg, Set
if (indexStart == nums.length || indexEnd == -1 || indexStart >= indexEnd) {
return;
}
Set
set.add(indexStart);
set.add(indexEnd);
if (have.contains(set)) {
return;
}
have.add(set);
if (nums[indexStart] + nums[indexEnd] == avg * 2) {
list.add(set);
int x = 1;
while (indexStart + x < indexEnd && nums[indexStart + x] == nums[indexStart]) {
x++;
}
int y = 1;
while (indexStart < indexEnd - y && nums[indexEnd - y] == nums[indexEnd]) {
y++;
}
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
Set
s.add(indexStart + i);
s.add(indexEnd - j);
if (s.size() == 2) {
list.add(s);
}
}
}
getRes(nums, indexStart + x, indexEnd - y, avg, list, have);
}
if (nums[indexStart] + nums[indexEnd] > avg * 2) {
getRes(nums, indexStart, indexEnd - 1, avg, list, have);
} else {
getRes(nums, indexStart + 1, indexEnd, avg, list, have);
}
}
}
全部评论
有没有考虑过avg无法整除向下取整,结果后面的计算全是用的错误的avg的情况
相关推荐
拿到ssp可以么:有钱真好啊,有钱还拿到50w+真好啊,50w+还能wlb更好了,我要嫉妒到发疯了
点赞 评论 收藏
分享