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

相关推荐

02-11 17:51
腾讯_TEG_技术
点赞 评论 收藏
分享
做人要有梦想dji:最新工位查看图片
点赞 评论 收藏
分享
02-02 20:25
门头沟学院 Java
数学转码崽:八股文也算是前人总结的精华,但是因为全是结果导向,你光背不去理解它背后的深层原理和这样做的原因,反而忽略了程序员最该重视的过程导向。推荐你不会的就去多问ai,比如我当时背的时候,concurrenthashmap底层原理常见八股网站都会讲,但是我不理解为什么它去用synchronize锁节点,为什么不用reentrantlock去锁节点。面试官问我你为什么觉得synchronize在这个场景下性能更好呢?虽然面试官可能也不确定清楚,但是你可以尝试给他解答,让他看见你的思考,这才是最重要的,毕竟你没实习,你的项目你也无法证明是你自己思考的产物,那就在别的地方体现你的能力
点赞 评论 收藏
分享
评论
点赞
2
分享

创作者周榜

更多
牛客网
牛客企业服务