Given a string s, return the string after replacing every uppercase letter with the same lowercase letter.
class Solution:
def toLowerCase(self, s: str) -> str:
return s.lower()
아스키로 계산하려 했는데 그거나 이거나..^^
You are given a list of songs where the ith song has a duration of time[i] seconds.
Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0.
class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
ans = 0
time.sort()
pairs = collections.defaultdict(int)
for i in range(len(time)):
tmp = (time[i] // 60) * 60 + 60 - time[i]
if time[i] in pairs:
ans += pairs[time[i]]
while tmp <= 500:
pairs[tmp] += 1
tmp += 60
return ans
우선 time
sort 해주고 pairs
라는 딕셔너리 만들기
times
값이 최대 500
이므로 적은편이라 생각해서
times[i]
의 가능한 짝꿍들을 모두 pairs
에 넣어주기
ex) times[i] = 30
이라면 60 - 30 = 30
부터 60
을 더한 값들이 짝꿍이 됨
(30, 90, 150, 210, ...) <= 500
ex) times[i] = 100
이라면 120 - 100 = 20
부터 시작~
(20, 80, 140, 200, ...) <= 500
첨엔 계속 timelimit 걸리는 방식만 생각나서 골 때렸네요..
class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
remainders = collections.defaultdict(int)
ret = 0
for t in time:
if t % 60 == 0: # check if a%60==0 && b%60==0
ret += remainders[0]
else: # check if a%60+b%60==60
ret += remainders[60-t%60]
remainders[t % 60] += 1 # remember to update the remainders
return ret
나머지를 딕셔너리에 넣어주기