用友技术笔试-友新星实习项目-Java-01卷

用友技术笔试-友新星实习项目-Java-01卷(2024.5.26 15:00-17:00)

第1题

public class Solution {
    public long minOperations(int[] nums1, int[] nums2, int k) {
        int n = nums1.length;
        int[] diff = new int[n];
        long totalIncrease = 0, totalDecrease = 0;
        for (int i = 0; i < n; i++) {
            diff[i] = nums2[i] - nums1[i];
            if (diff[i] % k != 0) {
                return -1;
            }
            if (diff[i] > 0) {
                totalIncrease += diff[i] / k;
            } else if (diff[i] < 0) {
                totalDecrease += -diff[i] / k;
            }
        }
        if (totalIncrease != totalDecrease) {
            return -1;
        }
        return totalIncrease;
    }
}

第2题

解法1

public class Solution {
    public ArrayList<Integer> findAllPerson(int n, int[][] meetings, int firstPerson) {
        ArrayList<Integer> ans = new ArrayList<>();
        ans.add(0);
        ans.add(firstPerson);
        Arrays.sort(meetings, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[2] - o2[2];
            }
        });
        for (int[] meeting : meetings) {
            if (ans.contains(meeting[0]) && !ans.contains(meeting[1])) {
                ans.add(meeting[1]);
            }
            if (ans.contains(meeting[1]) && !ans.contains(meeting[0])) {
                ans.add(meeting[0]);
            }
        }
        Collections.sort(ans);
        return ans;
    }
}

解法2

使用TreeSet去重并排序

public class Solution {
    public ArrayList<Integer> findAllPerson(int n, int[][] meetings, int firstPerson) {
        TreeSet<Integer> set = new TreeSet<>();
        set.add(0);
        set.add(firstPerson);
        Arrays.sort(meetings, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[2] - o2[2];
            }
        });
        for (int[] meeting : meetings) {
            if (set.contains(meeting[0])) {
                set.add(meeting[1]);
            }
            if (set.contains(meeting[1])) {
                set.add(meeting[0]);
            }
        }
        return new ArrayList<>(set);
    }
}

第3题

解法1(不符合题目要求的不能更改节点的value)

public class Solution {
    public ListNode groupSort(ListNode head, int n) {
        if (head == null || n <= 1) {
            return head;
        }
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode preEnd = dummyHead;
        while (head != null) {
            ListNode start = head;
            ListNode end = head;
            int count = 1;
            while (count < n && end.next != null) {
                end = end.next;
                count++;
            }
            if (count < n) {
                break;
            }
            ListNode nextStart = end.next;
            end.next = null;
            ListNode sortedHead = sort(start);
            preEnd.next = sortedHead;
            while (preEnd.next != null) {
                preEnd = preEnd.next;
            }
            preEnd.next = nextStart;
            head = nextStart;
        }
        return dummyHead.next;
    }

    public ListNode sort(ListNode head) {
        List<Integer> values = new ArrayList<>();
        ListNode cur = head;
        while (cur != null) {
            values.add(cur.val);
            cur = cur.next;
        }
        Collections.sort(values, Collections.reverseOrder());
        cur = head;
        for (Integer value : values) {
            cur.val = value;
            cur = cur.next;
        }
        return head;
    }
}

解法2(符合题目要求)

public class Solution {
    public ListNode groupSort(ListNode head, int n) {
        if (head == null || n <= 1) {
            return head;
        }
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode preEnd = dummyHead;
        while (head != null) {
            ListNode start = head;
            ListNode end = head;
            int count = 1;
            while (count < n && end.next != null) {
                end = end.next;
                count++;
            }
            if (count < n) {
                break;
            }
            ListNode nextStart = end.next;
            end.next = null;
            ListNode sortedHead = sort(preEnd);
            preEnd.next = sortedHead;
            while (preEnd.next != null) {
                preEnd = preEnd.next;
            }
            preEnd.next = nextStart;
            head = nextStart;
        }
        return dummyHead.next;
    }

    public ListNode sort(ListNode preEnd) {
        ListNode cur = preEnd.next;
        PriorityQueue<ListNode> pq = new PriorityQueue<>(new Comparator<ListNode>() {
            @Override
            public int compare(ListNode o1, ListNode o2) {
                return o2.val - o1.val;
            }
        });
        while (cur != null) {
            pq.add(cur);
            cur = cur.next;
        }
        cur = preEnd;
        while (!pq.isEmpty()) {
            cur.next = pq.poll();
            cur = cur.next;
        }
        cur.next = null;
        return preEnd.next;
    }
}

第4题

public class Solution {
    public int[][] interviews;

    public int maxValue(int[][] interviews, int k) {
        if (k < 1) {
            return 0;
        }
        Arrays.sort(interviews, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        this.interviews = interviews;
        int[][] dp = new int[interviews.length + 1][k + 1];
        for (int i = 1; i <= interviews.length; i++) {
            for (int j = 1; j <= k; j++) {
                dp[i][j] = dp[i - 1][j];
                int p = find(i - 1);
                if (p != -1) {
                    dp[i][j] = Math.max(dp[i][j], dp[p + 1][j - 1] + interviews[i - 1][2]);
                } else {
                    dp[i][j] = Math.max(dp[i][j], interviews[i - 1][2]);
                }
            }
        }
        return dp[interviews.length][k];
    }

    public int find(int i) {
        int low = 0, high = i - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (interviews[mid][1] < interviews[i][0]) {
                if (mid + 1 <= i - 1 && interviews[mid + 1][1] < interviews[i][0]) {
                    low = mid + 1;
                } else {
                    return mid;
                }
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }
}
#用友##实习##机试##笔试##Java#
全部评论
幽默用友
3 回复 分享
发布于 05-28 12:20 北京
牛的
点赞 回复 分享
发布于 05-26 17:20 广东
第一题1371,1434,3这种直接返回-1?
点赞 回复 分享
发布于 05-26 17:29 山西
第二题有问题吧,如果同一时间很多个meeting,会不会导致漏解?
点赞 回复 分享
发布于 05-26 17:58 北京
真猛啊
点赞 回复 分享
发布于 05-26 18:28 湖北
第三题是不是有点小问题呀,题目好像要求交换节点,不能直接更改 value 值
点赞 回复 分享
发布于 05-26 20:08 江苏
ac了三题。第三题有bug似的只过了一半不到
点赞 回复 分享
发布于 05-28 19:42 广东
有人约面了吗
点赞 回复 分享
发布于 05-29 01:51 河北

相关推荐

头像
10-15 22:27
已编辑
门头沟学院 C++
罗格镇的小镇做题家:我投了hr打电话来说学历太低了不符合要求,建议投荣耀,结果荣耀也投了一定水花没有,非本211硕
投递华为等公司10个岗位
点赞 评论 收藏
分享
找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
16 42 评论
分享
牛客网
牛客企业服务