Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.33 KB | None | 0 0
  1. # Problem 1
  2. class Solution(object):
  3.     def findLucky(self, arr):
  4.         """
  5.        :type arr: List[int]
  6.        :rtype: int
  7.        """
  8.         r = -1
  9.         for i in range(len(arr)):
  10.             if arr.count(i+1) == i+1:
  11.                 r = i+1
  12.         return r
  13.  
  14. # Problem 2
  15. class Solution(object):
  16.     def numTeams(self, rating):
  17.         """
  18.        :type rating: List[int]
  19.        :rtype: int
  20.        """
  21.         n = len(rating)
  22.         numTeams = 0
  23.         # Find the number of teams in increasing order
  24.         for i in range(n):
  25.             for j in range(i,n):
  26.                 if rating[j] <= rating[i]:
  27.                     continue
  28.                 for k in range(j,n):
  29.                     if rating[k] <= rating[j]:
  30.                         continue
  31.                     numTeams += 1
  32.         for i in range(n):
  33.             for j in range(i,n):
  34.                 if rating[j] >= rating[i]:
  35.                     continue
  36.                 for k in range(j,n):
  37.                     if rating[k] >= rating[j]:
  38.                         continue
  39.                     numTeams += 1
  40.         return numTeams
  41.                    
  42.  
  43. # Problem 3
  44. class UndergroundSystem(object):
  45.  
  46.     def __init__(self):
  47.         self.ids = {}       # A dict of each ID along with start/stop stations and their respective times.
  48.         self.pairs = {}  # A dict of station names where data entries are lists of tuples of start/end times.
  49.  
  50.     def checkIn(self, id, stationName, t):
  51.         """
  52.        :type id: int
  53.        :type stationName: str
  54.        :type t: int
  55.        :rtype: None
  56.        """
  57.         self.ids[id] = [stationName, t, 0]
  58.        
  59.  
  60.     def checkOut(self, id, stationName, t):
  61.         """
  62.        :type id: int
  63.        :type stationName: str
  64.        :type t: int
  65.        :rtype: None
  66.        """
  67.         self.ids[id][2] = t     # Update to the starting time
  68.         pair = (self.ids[id][0], stationName)
  69.         if pair not in self.pairs:
  70.             self.pairs[pair] = []
  71.         self.pairs[pair].append(t - self.ids[id][1])
  72.  
  73.     def getAverageTime(self, startStation, endStation):
  74.         """
  75.        :type startStation: str
  76.        :type endStation: str
  77.        :rtype: float
  78.        """
  79.         pair = (startStation, endStation)
  80.         pairs = self.pairs
  81.         return sum(pairs[pair])/float(len(pairs[pair]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement