商汤科技8月20日后端笔试
第三题做出来了,前两题都是90%。说超时了。是不是python太慢了。
1.一个字符串里的单词个数。按顺序找,每个字符用一次。
s = list(input()) glist = [] for temp in s: if temp == 'G': glist.append(temp) if temp == 'o': if 'Go' in glist: glist[glist.index('Go')] += temp continue elif'G' in glist: glist[glist.index('G')] += temp continue else: continue if temp == 'd': if 'Goo' in glist: glist[glist.index('Goo')] += temp else: continue print(glist.count('Good'))2.最长增长路径
N, M = map(int, input().split()) met = list() for i in range(N): temp = list(map(int, input().split())) met.append(temp) if not met: print(0) x = len(met) y = len(met[0]) lst = [] for i in range(x): for j in range(y): lst.append((met[i][j],i, j)) lst.sort() dp = [[1 for __ in range(y)]for __ in range(x)] #lst = sorted(sum([[(met[i][j], i, j)for j in range(y)]for i in range(x)],[])) for num,i,j in lst: dp[i][j] = 1 for di,dj in [(0,1),(1,0),(0,-1),(-1,0)]: r, c = i + di, j + dj if 0 <= r< x and 0 <= c < y: if met[i][j]> met[r][c]: dp[i][j] = max(dp[i][j],1+dp[r][c]) print(max([dp[i][j]for i in range(x)for j in range(y)]))3.不重叠区间。leetcode 435
class Solution(object): def eraseOverlapIntervals(self, intervals): """ :type intervals: List[List[int]] :rtype: int """ intervals.sort(key=lambda x:x[1], reverse= False) # return intervals count = 0 i = 0 while i < len(intervals)-1: if intervals[i][1] > intervals[i+1][0]: count += 1 del intervals[i+1] else: i += 1 return count菜菜的我终于也可以也这样一个帖子了,大家加油呀