add program
This commit is contained in:
23
编程题/数的划分.py
Normal file
23
编程题/数的划分.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# import lib
|
||||
#https://blog.csdn.net/elma_tww/article/details/86538836
|
||||
# def functions
|
||||
#dp[i][j]表示将整数i划分为j份的方案数
|
||||
#dp[i-j],拿出j个数,分别放上1,dp[i-j][2],2表示分为两份
|
||||
def divide(n,k,dp):
|
||||
for i in range(1,n+1):
|
||||
for j in range(1,k+1):
|
||||
if i>=j:#划分的分数不能超过给定的整数
|
||||
dp[i][j]=dp[i-j][j]+dp[i-1][j-1]#裂项相消
|
||||
print(i,j,dp[i][j])
|
||||
return dp[n][k]
|
||||
|
||||
#main
|
||||
if __name__ == "__main__":
|
||||
n,k = map(int,input().split(','))
|
||||
# n = int(n)
|
||||
# k = int(k)
|
||||
dp = [[0] *(k+1) for i in range(n+1)]
|
||||
dp [0][0]=1
|
||||
#print(dp)
|
||||
result = divide(n,k,dp)
|
||||
print(result)
|
||||
15
编程题/数组平衡点.py
Normal file
15
编程题/数组平衡点.py
Normal file
@@ -0,0 +1,15 @@
|
||||
def findBalancedIndex(arr):
|
||||
for temp in range(1,len(arr)):
|
||||
# a=sum(arr[:temp])
|
||||
# print(a)
|
||||
# b=sum(arr[temp+1:])
|
||||
# print(b)
|
||||
if sum(arr[:temp])==sum(arr[temp+1:]):
|
||||
#print(temp)
|
||||
return temp
|
||||
result1 = findBalancedIndex([1,2,3,4,6])
|
||||
result2 = findBalancedIndex([1,2,1])
|
||||
result3 = findBalancedIndex([1,2,2,2,1])
|
||||
print(result1)
|
||||
print(result2)
|
||||
print(result3)
|
||||
35
编程题/阿里/子集.py
Normal file
35
编程题/阿里/子集.py
Normal file
@@ -0,0 +1,35 @@
|
||||
#x,y先按照x排序,x相同的情况下,y按照从大到小排序,找递增不连续的最长子序列
|
||||
#import lib
|
||||
from bisect import bisect_left#二分查找
|
||||
#def functions
|
||||
def sub_set(n,x,y):
|
||||
#(),x[0],x从小到大,-[1],x相同的情况下,y从大到小
|
||||
ordered_list=sorted(zip(x,y),key=lambda x: (x[0],-x[1]))
|
||||
#length = len(ordered_list),n=length
|
||||
total =0
|
||||
long_sub_set =[ordered_list[0][1]]
|
||||
|
||||
for i in range(1,n):
|
||||
if ordered_list[i][1]>long_sub_set[-1]:
|
||||
long_sub_set.append(ordered_list[i][1])#大于就入栈
|
||||
else:
|
||||
#在long_sub_set中找到ordered_list[i][1]合适的插入点以维持有序
|
||||
index = bisect_left(long_sub_set,ordered_list[i][1])#返回插入索引,靠左
|
||||
long_sub_set[index]=ordered_list[i][1]#替换之前的元素
|
||||
|
||||
|
||||
return len(long_sub_set)
|
||||
|
||||
#main
|
||||
|
||||
if __name__ == "__main__":
|
||||
T = int(input())
|
||||
for i in range(T):
|
||||
n = int(input())
|
||||
#x=list(map(int,input().split(" ")))
|
||||
#y=list(map(int,input().split(" ")))
|
||||
x=map(int,input().split(" "))
|
||||
y=map(int,input().split(" "))
|
||||
result=sub_set(n,x,y)
|
||||
print(result)
|
||||
|
||||
Reference in New Issue
Block a user