0919小红书后端开发笔试情况及代码
选择有点恶心,考C++、python、go、java。挺无语的。
不过编程比较简单,等考试结束再补充一下代码。
第一题
import java.util.Scanner; public class Q1{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a1 = sc.nextInt(); int a2 = sc.nextInt(); int a3 = sc.nextInt(); int a4 = sc.nextInt(); int x = sc.nextInt(); int validA1 = 0, validA2 = 0, validA3 = 0, validA4 = 0; for (int i = 0; i < a1; i++) { int score = sc.nextInt(); if(score>x) validA1++; } for (int i = 0; i < a2; i++) { int score = sc.nextInt(); if(score>x) validA2++; } for (int i = 0; i < a3; i++) { int score = sc.nextInt(); if(score>x) validA3++; } for (int i = 0; i < a4; i++) { int score = sc.nextInt(); if(score>x) validA4++; } System.out.println(Math.min(Math.min(validA1,validA2),Math.min(validA3,validA4))); } }
第二题
import java.util.Scanner; public class Q2{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int dp[] = new int[n]; for(int i = 1; i < n; i++) { dp[i] = Integer.MAX_VALUE; for (int j = i-1; j >= i-k && j>=0; j--) { if(arr[j]>arr[i]) { dp[i] = Math.min(dp[i], dp[j]); } else { dp[i] = Math.min(dp[i], dp[j]+arr[i]-arr[j]); } } } System.out.println(dp[n-1]); } }
第三题
import java.util.Scanner; public class Q3{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int nums[] = new int[n]; for (int i = 0; i < n; i++) { nums[i] = sc.nextInt(); } int cnts[] = new int[100001]; int kcnts = 0; int left = 0, right = 0; int ans = 0; while(left < n) { while(right < n && kcnts==0) { cnts[nums[right]]++; if(cnts[nums[right]]>=k) { kcnts++; } right++; } if(kcnts == 0) { break; } //记录答案 ans += (n-right+1); if(cnts[nums[left]]==k) { kcnts--; } cnts[nums[left++]]--; } System.out.println(ans); } }
最后祝大家offer多多~