大疆后端笔试编程题情况统计0814
两道编程题
一、求数组在k+1长度的窗口中是否存在相等的值
第一题:
class Solution {
public boolean containsNearbyDuplic(int[] nums, int k) {
int len = nums.length;
HashSet<String> memo = new HashSet<>();
for (int i = 0; i < len; i++) {
if(i>k){
memo.remove(nums[i-k-1]+",");
}
if(memo.contains(nums[i]+",")){
return true;
}
memo.add(nums[i]+",");
}
return false;
}
}
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean res;
int nums_size = 0;
nums_size = in.nextInt();
int[] nums = new int[nums_size];
for(int nums_i = 0; nums_i < nums_size; nums_i++) {
nums[nums_i] = in.nextInt();
}
if(in.hasNextLine()) {
in.nextLine();
}
int k;
k = Integer.parseInt(in.nextLine().trim());
res = new Solution().containsNearbyDuplic(nums, k);
System.out.println(res);
}
}
class Solution {
public boolean containsNearbyDuplic(int[] nums, int k) {
int len = nums.length;
HashSet<String> memo = new HashSet<>();
for (int i = 0; i < len; i++) {
if(i>k){
memo.remove(nums[i-k-1]+",");
}
if(memo.contains(nums[i]+",")){
return true;
}
memo.add(nums[i]+",");
}
return false;
}
}
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
boolean res;
int nums_size = 0;
nums_size = in.nextInt();
int[] nums = new int[nums_size];
for(int nums_i = 0; nums_i < nums_size; nums_i++) {
nums[nums_i] = in.nextInt();
}
if(in.hasNextLine()) {
in.nextLine();
}
int k;
k = Integer.parseInt(in.nextLine().trim());
res = new Solution().containsNearbyDuplic(nums, k);
System.out.println(res);
}
}
二、最长疲劳时间
class Solution2 {
/* Write Code Here */
public int longestWPI(int[] hours) {
int len = hours.length;
int[] tiredSum = new int[len+1];
int[] normalSum = new int[len+1];
for (int i = 0; i < len; i++) {
if(hours[i]>8){
tiredSum[i+1]=tiredSum[i]+1;
normalSum[i+1]=normalSum[i];
}else{
normalSum[i+1]=normalSum[i]+1;
tiredSum[i+1]=tiredSum[i];
}
}
int res = 0;
for (int i = 0; i < len; i++) {
int right = len;
int tmp = (tiredSum[right]-tiredSum[i])-(normalSum[right]-normalSum[i]);
while(tmp<=0&&right>0){
right--;
tmp = (tiredSum[right]-tiredSum[i])-(normalSum[right]-normalSum[i]);
}
res = Math.max(res,right-i);
}
return res;
}
}
public class Main2 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int res;
int hours_size = 0;
hours_size = in.nextInt();
int[] hours = new int[hours_size];
for(int hours_i = 0; hours_i < hours_size; hours_i++) {
hours[hours_i] = in.nextInt();
}
if(in.hasNextLine()) {
in.nextLine();
}
res = new Solution2().longestWPI(hours);
System.out.println(String.valueOf(res));
}
}
两道题做完发现编程只有20分,后知后觉大疆笔试成绩主要看选择题是嘛?麻了。
/* Write Code Here */
public int longestWPI(int[] hours) {
int len = hours.length;
int[] tiredSum = new int[len+1];
int[] normalSum = new int[len+1];
for (int i = 0; i < len; i++) {
if(hours[i]>8){
tiredSum[i+1]=tiredSum[i]+1;
normalSum[i+1]=normalSum[i];
}else{
normalSum[i+1]=normalSum[i]+1;
tiredSum[i+1]=tiredSum[i];
}
}
int res = 0;
for (int i = 0; i < len; i++) {
int right = len;
int tmp = (tiredSum[right]-tiredSum[i])-(normalSum[right]-normalSum[i]);
while(tmp<=0&&right>0){
right--;
tmp = (tiredSum[right]-tiredSum[i])-(normalSum[right]-normalSum[i]);
}
res = Math.max(res,right-i);
}
return res;
}
}
public class Main2 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int res;
int hours_size = 0;
hours_size = in.nextInt();
int[] hours = new int[hours_size];
for(int hours_i = 0; hours_i < hours_size; hours_i++) {
hours[hours_i] = in.nextInt();
}
if(in.hasNextLine()) {
in.nextLine();
}
res = new Solution2().longestWPI(hours);
System.out.println(String.valueOf(res));
}
}
两道题做完发现编程只有20分,后知后觉大疆笔试成绩主要看选择题是嘛?麻了。