网易伏羲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.

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

相关推荐

10-13 22:56
门头沟学院 C++
rt,鼠鼠的浪潮网签明天过期,鼠鼠是山东人,好像自己也能接受。之前的面试大厂基本挂干净了,剩下小米二面后在泡,问了下面试官没有挂,但要泡。还有海信似乎也通过了,不过在深圳,鼠鼠也不是很想去。其它还有一些公司应该陆陆续续还有一些面试,现在有些纠结是直接签了还是再等再面呢?大佬们能不能给鼠鼠提一些意见,万分感谢!!!
牛客78696106...:浪潮可不是开摆,当初我还是开发的时候我组长跟我说他们组有段时间天天1,2点走,早上5点就来,全组肝出来心肌炎,浪潮挣钱省立花可不是说说,当然也看部门,但是浪潮普遍就那dio样,而且你算下时薪就知道不高,没事也是9点半走,不然算你旷工
投递小米集团等公司10个岗位
点赞 评论 收藏
分享
09-01 16:46
已编辑
门头沟学院 Java
mmvvpp:错了!!给了offer之后还有试用期,试用期过了就完事了?错了!还有每个季度的kpi考核,拿一个c就等着被劝退。那我好好干不拿c不就完了?错了!最多三年劳动合同到期,续不续期未知数。每年都有1800w毕业生毕业,今年你是小萌新蜜月期,明年你是老油条,长江后浪推前浪,前浪死在沙滩上。这就是——互联网!
秋招的破防瞬间
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务