腾讯音乐笔试 9.26

前言

牛客上一个大佬的题解链接

  • https://www.nowcoder.com/discuss/1065321?type=2&channel=-1&source_id=discuss_terminal_discuss_jinghua_nctrack
  • https://www.nowcoder.com/discuss/1064816
编程题1:好矩阵的数量

题目描述
我们定义一个矩阵为“好矩阵”,当且仅当该矩阵所有22的子矩阵数字和为偶数。
例如
在这里插入图片描述
是好矩阵,两个2
2的子矩阵的和分别为8和12.
请问n行m列,矩阵中每个数均在[1, x]范围内的好矩阵有多少种?由于答案过大,请对 1 0 9 + 7 10^9+7 109+7取模。
数据范围: 2 ≤ n , m , x ≤ 1 0 9 2 \le n,m,x \le 10^9 2n,m,x109
保证x为偶数。
示例1
输入

2, 2, 2

输出

8

在这里插入图片描述

编程题2:连续子数组数量

题目描述
给定一个数组,请你编写一个函数,返回元素乘积末尾零数量大于等于x的连续子数组数量。
答案可能很大,请将答案对 1 0 9 + 7 10^9+7 109+7取模再返回。
数组长度不超过 1 0 5 10^5 105
数组元素,x均为不超过 1 0 9 10^9 109的正整数。
示例1
输入

[5, 2, 3, 50, 4], 2

输出

6

说明

共有以下6个合法连续子数组:
[5, 2, 3, 50],乘积为1500,末尾有2个零。
[5, 2, 3, 50, 4],乘积为6000,末尾有3个零。
[2, 3, 50],乘积为300,末尾有2个零。
[2, 3, 50, 4],乘积为1200,末尾有2个零。
[3, 50, 4],乘积为600,末尾有2个零。
[50, 4],乘积为200,末尾有2个零。

代码
100%,双指针+滑动窗口

class Solution:
    def getSubarrayNum(self , a: list, x: int) -> int:
        # write code here

        MOD = pow(10, x)
        
        def adjust(val):
            if val>=MOD and val%MOD==0:
                return True
            else:
                return False

        n = len(a)
        ans = 0
        l, r = 0, 0
        num = 1
        while r<n:
            while not adjust(num) and r<n:
                num *= a[r]
                r += 1

            while adjust(num) and l<r:
                # print(l, r)
                ans += n-r+1
                num //= a[l]
                l += 1
            ans %= (10**9+7)
        return ans

if __name__=='__main__':
    test_case = Solution()
    print(test_case.getSubarrayNum([5, 2, 3, 50, 4], 2))
编程题3: 01串修改

题目描述
给定一个只包含’0’和’1’两种字符的字符串,每次操作可以选择相邻的两个字符,将它们同时变成’0’或者同时变成’1’。
请问最少多少次操作后,所有的字符都相同?
字符串长度不超过1000。
示例1
输入

“1001101”

输出

2

代码
100%,模拟+贪心?

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param str string字符串 
# @return int整型
#
class Solution:

    def get_res(par_data, target):
        data = [ch for ch in par_data]
        ans = 0
        idx = 0
        n = len(data)
        while idx<n:
            if data[idx]!=target:
                if idx+1<n:
                    data[idx] = target
                    data[idx+1] = target
                else:
                    data[idx] = target
                ans += 1
            idx += 1
        return ans

    def minOperations(self , str: str) -> int:
        # write code here
        data = [ch for ch in str]

        num1 = self.get_res(data, '0')
        num2 = self.get_res(data, '1')
        return max(num1, num2)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值