前言
华为三道题,100+200+300,100及格,大家做对第一题就好了,祝大家全都有心仪的offer,不要慌,不要焦虑
一、完美排列——玩具(全A)(注意:题目中说:如果不是完美排列,则输出0,没注意这种情况的应该A0.6或0.7)
代码:暴力就完事了
package huawei0909;import java.util.Scanner;/*** Created by IntelliJ IDEA.** @Author: * @Email: * @Date: 2020/9/9* @Time: 19:04* @Version: 1.0* @Description: Description*/
public class First {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int K = sc.nextInt();int[] perArr = new int[K];int[] perArr1 = new int[K];for (int i = 0; i < K; i++)perArr[i] = sc.nextInt();for (int i = 0; i < K; i++)perArr1[i] = sc.nextInt();int n = sc.nextInt();int[] arr = new int[n];int[] arr1 = new int[n];for (int i = 0; i < n; i++)arr[i] = sc.nextInt();for (int i = 0; i < n; i++)arr1[i] = sc.nextInt();sc.close();/*if (n<K){ //必须判断,不然A0.6或0.7,因为题目中说:如果不是完美排列,则输出0,详情看下面的System.out.println(i + 1);System.out.println(0);return;}*/for (int i = 0; i < n; i++) {if (arr[i] == perArr[0] && arr1[i] == perArr1[0] && i + K - 1 < n && arr[i + K - 1] == perArr[K - 1] && arr1[i + K - 1] == perArr1[K - 1]) {boolean flag = true;int index = i;for (int j = 1; j < K - 1; j++) {index++;if (!(arr[index] == perArr[j] && arr1[index] == perArr1[j])) {flag = false;break;}}if (flag) { //输出可能为0,如果没考虑到,则A0.6或0.7,因为题目中说:如果不是完美排列,则输出0System.out.println(i + 1);return;}}}System.out.println(0); //必须有,不然A0.6或0.7,因为题目中说:如果不是完美排列,则输出0,详情看下面的System.out.println(i + 1);}
}
二、最长的水沟(全A)
package huawei0909;import java.util.Scanner;/*** Created by IntelliJ IDEA.** @Author: * @Email: * @Date: 2020/9/9* @Time: 19:36* @Version: 1.0* @Description: Description*/
public class Second {public static int[][] matrix;public static int[][] dp;public static int[][] k = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};public static int n, m, ans;public static void main(String[] args) {Scanner sc = new Scanner(System.in);n = sc.nextInt();m = sc.nextInt();matrix = new int[n + 1][m + 1];dp = new int[n + 1][m + 1];for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)matrix[i][j] = sc.nextInt();for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)ans = Math.max(ans, dfs(i, j));System.out.println(ans + 1);}public static int dfs(int x, int y) {if (dp[x][y] != 0)return dp[x][y];for (int i = 0; i <= 3; i++) {int tx = x + k[i][0];int ty = y + k[i][1];if (!(tx < 1 || ty < 1 || tx > n || ty > m || matrix[tx][ty] >= matrix[x][y]))dp[x][y] = Math.max(dp[x][y], 1 + dfs(tx, ty));}return dp[x][y];}
}