lee解题分享

rongyao425 1

s = input().strip()
op = 0
if s[0] == '+':
    s = s[1:]
elif s[0] == '-':
    op = 1
    s = s[1:]


num = int(s)
if op == 1:
    num = -num
if num>pow(2,31)-1:
    print('INT_MAX')
elif num<-pow(2,31):
    print('INT_MIN')
else:
    print(num)

rongyao425 2

l = list(input().split())
# print(l)
p = float(l[0])
M = int(l[1])
lower = int(l[2])
upper = int(l[3])


def calculate_C(a,b):
    up = 1
    lo = 1
    i = 1
    while i<=b:
        up *= a
        a -= 1
        lo *= i
        i += 1
    return up/lo
result = 0
for i in range(lower,upper+1):
    C = calculate_C(M,i)
    p_i = C * pow(p,i)*pow(1-p,M-i)
    result += p_i
print("{:1.2f}".format(result))

rongyao0425 3

s_list = list(map(str,input().strip().split(',')))
# print(s_list)


# 前缀和,从1开始
pre_s = [0 for _ in range(len(s_list) + 1)]
for i in range(len(s_list)):
    pre_s[i+1] = pre_s[i] + len(s_list[i])
# print(pre_s)


n = int(input())



word_list = []
word_count = 0


# 双指针
i = 0
j = 1
while j < len(s_list) +1:
    word_count = pre_s[j] - pre_s[i] + j-i-1
    if word_count > n:
        tmp_list = []
        for k in range(i+1,j):
            tmp_list.append(k)
            
        word_list.append(tmp_list)
        i = j-1
        word_count = 0
    else:
        j += 1


tmp_list = []
for k in range(i+1,j):
    tmp_list.append(k)
word_list.append(tmp_list)
print(word_list)


# 模拟
for k in range(len(word_list)):
    l = word_list[k]
    word_length = pre_s[l[-1]] - pre_s[l[0]-1]
    word_row = ""
    if k == len(word_list)-1:
        for i in range(len(l)):
            word_row += s_list[l[i]-1] + "#"
        tmp = n-len(word_row)
        word_row += "#"*tmp
    elif len(l) == 1:
        tmp = n-word_length
        word_row = s_list[l[0]-1] + tmp*"#"
    elif (n-word_length)%(len(l)-1) != 0:
        tmp = (n-word_length)//(len(l)-1)
        remain = n-word_length - tmp*(len(l)-1)
        for i in range(len(l)):
            if i != len(l)-1:
                word_row += s_list[l[i]-1] + "#"*tmp
                if remain != 0:
                    word_row += "#"
                    remain -= 1
        word_row += s_list[l[i]-1] 
    else:
        tmp = (n-word_length)//(len(l)-1)
        for i in range(len(l)):
            if i != len(l)-1:
                word_row += s_list[l[i]-1] + "#"*tmp
        word_row += s_list[l[i]-1]
    print(word_row)

huawei426 1

import collections
N = int(input().strip())
# 入度
indegree = [0 for _ in range(N+1)]
# 出度节点
nx = collections.defaultdict(list)
for i in range(1,N+1):
    l = list(map(int,input().strip().split()))
    indegree[i] = l[0]
    for j in l[1:]:
        nx[j].append(i)


# 拓扑排序
q = collections.deque()
for i in range(1,N+1):
    if indegree[i] == 0:
        q.append(i)
cnt = 0
node_num = 0
while q :
    res = len(q)
    # 统计初始化次数
    cnt += 1
    for i in range(res):
        node = q.popleft()
        # 统计节点数
        node_num += 1
        for child in nx[node]:
            indegree[child] -= 1
            if indegree[child] == 0:
                q.append(child)
    # 有环
    if node_num > N:
        print(-1)
        break
print(cnt)

huawei426 2

class Node:
    def __init__(self,val,next = None,pre = None):
        self.val = val
        self.next = next
        self.pre = pre


lo,up = map(int,input().strip().split())



# 建立双向链表以及哈希表
hash_table = dict()
id2node = dict()
head = Node(-1)
tail = Node(-1)
tmp = head
used_set = set()
for i in range(lo,up+1):
    node = Node(i)
    hash_table[i] = node
    id2node[node] = i
    node.pre = tmp
    tmp.next = node
    tmp = node
tail.pre = tmp



# 输入操作
op_num = int(input())
for i in range(op_num):
    op_list = list(map(int,input().strip().split()))
    if op_list[0] == 1:
        if up - lo +1 -len(used_set)<op_list[1]:
            continue
        else:
            tmp = head
            j = 0
            while j < op_list[1]:
                tmp = head.next
                used_set.add(tmp)
                j += 1
            head.next = tmp.next
    elif op_list[0] == 2:
        node = hash_table[op_list[1]]
        if node in used_set:
            continue
        else:
            used_set.add(node)
            pre = node.pre
            next = node.next
            node.pre.next = next
            node.next.pre = pre
    elif op_list[0] == 3:
        node = hash_table[op_list[1]]
        if node not in used_set:
            continue
        else:
            used_set.remove(node)
            tail.pre.next = node
            node.next = tail
            tail.pre = node



print(id2node[head.next])

meituan415 贪心

n = int(input())
a_list = list(map(int,input().strip().split()))
b_list = list(map(int,input().strip().split()))
c_list = list(map(int,input().strip().split()))


d_list = [[a_list[i],b_list[i],c_list[i]] for i in range(len(a_list))]


# 外
tao1 = sorted(d_list,key = lambda x:x[0])
# 内
tao2 = sorted(d_list,key = lambda x:x[1])


# 二分搜索
def binary_search(l_list,target):
    l = 0
    r = len(l_list)
    while l<r:
        mid = (l+r)//2
        if l_list[mid][0] > target:
            r = mid
        elif l_list[mid][0] <= target:
            l = mid + 1
        elif l == 0 and r ==0 and mid == 0:
            return -1


    return l-1


# 每次找满足条件的外最大的
size_ = float('inf')
cost = 0
per = 0
pre_idx = 0
while True:
    idx= binary_search(tao1,size_)
    if idx == -1:
        break 
    if size_ != float('inf'):
        tao2[pre_idx][1] -= tao1[idx][0]
    pre_idx = idx
    size_ = tao1[idx][1]
    per = tao1[idx][2]


for a,b,c in tao2:
    cost += b*c



print(cost)

meituan415 模拟

n = int(input())
l = list(map(int,input().split()))
hash_table = dict()
for i in range(n+1):
    hash_table[i] = 0
for i in l:
    hash_table[i] += 1
for key in hash_table:
    if hash_table[key] > 1 and key<n:
        hash_table[key] -= 1
        hash_table[key+1] += 1
for i in range(n+1):
    if hash_table[i] == 0:
        print(i)
        break

rongyao422 bfs

import collections
l = list(map(int,input().strip().split()))
q = collections.deque()
for i in range(0,(len(l)+1)//2):
    q.append(i)


n = len(q)
node = 0
path = 1
flag = True


# bfs
while q:
    idx = q.popleft()
    node += 1
    if l[idx] + idx == len(l)-1:
        print(path + 1)
        flag =False
        break
    elif l[idx] +idx>len(l):
        pass
    else:
        q.append(idx+l[idx])
    if node == n:
        path += 1
        node = 0
        n = len(q)
if flag:
    print(-1)

rongyao0422 3

n,m = map(int,input().strip().split())
height = [[0]*m for _ in range(n)]
for i in range(n):
    height[i] = list(map(int,input().strip().split()))
x,y,z,w = map(int,input().strip().split())


dp = {}
dirs = [[1,0],[-1,0],[0,1],[0,-1]]
# dp + dfs(dp的顺序)
def dfs(i,j):
    if i==z and j == w:
        return 1
    # 记忆化搜索
    if (i,j) in dp:
        return dp[(i,j)]
    cur = 0
    for nx,ny in dirs:
        if nx+i>=n or ny+j>=m or nx+i<0 or ny+j<0 or height[i][j]>=height[i+nx][j+ny]:
            continue
        cur += dfs(nx+i,ny+j)
    dp[(i,j)] = cur
    return cur
print(dfs(x,y))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值