网易伏羲4.22笔试

1.宝石分数,给定行囊为k,宝石数组price,从i到j一定要装在同一个行囊里,并且每个行囊的分数为price[i]+price[j],全都放进行囊里且分数为所有行囊价值和。求最大和最小之间的差值。用例:[2,3,5,4] k=2,输出:(15-11=)4

2.永劫无间两个玩家分别在不同坐标上,互相找到对方最小用时,其中二维数组地图map为1表示可达,为0不可达,找不到对方输出-1。

本来以为都是动态规划,所以浪费了很长时间,最后半小时用回溯法,做出来第二题【100%】,然后继续用回溯法做第一题【50%】,刚用了15分钟又做了一下,自己测试几个都能通过,不知道这实际上对不对【回溯法真好用啊,腾讯音乐回溯法一个没做出来给我打击得不轻】。下面贴出代码:

import java.util.*;
public class Solution {
    static int max = Integer.MIN_VALUE;
    static int min = Integer.MAX_VALUE;
    static int tempScore = 0;

    public static int putGems (int[] price, int k) {
        if(price.length<=k){
            return 0;
        }
        helper(price, 0, k, 0);
        return max-min;
    }
    public static void main(String[] args) {
        System.out.println("----------" + putGems(new int[]{2,3,5, 4}, 2));
    }
        public static void helper(int[] price, int start, int k, int lap){
            //System.out.println(start + " "+ lap + " Score: " + tempScore);
            if(k==lap||price.length-start<k-lap){
                if(k == lap&&start==price.length){
                    max = Math.max(max, tempScore);
                    min = Math.min(min, tempScore);
                }
                return;
            }
            tempScore+=price[start];
            for(int i=start; i<price.length; i++){
                tempScore+=price[i];
                helper(price, i+1, k, lap+1);
                tempScore-=price[i];
            }
            tempScore-=price[start];
        }

    }
import java.util.*;
public class Solution {
    static int res = Integer.MAX_VALUE;
    public static int getEstTime (int[][] map, int a_x, int a_y, int b_x, int b_y) {

        helper(map, a_x, a_y,b_x, b_y, 0, new int[map.length][map[0].length]);
        return res==Integer.MAX_VALUE?-1:res%2==1?res/2+1:res/2;
    }

    private static void helper(int[][] map, int a_x, int a_y, int b_x, int b_y, int step, int[][] used) {
        if(b_x==a_x&&b_y==a_y){
            res = Math.min(res, step);
        }
        used[a_x][a_y] = 1;
        List<int[]> temp = new ArrayList<>();
        if(a_x+1<map[0].length&&map[a_x+1][a_y]==1&&used[a_x+1][a_y]==0)
            temp.add(new int[]{a_x+1, a_y});
        if(a_x-1>=0&&map[a_x-1][a_y]==1&&used[a_x-1][a_y]==0)
            temp.add(new int[]{a_x-1, a_y});
        if(a_y+1<map.length&&map[a_x][a_y+1]==1&&used[a_x][a_y+1]==0)
            temp.add(new int[]{a_x, a_y+1});
        if(a_y-1>=0&&map[a_x][a_y-1]==1&&used[a_x][a_y-1]==0)
            temp.add(new int[]{a_x, a_y-1});

        for(int i=0; i<temp.size(); i++){
            helper(map, temp.get(i)[0], temp.get(i)[1], b_x, b_y, step+1, used);
        }
    }

    public static void main(String[] args) {
        int[][] map = new int[][]{{1, 1, 1, 0, 1},{1, 0, 1, 1, 1}, {1, 1, 1, 0, 1},{0, 0, 1, 1, 1},{1, 0, 0, 0, 1}};
        System.out.println(getEstTime(map, 0,0,4,4));
    }
}

1.

#网易信息集散地#
全部评论
我第一个是动态规划做的,算两次分别是最大最小值。但是看好像有更简单的贪心做法
点赞 回复 分享
发布于 2023-04-22 16:52 四川
试了一下在leetcode2551第一题上超时了,是贪心法
点赞 回复 分享
发布于 2023-04-25 14:59 江苏
请问笔试要写输入输出吗
点赞 回复 分享
发布于 2024-04-14 13:04 北京

相关推荐

神哥不得了:神哥来啦~自我评价和校园经历的话可以直接删了,从大厂暑期的话应该没有什么太多问题,应该是能拿到很多大厂面试机会的,就是在面试的时候表示的好一点就行,可以在面试前先把高频top 50的八股多巩固几遍,千万不要看那些假高频八股,这两个项目的话问题不是很大,应该能够帮你找到大厂实习的,算法的话一定要刷起来,因为大厂有些还是比较看重算法的
点赞 评论 收藏
分享
03-11 21:46
西北大学 Java
河和静子:这只是实习工资,我学长北大通班博一的,他同学被这家天天发邮件让他去实习,一个月10w
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客企业服务