美团笔试一二题,只能ac50%(反面教材,已解决)

第一题代码,递归+备忘录:
package interview;

import java.util.*;

public class Main {
    static long[][] cache;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        sc.nextLine();

        cache = new long[2][size];

        char[][] places = new char[2][size];
        places[0] = sc.nextLine().toCharArray();
        places[1] = sc.nextLine().toCharArray();

        long ans =  walk(2 - 1, size - 1, places);
        System.out.println(ans == 0 ? - 1 : ans);
    }

	public static long walk(int i, int j, char[][] places) {
        if(i < 0 || j < 0 || i == 2 || j == places[0].length) {
            return 0;
        }

        if(places[i][j] == 'X') {
            return 0;
        }

        if((i == 0 && j == 0)) {
            return 1;
        }

        if(cache[i][j] != 0) {
            return cache[i][j];
        }

        long n = walk(i, j - 1, places)  + walk(i + 1, j - 1, places) + walk(i - 1, j - 1, places);
        cache[i][j] = n;


        return n;
    }
}
第二题,数组保存结果:
package interview;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i<n; i++) {
            nums[i] = sc.nextInt();
        }

        int[] dp = new int[1001];

        for(int i = 0; i<n; i++) {
            dp[nums[i]] = dp[nums[i]] + 1;
            if((nums[i] | x) != nums[i]) {
                dp[nums[i] | x] =  dp[nums[i] | x] + 1;
            }
        }

        int ans = 0;
        for(int i = 0; i<1001; i++) {
            ans = Math.max(dp[i], ans);
        }

        System.out.println(ans);
    }
}



#美团笔试##美团##笔试题目#
全部评论
第一题递归超时,得用迭代dp 第二题位运算后,数组越界 继续修炼,再见
1 回复 分享
发布于 2020-03-12 22:36
AC百分之50是说只能做出来一半的用例嘛
点赞 回复 分享
发布于 2022-03-25 18:02
可以发下题目不 这样盲看。。
点赞 回复 分享
发布于 2020-03-13 00:48
递归超时。。只用dp才行
点赞 回复 分享
发布于 2020-03-12 22:31

相关推荐

05-12 10:10
已编辑
门头沟学院 人工智能
写这篇之前我犹豫了挺久。一方面是怕被人骂,&quot;又一个收割焦虑的转行帖&quot;;另一方面是看了太多用&nbsp;GPT&nbsp;套娃出来的「学习路线」文章,AI&nbsp;味重得让人没法读完。所以这篇全是亲身踩过的坑,时间线、用过的项目、当时的心路全都尽量原样写出来。如果你是大学生在迷茫要不要转&nbsp;AI,或者已经在转的路上,希望能给点参考。&nbsp;一个反共识的开场:你以为进&nbsp;OpenAI&nbsp;的人都是博士?&nbsp;先讲个故事,跟我没关系,但跟所有想转&nbsp;AI&nbsp;的人都有关系。&nbsp;OpenAI&nbsp;的&nbsp;Sora&nbsp;团队(就是搞文生视频那个)一共&nbsp;13&nbsp;个人。这里面有两个人特别有意思:&nbsp;Will&nbsp;DePue,密歇根大学计算机系,直接辍学了。17...
_hengheng:我也本,也算是做ai相关,我最开始感觉做ai工程师有多么多么困难,后来发现懂了原理后整体训练完全可以看成一个流程化的内容,开源方案太多了,大多基本都是按着模子在自家业务上做各种操作,就算是大厂的小部门也没那么多资源去训基模,反而更多的是像怎么把技术往业务方向靠近了,不过当前时代如果本科学历没那么好加上自己执行力不是特别强还真不建议走ai工程师这条路,可以试试其他ai的偏业务方向,不然校招不太好杀出来
点赞 评论 收藏
分享
评论
点赞
10
分享

创作者周榜

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