24年9月8日 小红书数据开发笔试
前言
题目挺难,涉及hadoop、spark等内容,且计算机网络、操作系统、数据结构与算法也均涉及。
20题选择题(多选、单选均有),40分
3题编程算法题,分值分别为 15、20和25分。
惭愧,只a了最后一题算法题。
第二题看着有点复杂,时间不太够了,就一点没做
第一题
给n个正整数组成的数组,a1、a2、a3,...., an,任意 l, r (l <= r) ,al + ... + ar 即为一个区间和。
现在可以调整数组顺序,对所有区间和进行求和,返回该结果和。
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } Solution solution = new Solution(nums); System.out.println(solution.solute()); } } class Solution{ int[] nums; int[] timesArr; int n; public Solution(int[] nums) { this.nums = nums; this.n = nums.length; this.timesArr = new int[n]; setTimes(); } public int solute(){ Arrays.sort(nums); int[] locateArr = new int[n]; int left = 0; int right = n - 1; for(int i = 0; i < n; i++){ if(i % 2 == 0){ locateArr[left++] = nums[n - 1 - i]; } else { locateArr[right--] = nums[n - 1 - i]; } } int sum = 0; for (int i = 0; i < n; i++) { sum += timesArr[i] * locateArr[i]; } return sum; } public void setTimes(){ timesArr[0] = n; for(int i = 1; i < n / 2; i++){ timesArr[i] = timesArr[i - 1] + n - i * 2; } int left = 0; int right = n - 1; while(left < right){ timesArr[right--] = timesArr[left++]; } if(n % 2 != 0){ int mid = n / 2; timesArr[mid] = timesArr[mid - 1] + n - mid * 2; } } }
搞不清楚为啥只有27%哈哈哈,感觉已经想的很通透了?(自我以为)
第三题
给n个正整数组成的数组,a1、a2、a3,...., an。如果 al = ar,那么[l, r]就称为好区间。
那么,任意查询q次,每次输入两个数字分别代表 l 和 r
输出这q次查询 l 和 r 之间最长的好区间长度。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int q = in.nextInt(); int[] nums = new int[n + 1]; for (int i = 0; i < n; i++) { nums[i + 1] = in.nextInt(); } Solute solute = new Solute(nums); for (int i = 0; i < q; i++) { System.out.println(solute.getMaxGood(in.nextInt(), in.nextInt())); } } } class Solute { int[] nums; int n; int[] leftDiffArr; int[] rightDiffArr; public Solute(int[] nums) { this.nums = nums; this.n = nums.length - 1; this.leftDiffArr = new int[n + 1]; this.rightDiffArr = new int[n + 1]; leftDiffArr[1] = -1; for (int i = 2; i <= n; i++) { if (nums[i] != nums[i - 1]) { leftDiffArr[i] = i - 1; } else { leftDiffArr[i] = leftDiffArr[i - 1]; } } rightDiffArr[n] = n + 1; for (int i = n - 1; i >= 1; i--) { if (nums[i] != nums[i + 1]) { rightDiffArr[i] = i + 1; } else { rightDiffArr[i] = rightDiffArr[i + 1]; } } } public int getMaxGood(int left, int right) { if (nums[left] != nums[right]) return right - left + 1; int newLeftIndex = rightDiffArr[left]; if (newLeftIndex >= right) return 0; int newRightIndex = leftDiffArr[right]; if (right - newLeftIndex > newRightIndex - left) { return right - newLeftIndex + 1; } else { return newRightIndex - left + 1; } } }#小红书##题目描述#