腾讯音乐笔试

感觉非常难大概

mid hard hard AC 2道第三题没思路但是dp味儿好浓

// 只含 0,1的串,每次只能将相邻的2个字符同时改成0或1
// 最少问多少次,可以让串的所有字符相等
// s.length <= 1e3
 public int minOperations(String s) {
        // write code here
        return Math.min(cnt(s, 0), cnt(s, 1));
 }

// 将s全部改成x, 最少的操作次数
 public int cnt(String s, int x) {
         int res = 0;
        for (int i = 0; i < s.length(); i++) {
            int j = s.charAt(i) - '0';
            if (j == x)
                continue;
            i++;
            res++;
        }
        return res;
 }
// 求有多少连续子数组的元素乘积 0的个数大于等于x
// 防止结果太大对 1e9 + 7 取模
// a.length <= 1e5,  1 <= a[i],x <= 1e9
public class 连续子数组的数量 {
    static int mod = (int) (1e9 + 7);
    public int getSubarrayNum(ArrayList<Integer> a, int x) {
        // write code here
        // 样例:
        // a =[5,2,3,50,4], x = 2 ===> 6
        // [5,2,3,50,4],[50,4],[3,50,4],[2,3,5,10,4],[2,3,50],[5,2,3,50]
        int[] sum2 = new int[a.size() + 1];
        int[] sum5 = new int[a.size() + 1];
        int res = 0;
        int c2 = 0;
        int c5 = 0;
        for (int i = 0; i < a.size(); i++) {
            int y = a.get(i);

            while (y % 2 == 0) {
                c2++;
                y /= 2;
            }
            while (y % 5 == 0) {
                c5++;
                y /= 5;
            }
            sum2[i + 1] = c2;
            sum5[i + 1] = c5;
            if (Math.min(c2, c5) < x)
                continue;
            int min = Math.min(find(sum2, c2 - x, i), find(sum5, c5 - x, i));
            res = (res + min + 1) % mod;
        }
        return res;
    }

    int find(int[] a, int x, int end) {
        int l = 1, r = end;
        while (l <= r) {
            int mid = l + r >> 1;
            if (a[mid] <= x) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return r;
    }

    public static void main(String[] args) {
        连续子数组的数量 s = new 连续子数组的数量();
        Integer[] a = {5, 2, 3, 50, 4};
        List<Integer> list = Arrays.asList(a);
        int res = s.getSubarrayNum(new ArrayList<>(list), 2);
        System.out.println(res);
    }
}
public class 好矩阵 {
    static int mod = (int) (1e9 + 7);
    // 定义好矩阵:n*m的矩阵中所有2*2的子矩阵的所有元素和为偶数
    // n * m 的矩阵中每个元素都在[1,x],能构造出多少好矩阵
    //   2 <=n,m,x <= 1e9
    public int numsOfGoodMatrix(int n, int m, int x) {
        // write code here
        return -1;
    }
}
#腾讯音乐##腾讯音乐23秋招笔试好难啊,麻了#
全部评论
情况一模一样
点赞 回复
分享
发布于 2022-09-26 21:03 广东
你想太复杂了
点赞 回复
分享
发布于 2022-09-26 21:23 广东
滴滴
校招火热招聘中
官网直投
老哥 第二题这个find方法没太看懂,能给将一下吗
点赞 回复
分享
发布于 2022-09-26 21:24 陕西
第三题
点赞 回复
分享
发布于 2022-09-26 22:16 四川
除了好矩阵 其他两道大可以不必dp
点赞 回复
分享
发布于 2022-09-26 22:34 北京
同学同花顺尝试一下吗,面试简单不造火箭,可保姆式全程跟进度,我帖子有内推
点赞 回复
分享
发布于 2022-09-27 09:30 浙江

相关推荐

8 10 评论
分享
牛客网
牛客企业服务