华为笔试题Java总结

华为笔试

差分塔子哥监考

import java.util.*;//错一个

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = 1000010;
        int n = in.nextInt();
        int[] diff = new int[N];
        int minTime = Integer.MAX_VALUE;
        int maxTime = Integer.MIN_VALUE;
        for(int i = 1; i <= n; i++){
            int l = in.nextInt();
            int r = in.nextInt();
            insert(diff, l, r,1);
            minTime = Math.min(minTime, l);
            maxTime = Math.max(maxTime, r);
        }
        int res = 0;
        for(int i = minTime; i <= maxTime; i++){
            diff[i] += diff[i-1];
            if(diff[i] == 1){
                res += 3;
            }else if(diff[i] >= 2){
                res += 4;
            }else if(diff[i] == 0){
                res += 1;
            }
        }
        System.out.println(res);
    }
    private static void insert(int[] diff, int l, int r, int c){
        diff[l] += c;
        diff[r+1] -= c;
    }
}

BFS塔子哥出城

import java.util.*;//错两个

public class Main {
    static int N = 100010;
    static int n, m, d;
    static Map<Integer, List<Integer>> graph = new HashMap<>();
    static int[] block;
    static List<List<Integer>> res = new ArrayList<>();
    static int[] prev;
    static int[] dist;
    public static void main(String[] args) {
        Scanner in  = new Scanner(System.in);
        n = in.nextInt();
        for(int i = 0; i < n; i++){
            graph.put(i, new ArrayList<>());
        }
        prev = new int[n];
        dist = new int[n];
        m = in.nextInt();
        for(int i = 0; i < m; i++){
            int a = in.nextInt();
            int b = in.nextInt();
            add(a, b);
        }
        d = in.nextInt();
        block = new int[n];
        for(int i = 0; i < d; i++){
            int x = in.nextInt();
            block[x] = 1;
        }
        bfs();
        printPath();
    }

    private static void printPath() {
        if(res.size() == 0 || res == null){
            System.out.println("NULL");
        }
        System.out.print(0);
        int minIndex = Integer.MAX_VALUE;
        List<Integer> minPath = null;
        for (List<Integer> path : res) {
            int lastIndex = path.get(path.size() - 1);
            if (lastIndex < minIndex) {
                minIndex = lastIndex;
                minPath = path;
            }
        }
        for (int i = minPath.size() - 1; i >= 0; i--) {
            if(minPath.get(i) == 0){
                System.out.print(minPath.get(i));
            }else{
                System.out.print("->" + minPath.get(i));
            }
        }
    }

    private static void bfs() {
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(0);
        while(!queue.isEmpty()){
            int t = queue.poll();
            if(graph.get(t).size() == 0){
                List<Integer> path = new ArrayList<>();
                while(t != 0){
                    path.add(t);
                    t = prev[t];
                }
                res.add(path);
                break;
            }
            for(int node : graph.get(t)){
                if(block[node] == 1) continue;
                dist[node] = dist[t] + 1;
                prev[node] = t;
                queue.offer(node);
            }
        }
    }

    private static void add(int a, int b){
        graph.get(a).add(b);
    }
}

二分购物系统的降级策略

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] str = in.nextLine().split(" ");
        int[] nums = new int[str.length];
        for(int i = 0; i < str.length; i++){
            nums[i] = Integer.parseInt(str[i]);
        }
        int cnt = in.nextInt();
        long sum = 0;
        for(int i = 0; i < nums.length; i++){
            sum += nums[i];
        }
        if(sum <= cnt){
            System.out.println(-1);
            return;
        }
        int value = 100010;
        int l = 0, r = value;
        while(l < r){
            int mid = l + r + 1>> 1;
            if(check(mid, nums, cnt)){
                l = mid;
            }else{
                r = mid - 1;
            }
        }
        System.out.println(l);
    }
    private static boolean check(int value, int[] nums, int cnt){
        long sum = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] < value){
                sum += nums[i];
            }else{
                sum += value;
            }
        }
        if(sum <= cnt) return true;
        return false;

    }
}

dfs获取最多食物

import java.util.*;

public class Main {
    static int N = 100010;
    static int n;
    static int res = Integer.MIN_VALUE;
    static int[] value = new int[N];
    static Map<Integer, List<Integer>> graph = new HashMap<>();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i = 0; i < N; i++){
            graph.put(i, new ArrayList<>());
        }
        for(int i = 0; i < n; i++){
            int id = in.nextInt();
            int pid = in.nextInt();
            if(pid != -1){
                add(pid, id);
            }
            value[id] = in.nextInt();
        }
        for(int i = 0; i < n; i++){
            dfs(i, 0);
        }
        System.out.println(res);
    }
    private static void dfs(int i, int sum) {
        sum += value[i];//不存在环,所以不需要visited
        res = Math.max(res, sum);//不用等待叶子节点才算
        for(int j : graph.get(i)){
            dfs(j, sum);
        }
    }
    private static void add(int a, int b){
        graph.get(a).add(b);
    }
}

dfs最佳检测顺序

import java.util.*;
public class Main {
    static int N = 100010;
    static int n;
    static Map<Integer, List<Integer>> graph = new HashMap<>();
    static boolean[] visited = new boolean[N];
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i = 0; i < N; i++){
            graph.put(i, new ArrayList<>());
        }
        for(int i = 0; i < n; i++){
            int temp = in.nextInt();
            if(temp != -1){
                add(temp, i);
            }
        }
        List<int[]> res = new ArrayList<>();
        for(int i = 0; i < n; i++){
            res.add(new int[]{dfs(i), i});
        }
        Collections.sort(res, new Comparator<int[]>() {
            @Override
            public int compare(int[] a, int[] b) {
                if (a[0] != b[0]) {
                    return b[0] - a[0];
                } else {
                    return Integer.compare(a[1], b[1]);
                }
            }
        });
        for(int[] pair : res){
            System.out.print(pair[1] + " ");
        }
    }
    private static int dfs(int i){
        int count = 0;
        visited[i] = true;
        for(int j : graph.get(i)){
            if(!visited[j]){
                count += dfs(j);
            }
        }
        visited[i] = false;
        return count+1;
    }
    private static void add(int a, int b){
        graph.get(a).add(b);
    }
}

模拟DNS本地缓存


二分2022.9.21-数组取min

import javax.swing.tree.TreeNode;
import java.util.*;
public class Main {
    static int N = 100010;
    static int[] nums;
    static int tot;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        tot = in.nextInt();
        nums = new int[n];
        int sum = 0;
        for(int i = 0; i < n; i++){
            nums[i] = in.nextInt();
            sum += nums[i];
        }
        if(sum <= tot){
            System.out.println(-1);
            return;
        }
        int l = 0, r = N;
        while(l < r){
            int mid = l + r + 1 >> 1;
            if(check(mid)){
                l = mid;
            }else{
                r = mid-1;
            }
        }
        System.out.println(l);
    }
    private static boolean check(int mid){
        int sum = 0;
        for(int i : nums){
            sum += Math.min(mid, i);
            if(sum > tot){
                return false;
            }
        }
        return true;
    }
}

差分天文爱好者

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = 100010;
        int n = in.nextInt();
        int[] l = new int[N];
        int[] r = new int[N];
        int[] diff = new int[N];//每分钟能观测流星数量
        for(int i = 1; i <= n; i++){
            l[i] = in.nextInt();
        }
        for (int i = 1; i <= n; i++) {
            r[i] = in.nextInt();
        }
        for(int i = 1; i <= n; i++){
            insert(diff, l[i], r[i], 1);
        }
        int max = 0;
        int count = 0;
        for (int i = 1; i < N; i++) {
            diff[i] += diff[i-1];
            if(diff[i] > max){
                max = diff[i];
                count = 1;
            }else if(diff[i] == max){
                count++;
            }
        }
        System.out.println(max + " " + count);
    }
    private static void insert(int[] diff, int l, int r, int c){
        diff[l] += c;
        diff[r+1] -= c;
    }
}

差分1109. 航班预订统计

class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int N = 100010;
        int[] diff = new int[N];
        int[] res = new int[n];
        int count = bookings.length;
        for(int i = 1; i <= count; i++){
            insert(diff, bookings[i-1][0], bookings[i-1][1], bookings[i-1][2]);
        }
        for(int i = 1; i <= n; i++){
            diff[i] += diff[i-1];
            res[i-1] = diff[i];
        }
        return res;
    }
    private void insert(int[] diff, int l, int r, int c){
        diff[l] += c;
        diff[r+1] -= c;
    }
}

幼儿园排队报数

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] nums = new int[n];
        int[] res = new int[n];
        int[] arr = new int[1010];//记录某个数出现多少次
        for(int i = 0; i < n; i++){
            nums[i] = in.nextInt();
            arr[nums[i]]++;
        }
        for(int i = 0; i < n; i++){
            int count = 0;
            arr[nums[i]]--;
            for(int j = 0; j < arr.length; j++){
                if(j < nums[i]){
                    count += arr[j];
                }
            }
            res[i] = count;
        }
        for(int i : res){
            System.out.print(i + " ");
        }
    }
}

hashmap排序检测热点字符

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(); // 依次统计出现最多的a个字符
        int b = in.nextInt(); // 间隔长度
        String s = in.next(); // 需要统计的字符串

        HashMap<Character, Integer> map = new HashMap<>();

        for (int i = 0; i < s.length(); i+=b) {
            for(int j = i; j < i+b; j++){
                char c = s.charAt(j);
                map.put(c, map.getOrDefault(c, 0) + 1);
            }
            // 将 map 中的所有键值对转换为一个 Entry 的列表,存入 list 中
            List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
            // 自定义一个比较器,按照值的降序排列,如果值相等,则按照键的升序排列
            Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
                @Override
                public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
                    if (o1.getValue() != o2.getValue()) {
                        return o2.getValue() - o1.getValue();
                    } else {
                        return o2.getKey() - o1.getKey();
                    }
                }
            });
            for(int k = 0; k < a; k++){
                System.out.print(list.get(k).getKey());
            }
        }
    }
}

完全背包攻城战

/*
状态表示:dp[i][j]:所有只考虑前i个物品,且总体积不大于j的所有选法
状态转移:dp[i][j] = dp[i-1,j-v[i]*k] + w[i]*k; k=0,1,2,...,T/t[i]; k最多就取到T/t[i]
最后取max
*/
import java.util.*;
public class Main {
    static int N = 1010;
    static int[][] dp = new int[N][N];
    static int[] v = new int[N];
    static int[] w = new int[N];
    static int[] t = new int[N];
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int M = in.nextInt();
        int T = in.nextInt();
        for(int i = 1; i <= N; i++){
            w[i] = in.nextInt();
            v[i] = in.nextInt();
            t[i] = in.nextInt();
        }
        for(int i = 1; i <= N; i++){
            for(int j = 0; j <= M; j++){
                for(int k = 0; k <= T/t[i] && k*v[i] <= j; k++){
                    dp[i][j] = Math.max(dp[i][j], dp[i-1][j-k*v[i]] + k*w[i]);
                }
            }
        }
        System.out.println(dp[N][M]);
    }
}

第k大字母

import java.lang.reflect.Array;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        Map<Character, Integer> map = new HashMap<>();//记录下字符第一次出现的索引位置
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(map.containsKey(c)){
                continue;
            }
            map.put(c, i);
        }
        char[] ch = s.toCharArray();
        Arrays.sort(ch);
        int k = in.nextInt();
        if(k > s.length()){
            System.out.println(map.get(ch[s.length()-1]));
            return;
        }
        System.out.println(map.get(ch[k-1]));
    }
}

平均像素值

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String[] strs = s.split(" ");
        int n = strs.length;
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(strs[i]);
        }

        int l = -128, r = 128;
        int ansK = 0;
        double ansDiff = Double.MAX_VALUE;
        while(l <= r){
            int mid = l + r >> 1;
            double avg = getAvg(nums, mid);
            if(avg > 128){
                r = mid - 1;
            }else{
                l = mid + 1;
            }
            double diff = Math.abs(128 - avg);
            if(diff < ansDiff){
                ansDiff = diff;
                ansK = mid;
            }
        }
        System.out.println(ansK);
    }

    private static double getAvg(int[] nums, int k){
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i] + k;
        }
        return sum / (double) nums.length;
    }
}

dfs年会奖品分配策略

/*
x==0:满足条件变更xyz时,有:preceX(6) + preceZ(5) !< x(0) - z(0)
x,y,z:记录之前最优的分配
*/
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    private static int n;
    private static int[] prices;
    private static int x, y, z, res;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        prices = new int[n];
        for (int i = 0; i < n; i++) {
            prices[i] = scanner.nextInt();
        }
        Arrays.sort(prices);
        dfs(0, 0, 0, 0);
        System.out.println(ans);
    }

    private static void dfs(int level, int priceX, int priceY, int priceZ) {
        if (level == n) {
            if (priceX > priceY && priceY > priceZ && (x == 0 || priceX - priceZ < x - z)) {
                x = priceX;
                y = priceY;
                z = priceZ;
                res = x - z;
            }
            return;
        }
        dfs(level + 1, priceX + prices[level], priceY, priceZ);
        dfs(level + 1, priceX, priceY + prices[level], priceZ);
        dfs(level + 1, priceX, priceY, priceZ + prices[level]);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Guanam_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值