LeetCode 181th competition

1、在整数数组中,如果一个整数的出现频次和它的数值大小相等,我们就称这个整数为「幸运数」。给你一个整数数组 arr,请你从中找出并返回一个幸运数。
如果数组中存在多个幸运数,只需返回最大的那个。如果数组中不含幸运数,则返回 -1 。

示例 1:
输入:arr = [2,2,3,4]
输出:2
解释:数组中唯一的幸运数是 2 ,因为数值 2 的出现频次也是 2 。

链接:https://leetcode-cn.com/problems/find-lucky-integer-in-an-array

class Solution:
    def findLucky(self, arr: List[int]) -> int:
        res = {}
        ans = 0
       
        for i in arr:
            res.setdefault(i,0)
            res[i] += 1

        for key, value in res.items():
            if key == value and key > ans:
                ans = key
        
        return ans if ans else -1

2、n 名士兵站成一排。每个士兵都有一个 独一无二 的评分 rating 。
每 3 个士兵可以组成一个作战单位,分组规则如下:
1)从队伍中选出下标分别为 i、j、k 的 3 名士兵,他们的评分分别为 rating[i]、rating[j]、rating[k]
2)作战单位需满足: rating[i] < rating[j] < rating[k] 或者 rating[i] > rating[j] > rating[k] ,其中 0 <= i < j < k < n
请你返回按上述条件可以组建的作战单位数量。每个士兵都可以是多个作战单位的一部分。

示例 1:
输入:rating = [2,5,3,4,1]
输出:3
解释:我们可以组建三个作战单位(2,3,4)、(5,4,1)、(5,3,1) 。

class Solution:
    def numTeams(self, rating: List[int]) -> int:
        def f(S):
            n = len(S)
            ans = 0
            for i in range(n):
                l = 0
                r = 0
                for j in range(0,i):
                    if S[j]<S[i]:
                        l += 1
                for j in range(i+1,n):
                    if S[j]>S[i]:
                        r += 1
                ans += l*r
            return ans
        ans = f(rating)
        rating.reverse()
        ans += f(rating)
        return ans

3、请你实现一个类 UndergroundSystem ,它支持以下 3 种方法:

  1. checkIn(int id, string stationName, int t)编号为 id 的乘客在 t 时刻进入地铁站 stationName 。
    一个乘客在同一时间只能在一个地铁站进入或者离开。
  2. checkOut(int id, string stationName, int t)编号为 id 的乘客在 t 时刻离开地铁站 stationName 。
  3. getAverageTime(string startStation, string endStation) 返回从地铁站 startStation 到地铁站 endStation 的平均花费时间。平均时间计算的行程包括当前为止所有从 startStation 直接到达 endStation 的行程。调用 getAverageTime 时,询问的路线至少包含一趟行程。

你可以假设所有对 checkIn 和 checkOut 的调用都是符合逻辑的。也就是说,如果一个顾客在 t1 时刻到达某个地铁站,那么他离开的时间 t2 一定满足 t2 > t1 。所有的事件都按时间顺序给出。
LeetCode 181th competition_第1张图片

class UndergroundSystem:

    def __init__(self):
        self.timecnts = dict()
        self.passengers = dict()


    def checkIn(self, id: int, stationName: str, t: int) -> None:
        self.passengers[id] = (stationName, t)


    def checkOut(self, id: int, stationName: str, t: int) -> None:
        start, t0 = self.passengers[id]
        trip = '{}->{}'.format(start, stationName)
        tspend = t - t0
        if trip not in self.timecnts:
            self.timecnts[trip] = (tspend, 1)
        else:
            tott, totc = self.timecnts[trip]
            self.timecnts[trip] = (tott + tspend, totc + 1)


    def getAverageTime(self, startStation: str, endStation: str) -> float:
        trip = '{}->{}'.format(startStation, endStation)
        tott, totc = self.timecnts[trip]
        return float(tott) / totc


# Your UndergroundSystem object will be instantiated and called as such:
# obj = UndergroundSystem()
# obj.checkIn(id,stationName,t)
# obj.checkOut(id,stationName,t)
# param_3 = obj.getAverageTime(startStation,endStation)

4、给你两个长度为 n 的字符串 s1 和 s2 ,以及一个字符串 evil 。请你返回 好字符串 的数目。
好字符串 的定义为:它的长度为 n ,字典序大于等于 s1 ,字典序小于等于 s2 ,且不包含 evil 为子字符串。
由于答案可能很大,请你返回答案对 10^9 + 7 取余的结果。

示例 1:
输入:n = 2, s1 = “aa”, s2 = “da”, evil = “b”
输出:51
解释:总共有 25 个以’a’ 开头的好字符串:“aa”,“ac”,“ad”,…,“az”。还有 25 个以 'c’开头的好字符串:“ca”,“cc”,“cd”,…,“cz”。最后,还有一个以 ‘d’ 开头的好字符串:“da”。

class Solution:
    def findGoodStrings(self, n: int, s1: str, s2: str, evil: str) -> int:
        
        
        MOD = int(1e9 + 7)
        m = len(evil)
        orda = ord('a')
        ordz = ord('z')
        
        nxt = [-1] * (m + 1)
        i = 0
        j = -1
        while i < m:
            if j == -1 or evil[i] == evil[j]:
                i += 1
                j += 1
                nxt[i] = j
            else:
                j = nxt[j]
        
        from functools import lru_cache
        
        @lru_cache(None)
        def dfs(idx, reach1, reach2, match):
            if match == m - 1: return 0
            if idx == n:
                return 1
        
            s = ord(s1[idx])
            e = ord(s2[idx])
            res = 0
            lim1 = s if reach1 else orda
            lim2 = e + 1 if reach2 else ordz + 1
            for i in range(lim1, lim2):
                ch = chr(i)
                if ch == evil[match + 1]:
                    nxtmat = match + 1
                else:
                    nxtmat = match + 1
                    while nxtmat != -1 and ch != evil[nxtmat]:
                        nxtmat = nxt[nxtmat]
                res += dfs(idx + 1, reach1 and i == s, reach2  and i == e, nxtmat)
            return res % MOD
        
        
        return dfs(0, True, True, -1)

你可能感兴趣的:(leetcode)