美团8.13笔试代码
package meituan.bishi.one; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int t = sc.nextInt(); int[] ans = new int[n]; for (int i = 0;i < n;i++){ ans[i] = sc.nextInt(); } Arrays.sort(ans); int sum = 0; int time = 0; for (int i = 0;i < n;i++){ if(time + t > ans[i]){ sum++; }else{ time += t; } } System.out.println(sum); } }
package meituan.bishi.two; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[][] res = new int[n][m]; int len = sc.nextInt(); String sb = sc.next(); int num = 0; for (int i = 0;i < n;i++){ for (int j = 0;j < m;j++){ res[i][j] = 1; } } int row = 0; int col = 0; res[0][0] = 0; num++; int cnt = 0; for (char c : sb.toCharArray()){ if(c=='W'){ row--; cnt++; if(res[row][col]==1){ res[row][col]=0; num++; if (num==n*m){ System.out.println("Yes"); System.out.println(cnt); } } } if(c=='A'){ col--; cnt++; if(res[row][col]==1){ res[row][col]=0; num++; if (num==n*m){ System.out.println("Yes"); System.out.println(cnt); } } } if(c=='S'){ row++; cnt++; if(res[row][col]==1){ res[row][col]=0; num++; if (num==n*m){ System.out.println("Yes"); System.out.println(cnt); } } } if(c=='D'){ col++; cnt++; if(res[row][col]==1){ res[row][col]=0; num++; if (num==n*m){ System.out.println("Yes"); System.out.println(cnt); } } } } if (num<n*m){ System.out.println("No"); System.out.println(n*m-num); } } }
package meituan.bishi.three; import java.util.Deque; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] dianshu = new int[n]; for(int i = 0;i < n;i++){ dianshu[i] = sc.nextInt(); } Deque<Integer> deque = new LinkedList<>(); for (int i = n - 1;i >= 0;i--){ if(deque.isEmpty() || deque.size()==1){ deque.addFirst(dianshu[i]); }else{ deque.addFirst(dianshu[i]); for (int j = 0;j < 2;j++){ int temp = deque.removeLast(); deque.addFirst(temp); } } } for (Integer integer : deque){ System.out.print(integer + " "); } } }
package meituan.bishi.four; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] res = new int[n]; for(int i = 0;i < n;i++){ res[i] = sc.nextInt(); } Map<Integer, List<Integer>> map = new HashMap<>(); for (int i = 0;i < n;i++){ if (map.containsKey(res[i])){ map.get(res[i]).add(i); }else{ map.put(res[i],new ArrayList<>()); map.get(res[i]).add(i); } } int sum = 0; for (int i = 0;i < n;i++){ for (int j = i + 1;j < n;j++){ if (map.containsKey(3*res[j]-res[i])){ for (Integer integer : map.get(3*res[j]-res[i])){ if (integer > j){ sum++; } } } } } System.out.println(sum); } }
package meituan.bishi.five; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] tree = new int[n+1]; for (int i = 1;i < n+1;i++){ tree[i] = sc.nextInt(); } Main main = new Main(); long ans = main.dfs(1,n,tree); System.out.println(ans); } long dfs(int root,int n,int[] tree){ if (root > n){ return 0; } return tree[root] + Math.max(dfs(2*root,n,tree),dfs(2*root+1,n,tree)); } }
#美团##美团笔试##秋招进度交流#