Dataset Viewer
Auto-converted to Parquet Duplicate
_id
stringlengths
2
5
partition
stringclasses
2 values
text
stringlengths
5
289k
language
stringclasses
1 value
meta_information
dict
title
stringclasses
1 value
d1
train
for _ in range(int(input())): n = int(input()) mass = [] zo = 0 oz = 0 zz = 0 oo = 0 ozs = [] zos = [] ozss = set() zoss = set() for j in range(n): k = input() mass.append(k) if k[0] == '0' and k[-1] == '1': zoss.add(k) zos.appe...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1259/D" }
d4
train
def solve(): n, k = map(int,input().split()) lst = list(map(int,input().split())) lst.sort() ans = 0 for i in range(n - k - 1, n): ans += lst[i] print(ans) for i in range(int(input())): solve()
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1430/B" }
d7
train
import sys input = sys.stdin.readline for f in range(int(input())): n,m=list(map(int,input().split())) neig=[0]*n for i in range(n): neig[i]=[0] for i in range(m): a,b=list(map(int,input().split())) a-=1 b-=1 neig[a][0]+=1 neig[a].append(b) lev=[1...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1368/E" }
d10
train
for _ in range(int(input())): s = input() p = [i for i in s.split("0") if i!=""] p.sort(reverse=True) ans = 0 for i in range(0,len(p),2): ans+=len(p[i]) print(ans)
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1398/B" }
d13
train
from math import * mod = 1000000007 for zz in range(int(input())): n = int(input()) a = [ int(i) for i in input().split()] b = [int(i) for i in input().split()] ha = True hp = False hm = False for i in range(n): if b[i] != a[i]: if b[i] > a[i]: if (hp): ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1333/B" }
d16
train
from math import * zzz = int(input()) for zz in range(zzz): a, b, x, y = list(map(int, input().split())) print(max(x*b, (a-x-1)*b, y*a, (b - y - 1)*a))
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1315/A" }
d19
train
import math T = int(input()) for _ in range(T): n = int(input()) print(1/math.tan(math.pi/2/n))
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1354/C1" }
d22
train
t = int(input()) for _ in range(t): n = list(input().strip()) s = list(map(int, input().strip().split())) check = set(s) found = False for i in range(1, 1025): newset = set([e^i for e in s]) if check == newset: print(i) found = True break if ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1362/B" }
d25
train
for _ in range(int(input())): d=int(input()) anws=False if d**2>=4*d: root=(d**2-4*d)**0.5 a=(d+root)/2 b=(d-root)/2 anws=True if anws: print("Y {:.9f} {:.9f}".format(a,b)) else: print("N")
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1076/C" }
d28
train
tests = int(input()) for test in range(tests): n = int(input()) a = [int(i) for i in input().split()] d = {} for i in range(n): s = 0 while a[i] % 2 == 0: a[i] //= 2 s += 1 if a[i] in list(d.keys()): d[a[i]] = max(s, d[a[i]]) else: ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1259/B" }
d31
train
t = int(input()) for i in range(t): n = int(input()) s = input() ans = 0 for y in range(1, n): if s[y] == s[y-1]: ans += 1 print((ans + ans % 2) // 2)
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1437/B" }
d34
train
t=int(input()) for i in range(t): n=int(input()) print(2) print(n-1,n) for i in range(n-2,0,-1): print(i,i+2)
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1430/C" }
d37
train
n=int(input()) a=list(map(int,input().split())) k=[] for i in range(n): for j in range(a[i]): k.append(i+1) m=int(input()) b=list(map(int,input().split())) for i in b: print(k[i-1])
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/474/B" }
d40
train
t=int(input()) for tt in range(t): a,b,p=map(int,input().split()) s=input() n=len(s) cost = [0]*n cost[-1] = 0 typ = '' i=n-2 while i>=0: if s[i]==typ: cost[i] = cost[i+1] else: typ = s[i] cost[i] = cost[i+1] + (a if typ=='A' else b) ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1315/B" }
d43
train
LOG = 20 def solve(s): n = len(s) res = 0 z = 0 for t in range(0, n): if s[t] == '0': z += 1 continue for l in range(1, min(LOG, n - t + 1)): x = int(s[t:t+l], 2) # print(l, t, x, l + z) if l + z >= x: res += 1 # print(t, l, x, res, z) z = 0 return res t = int(input()) while t > 0: ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1217/C" }
d46
train
import sys import random from fractions import Fraction from math import * def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def finput(): return float(input()) def tinput(): return input().split() def linput(): return list(input()) def rinput(): return ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1419/B" }
d49
train
for haaghfj in range(int(input())): x,y,k = list(map(int,input().split())) print(k + (y * k + k - 1 +x-2) // (x - 1))
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1418/A" }
d52
train
def main(): t = int(input()) for z in range(t): n, k, d1, d2 = map(int, input().split()) if n % 3 != 0: print('no') continue f = 0 for i in [-1, +1]: for j in [-1, +1]: w = (k - i * d1 - j * d2) if f == 0 and (w ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/451/C" }
d55
train
for i in range(int(input())): n=int(input()) s=list(map(int,input().split())) a=0 for i in s: if i<2049:a+=i if a<2048:print("NO") else:print("YES")
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1221/A" }
d58
train
t = int(input()) for case in range(t): n = int(input()) arr = list(map(int, input().split())) if arr[-1] > arr[0]: print("YES") else: print("NO")
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1375/C" }
d61
train
n = int(input()) for _ in range(n): a, b = list(map(int, input().split())) print(a ^ b)
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1421/A" }
d64
train
def solve(): n, k = map(int,input().split()) lst1 = list(map(int,input().split())) lst1.sort(reverse=True) ind = 0 ans = 0 lst2 = list(map(int,input().split())) lst2.sort() for i in range(k): lst2[i] -= 1 if lst2[i] == 0: ans += lst1[ind] ans += lst1[ind] ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1369/C" }
d70
train
t = int(input()) for case in range(t): a, b = list(map(int, input().split())) s = input() z = 10000 total = 0 act = False for i in range(len(s)): cur = s[i] if cur == '0': z += 1 act = False else: if not act: act = Tr...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1443/B" }
d76
train
import math T = int(input()) for _ in range(T): n = int(input()) diags = 1/math.sin(math.pi/2/n) print(diags * math.cos(math.pi/4/n))
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1354/C2" }
d79
train
import sys q = int(sys.stdin.readline().strip()) for t in range(0, q): n, m = list(map(int, sys.stdin.readline().strip().split())) L = [] R = [0] * n C = [0] * m for i in range (0, n): L.append(sys.stdin.readline().strip()) for j in range (0, m): if L[i][j] != "*": ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1194/B" }
d82
train
for _ in range(int(input())): a, b, c = input(), input(), input() n = len(a) ok = True for i in range(n): if c[i] not in [a[i], b[i]]: ok = False print('YES' if ok else 'NO')
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1301/A" }
d85
train
# encoding: utf-8 from sys import stdin def solve(a): # root node of tries denotes empty stack stack = [None] node_stack = [[1, {}]] trie = node_stack[-1] counter = 0 for i in range(len(a)): el = a[i] if len(stack) == 0 or stack[-1] != el: current_node = node_sta...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1241/F" }
d88
train
import sys readline = sys.stdin.readline readlines = sys.stdin.readlines ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def gcd(a, b): while b: a, b = b, a%...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1389/E" }
d91
train
import math from collections import deque from sys import stdin, stdout from string import ascii_letters import sys letters = ascii_letters input = stdin.readline #print = stdout.write for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) can = list(map(int, input().split()))...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1418/B" }
d94
train
for tc in range(int(input())): n,m = list(map(int, input().split())) al = list(map(int, input().split())) bl = list(map(int, input().split())) aidx = {} for i,e in enumerate(al): aidx[e]=i midx = -1 res = 0 for i,e in enumerate(bl): idx = aidx[e] if idx <= midx: ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1279/C" }
d97
train
import sys from operator import itemgetter def count(a, b, num_a, num_b, cur_time): current_result = 0 #print('count time = ', cur_time, "num_a =", num_a, 'num_b = ', num_b) if num_a * a + num_b * b <= cur_time and cur_time >= 0: cur_time -= num_a * a + num_b * b current_result = num_a + num_b if num_a < tota...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1282/C" }
d100
train
for _ in range(int(input())): # a, b = map(int, input().split()) n = int(input()) # arr = list(map(int, input().split())) s = input() l = 0 r = n - 1 if s.count('0') == n: print(s) continue if s.count('1') == n: print(s) continue while s[l] == '0': ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1369/B" }
d103
train
s = [] for i in range(1, 10): k = 0 for l in range(1, 10): k *= 10 k += i s.append(k) s.sort() q = int(input()) while q: n = int(input()) l = 0 r = len(s) while l + 1 < r: m = (l + r) // 2 if s[m] <= n: l = m else: r = m ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1259/A" }
d106
train
import math t = int(input()) for test in range(t): n,k = map(int,input().split()) A = list(map(int,input().split())) A.sort() ans = 0 for i in range(1,n): if(A[i]>k): ans = 0 break rem = k-A[i] ans+=rem//A[0] print(ans)
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1417/A" }
d109
train
for _ in range(int(input())): n=int(input()) li=list(map(int,input().split())) ans=0 for i in range(n): if li[i]>=i: ans+=1 else: break for i in range(n): if li[n-1-i]>=i: ans+=1 else: break if ans>n: print("...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1291/B" }
d112
train
#!/usr/bin/env python # coding:utf-8 # Copyright (C) dirlt from sys import stdin def run(n, m, pixels): ans = 1 << 30 acc = [[0] * (m + 1) for _ in range(n + 1)] for i in range(n): for j in range(m): acc[i + 1][j + 1] = acc[i + 1][j] + int(pixels[i][j]) for j in range(m): ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/838/A" }
d115
train
import sys input = sys.stdin.readline import bisect t=int(input()) for testcases in range(t): n=int(input()) A=list(map(int,input().split())) m=int(input()) PS=[tuple(map(int,input().split())) for i in range(m)] PS.sort() K=[PS[-1]] for a,b in PS[::-1][1:]: if b<=K[-1][1]: ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1257/D" }
d118
train
from sys import stdin input = stdin.readline tests = int(input()) for test in range(tests): n, m = list(map(int, input().split())) a = [[0] * m for _ in range(n)] r = [[int(i) for i in input().split()] for _ in range(n)] c = [[int(i) for i in input().split()] for _ in range(m)] z = [[-1, -1] for _ ...
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1413/B" }
d121
train
import sys input = sys.stdin.readline t=int(input()) def calc(x): return x*(x+1)//2 for test in range(t): n,m=list(map(int,input().split())) ANS=calc(n) k=n-m q,mod=divmod(k,m+1) ANS-=calc(q+1)*mod+calc(q)*(m+1-mod) print(ANS)
PYTHON
{ "starter_code": "", "url": "https://codeforces.com/problemset/problem/1301/C" }
d124
train
import math class Solution: def numMusicPlaylists(self, N: int, L: int, K: int) -> int: s=0 c=0 r=0 x=math.factorial(N) while(True): c=x*((N-r-K)**(L-K))*(-1)**(r)//(math.factorial(N-r-K)*math.factorial(r)) if(c!=0): s=(s+c)%(10**9+7) ...
PYTHON
{ "starter_code": "\nclass Solution:\n def numMusicPlaylists(self, N: int, L: int, K: int) -> int:\n ", "url": "https://leetcode.com/problems/number-of-music-playlists/" }
d127
train
class Solution: def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int: n = len(s) count = collections.Counter(s[i : i + minSize] for i in range(0, n - minSize + 1)) res = 0 for k, v in count.items(): if len(set(k)) <= maxLetters: ...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:\n ", "url": "https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/" }
d130
train
class Solution: def maxScoreSightseeingPair(self, A: List[int]) -> int: curmaxsight = A[0] - 1 curmaxpair = 0 for sight in A[1:]: if sight + curmaxsight > curmaxpair: curmaxpair = sight + curmaxsight if sight > curmaxsight: curmaxsight ...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxScoreSightseeingPair(self, A: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/best-sightseeing-pair/" }
d133
train
class Solution: def mincostTickets(self, days: List[int], costs: List[int]) -> int: dp = [0] + [-1 for i in range(days[-1])] for day in days: dp[day] = 0 for i in range(1, len(dp)): if dp[i] == -1: dp[i] = dp[i-1] ...
PYTHON
{ "starter_code": "\nclass Solution:\n def mincostTickets(self, days: List[int], costs: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/minimum-cost-for-tickets/" }
d136
train
class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: j = 0 l = [] for i in pushed: l.append(i) while l and (l[-1] == popped[j]): l.pop() j += 1 if l: return False re...
PYTHON
{ "starter_code": "\nclass Solution:\n def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:\n ", "url": "https://leetcode.com/problems/validate-stack-sequences/" }
d139
train
class Solution: def getMaxLen(self, nums: List[int]) -> int: maxx = 0 nums.append(0) # starting position # where we find a 0 i = -1 minusarr = [] for j,n in enumerate(nums): if n == 0: # now figure out previous one...
PYTHON
{ "starter_code": "\nclass Solution:\n def getMaxLen(self, nums: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/" }
d142
train
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() lo = 0 hi = len(people) - 1 count = 0 while lo <= hi: count += 1 if people[lo] + people[hi] <= limit: lo += 1 h...
PYTHON
{ "starter_code": "\nclass Solution:\n def numRescueBoats(self, people: List[int], limit: int) -> int:\n ", "url": "https://leetcode.com/problems/boats-to-save-people/" }
d145
train
class Solution: def minSteps(self, n): """ :type n: int :rtype: int """ primeFactors=[] for i in range(2,int(n**.5)+1): while n%i==0: primeFactors.append(i) n=n//i if n>1: primeFactors.append(...
PYTHON
{ "starter_code": "\nclass Solution:\n def minSteps(self, n: int) -> int:\n ", "url": "https://leetcode.com/problems/2-keys-keyboard/" }
d148
train
class Solution: def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int: mod = 10**9+7 order = sorted(range(n), key=lambda i: efficiency[i], reverse=True) heap = [] filled = 0 rec = 0 speed_sum = 0 for i in order: ...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:\n ", "url": "https://leetcode.com/problems/maximum-performance-of-a-team/" }
d151
train
class Solution: def partitionDisjoint(self, A: List[int]) -> int: biggest = A[0] newbiggest = A[0] lenL = 1 total = 1 for itr in A[1:]: total += 1 if itr < biggest: lenL = total biggest = newbiggest else: ...
PYTHON
{ "starter_code": "\nclass Solution:\n def partitionDisjoint(self, A: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/partition-array-into-disjoint-intervals/" }
d154
train
class Solution: def makesquare(self, nums): """ :type nums: List[int] :rtype: bool """ if len(nums) < 4: return False length = sum(nums) if length % 4: return False length = (int) (length / 4) ...
PYTHON
{ "starter_code": "\nclass Solution:\n def makesquare(self, nums: List[int]) -> bool:\n ", "url": "https://leetcode.com/problems/matchsticks-to-square/" }
d157
train
import sys def dp(s1, s2, i, j, mem): if (i, j) in mem: return mem[(i, j)] elif i >= len(s1) and j >= len(s2): res = '' elif i >= len(s1): res = s2[j:] elif j >= len(s2): res = s1[i:] else: if s1[i] == s2[j]: res = s1[i] + dp(s1, s2, i+1, j+1, mem...
PYTHON
{ "starter_code": "\nclass Solution:\n def shortestCommonSupersequence(self, str1: str, str2: str) -> str:\n ", "url": "https://leetcode.com/problems/shortest-common-supersequence/" }
d160
train
from collections import deque class Solution: def constrainedSubsetSum(self, nums, k): N, queue = len(nums), deque() dp = [val for val in nums] for i, val in enumerate(nums): if queue and (i - queue[0] > k): queue.popleft() if queue and dp[queue[0]] > ...
PYTHON
{ "starter_code": "\nclass Solution:\n def constrainedSubsetSum(self, nums: List[int], k: int) -> int:\n ", "url": "https://leetcode.com/problems/constrained-subsequence-sum/" }
d163
train
class Solution: def longestCommonSubsequence(self, a: str, b: str) -> int: last, current = [0] * (len(b) + 1), [0] * (len(b) + 1) for i in range(len(a) - 1, -1, -1): for j in range(len(b) - 1, -1, -1): if a[i] == b[j]: current[j] = 1 + last[j ...
PYTHON
{ "starter_code": "\nclass Solution:\n def longestCommonSubsequence(self, text1: str, text2: str) -> int:\n ", "url": "https://leetcode.com/problems/longest-common-subsequence/" }
d166
train
class Solution: def findLongestWord(self, s, d): """ :type s: str :type d: List[str] :rtype: str """ result = '' for word in d: lo = 0 for l in word: lo = s.find(l, lo)+1 if lo == 0: ...
PYTHON
{ "starter_code": "\nclass Solution:\n def findLongestWord(self, s: str, d: List[str]) -> str:\n ", "url": "https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/" }
d169
train
from collections import Counter class Solution: def canConstruct(self, s: str, k: int) -> bool: if k > len(s): #return False return False counter = Counter(s) odd_counts = 0 for char in counter: if counter[char] % 2 == 1: odd_counts +=...
PYTHON
{ "starter_code": "\nclass Solution:\n def canConstruct(self, s: str, k: int) -> bool:\n ", "url": "https://leetcode.com/problems/construct-k-palindrome-strings/" }
d172
train
class Solution: def maxProduct(self, nums): """ :type nums: List[int] :rtype: int """ def prod(nums): #function to calculate product prod = 1 for i in nums: prod*=i return prod def listsplit(l...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/maximum-product-subarray/" }
d178
train
class Solution: def minWindow(self, s, t): """ :type s: str :type t: str :rtype: str """ left=-1 right = 0 result = "" totalMatch = 0 d = {} for c in t: d[c] = d.get(c, 0) + 1 for right in ran...
PYTHON
{ "starter_code": "\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n ", "url": "https://leetcode.com/problems/minimum-window-substring/" }
d181
train
class Solution: def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int: if startFuel >= target: return 0 heap = [] #record the reachable gas for now stop = 0 #total stops dist = startFuel #reachable distance for d, g in stations: i...
PYTHON
{ "starter_code": "\nclass Solution:\n def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:\n ", "url": "https://leetcode.com/problems/minimum-number-of-refueling-stops/" }
d184
train
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: # DP(a=index of last, b=index of last) = max of: # DP(a-1, b) # DP(a-1, i) + nums1[a] * max_or_min(nums2[i+1:b+1]) # same for b INF = int(1e9) n, m = len(nums1)...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/max-dot-product-of-two-subsequences/" }
d187
train
class Solution: def largestNumber(self, cost: List[int], target: int) -> str: dp = [0] + [-target]*target for t in range(1, target+1): dp[t] = max([dp[t-i] for i in cost if i<=t]+[dp[t]]) + 1 if dp[-1]<=0: return '0' res = '' for i in range(8, -1, -1): ...
PYTHON
{ "starter_code": "\nclass Solution:\n def largestNumber(self, cost: List[int], target: int) -> str:\n ", "url": "https://leetcode.com/problems/form-largest-integer-with-digits-that-add-up-to-target/" }
d190
train
class Solution: def preferences_to_scores(self, preferences): scores = {} for u, up in enumerate(preferences): for s, v in enumerate(up): scores[(u, v)] = s return scores def unhappy_friends(self, scores, a, b): ret = set() for ai, aa in e...
PYTHON
{ "starter_code": "\nclass Solution:\n def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:\n ", "url": "https://leetcode.com/problems/count-unhappy-friends/" }
d193
train
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() i = 0 j = len(piles) - 1 max_coins = 0 for i in range(len(piles) // 3, len(piles), 2): max_coins += piles[i] return max_coins
PYTHON
{ "starter_code": "\nclass Solution:\n def maxCoins(self, piles: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/maximum-number-of-coins-you-can-get/" }
d196
train
class Solution: def countTriplets(self, A: List[int]) -> int: counters = [0] * (1 << 16) counters[0] = len(A) for num in A: mask = (~num) & ((1 << 16) - 1) sm = mask while sm != 0: counters[sm] += 1 sm = (sm - 1) & mask ...
PYTHON
{ "starter_code": "\nclass Solution:\n def countTriplets(self, A: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/triples-with-bitwise-and-equal-to-zero/" }
d199
train
class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: dist = [ abs( ord(s[i]) - ord(t[i]) ) for i in range(len(s))] # i = 0 # cur = 0 # res = 0 # for j in range(len(s)): # cur += dist[j] # while cur>maxCost: # ...
PYTHON
{ "starter_code": "\nclass Solution:\n def equalSubstring(self, s: str, t: str, maxCost: int) -> int:\n ", "url": "https://leetcode.com/problems/get-equal-substrings-within-budget/" }
d202
train
class Solution: hash = {} def numTrees(self, n): """ :type n: int :rtype: int """ # return base case if n == 0: return 1 if n == 1 or n == 2: return n # try fetching from hash try: ...
PYTHON
{ "starter_code": "\nclass Solution:\n def numTrees(self, n: int) -> int:\n ", "url": "https://leetcode.com/problems/unique-binary-search-trees/" }
d205
train
class Solution: def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ # left = 0 # right = len(nums) - 1 # while left <= right: # mid = int((left + right)/2) # if nums[mid] == ta...
PYTHON
{ "starter_code": "\nclass Solution:\n def search(self, nums: List[int], target: int) -> int:\n ", "url": "https://leetcode.com/problems/search-in-rotated-sorted-array/" }
d208
train
class Solution: def largestNumber(self, nums): """ :type nums: List[int] :rtype: str """ nums = [str(n) for n in nums] nums.sort(reverse=True) for i in range(1, len(nums)): if len(nums[i-1]) > len(nums[i]): ...
PYTHON
{ "starter_code": "\nclass Solution:\n def largestNumber(self, nums: List[int]) -> str:\n ", "url": "https://leetcode.com/problems/largest-number/" }
d211
train
class Solution: def containsNearbyAlmostDuplicate(self, nums, k, t): """ :type nums: List[int] :type k: int :type t: int :rtype: bool """ if len(nums) < 2 or k <= 0 or t < 0: return False if t == 0: visited = set() fo...
PYTHON
{ "starter_code": "\nclass Solution:\n def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:\n ", "url": "https://leetcode.com/problems/contains-duplicate-iii/" }
d214
train
class Solution: def myPow(self, x, n): """ :type x: float :type n: int :rtype: float """ if n == 0: return 1 if abs(n) == 1: if n == 1: return x else: return 1/x if n > 0...
PYTHON
{ "starter_code": "\nclass Solution:\n def myPow(self, x: float, n: int) -> float:\n ", "url": "https://leetcode.com/problems/powx-n/" }
d217
train
class Solution: def minNumberOfFrogs(self, croakOfFrogs: str) -> int: # valid string? can be seperated into full croaks: ### dict of letters. c, r, o, a, k should all be equal, nothing else in if len(croakOfFrogs)%5!=0 or croakOfFrogs[0]!='c' or croakOfFrogs[-1]!='k': return -1 ...
PYTHON
{ "starter_code": "\nclass Solution:\n def minNumberOfFrogs(self, croakOfFrogs: str) -> int:\n ", "url": "https://leetcode.com/problems/minimum-number-of-frogs-croaking/" }
d220
train
class Solution: def longestWPI(self, hours: List[int]) -> int: ans, count, seen = 0, 0, {} for i, hour in enumerate(hours): count = count + 1 if hour > 8 else count - 1 if count > 0: ans = i + 1 else: if count not in seen: ...
PYTHON
{ "starter_code": "\nclass Solution:\n def longestWPI(self, hours: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/longest-well-performing-interval/" }
d223
train
class Solution: def lenLongestFibSubseq(self, A: List[int]) -> int: def getFS(x1, x2): F = [x1, x2] while F[-1] <= 1000000000: F.append(F[-2] + F[-1]) return F C1 = getFS(1, 0) C2 = C1[1:] def getLLFS(x1, x2): ...
PYTHON
{ "starter_code": "\nclass Solution:\n def lenLongestFibSubseq(self, A: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/" }
d226
train
INF = float('inf') class Solution: def pushDominoes(self, dominoes: str) -> str: n = len(dominoes) d1 = [-1] * n d2 = [-1] * n cnt = INF for i in range(n - 1, -1, -1): if dominoes[i] == 'L': cnt = 0 elif dominoes[i] == '.': ...
PYTHON
{ "starter_code": "\nclass Solution:\n def pushDominoes(self, dominoes: str) -> str:\n ", "url": "https://leetcode.com/problems/push-dominoes/" }
d229
train
class Solution: def maxVowels(self, s: str, k: int) -> int: n = len(s) vowel = set(['a','e','i','o','u']) i=0 res = 0 while i<k: if s[i] in vowel: res+=1 i+=1 j=k i=0 maxV = res while j<n: if ...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxVowels(self, s: str, k: int) -> int:\n ", "url": "https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/" }
d232
train
class Solution: def firstMissingPositive(self, nums): """ :type nums: List[int] :rtype: int """ nums = sorted(set(nums), key=lambda x: x) result = 0 for i in range(len(nums)): if nums[i] <= 0: continue elif nu...
PYTHON
{ "starter_code": "\nclass Solution:\n def firstMissingPositive(self, nums: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/first-missing-positive/" }
d235
train
class Solution: def minAddToMakeValid(self, S: str) -> int: if not S: return 0 stack = [] add = 0 for c in S: if c == '(': stack.append(c) elif c == ')': if stack: stack.pop() ...
PYTHON
{ "starter_code": "\nclass Solution:\n def minAddToMakeValid(self, S: str) -> int:\n ", "url": "https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/" }
d238
train
class Solution: def numSubarraysWithSum(self, pl, S): ans = 0 if(S == 0): c = 0 for i in range(len(pl)): if(pl[i] == 0): c+=1 else: c = 0 ans +=c return ans; ...
PYTHON
{ "starter_code": "\nclass Solution:\n def numSubarraysWithSum(self, A: List[int], S: int) -> int:\n ", "url": "https://leetcode.com/problems/binary-subarrays-with-sum/" }
d241
train
class Solution: def frequencySort(self, s): """ :type s: str :rtype: str """ counter = collections.Counter(s) colls = sorted(counter.items(), key=lambda k: k[1], reverse=True) res = '' for k, v in colls: res += k * v retu...
PYTHON
{ "starter_code": "\nclass Solution:\n def frequencySort(self, s: str) -> str:\n ", "url": "https://leetcode.com/problems/sort-characters-by-frequency/" }
d244
train
class Solution: def flipgame(self, fronts: List[int], backs: List[int]) -> int: w = set(fronts[i] for i in range(len(fronts)) if fronts[i] == backs[i]) x = set() for a in fronts: if a not in w: x.add(a) for a in backs: if a not in w: ...
PYTHON
{ "starter_code": "\nclass Solution:\n def flipgame(self, fronts: List[int], backs: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/card-flipping-game/" }
d247
train
class Solution: def replaceWords(self, dt, sentence): """ :type dict: List[str] :type sentence: str :rtype: str """ trie = {} for w in dt: t = trie for c in w: if c not in t: t[c] = {} t = t[...
PYTHON
{ "starter_code": "\nclass Solution:\n def replaceWords(self, dictionary: List[str], sentence: str) -> str:\n ", "url": "https://leetcode.com/problems/replace-words/" }
d250
train
class Solution: def numMagicSquaresInside(self, grid: List[List[int]]) -> int: # slide window and call isMagicSquare if len(grid) < 3 or len(grid[0]) < 3: return 0 rows = len(grid) cols = len(grid[0]) magic_squares = 0 for i in range(rows - 2): ...
PYTHON
{ "starter_code": "\nclass Solution:\n def numMagicSquaresInside(self, grid: List[List[int]]) -> int:\n ", "url": "https://leetcode.com/problems/magic-squares-in-grid/" }
d253
train
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: for i,r in enumerate(ranges): l = max(0,i-r) ranges[l] = max(i+r, ranges[l]) res = lo = hi = 0 while hi < n: lo, hi = hi, max(ranges[lo:hi+1]) if hi =...
PYTHON
{ "starter_code": "\nclass Solution:\n def minTaps(self, n: int, ranges: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/" }
d256
train
class Solution: def jump(self,nums): """ :type nums: List[int] :rtype: int """ if len(nums) == 1: return 0 else: step = 0 pos = 0 while pos != len(nums) - 1: bestStep = -1 bes...
PYTHON
{ "starter_code": "\nclass Solution:\n def jump(self, nums: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/jump-game-ii/" }
d259
train
class Solution: def originalDigits(self, s): """ :type s: str :rtype: str """ dmap={} dmap[0]=s.count('z') dmap[2]=s.count('w') dmap[4]=s.count('u') dmap[6]=s.count('x') dmap[8]=s.count('g') dmap[1]=s.count('o')-dmap...
PYTHON
{ "starter_code": "\nclass Solution:\n def originalDigits(self, s: str) -> str:\n ", "url": "https://leetcode.com/problems/reconstruct-original-digits-from-english/" }
d262
train
class Solution: def findKthLargest(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """ nums = sorted(nums, reverse=True) return nums[k - 1]
PYTHON
{ "starter_code": "\nclass Solution:\n def findKthLargest(self, nums: List[int], k: int) -> int:\n ", "url": "https://leetcode.com/problems/kth-largest-element-in-an-array/" }
d265
train
class Solution: def maxLength(self, arr: List[str]) -> int: def digit_representation(s): ans = 0 for c in s: ans |= 1<<(ord(c)-ord('a')) return ans A = sorted([(len(s), digit_representation(s)) for s in set(arr) if len(set(s))==len(s)], re...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxLength(self, arr: List[str]) -> int:\n ", "url": "https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/" }
d268
train
class Solution: def evalRPN(self, tokens): """ :type tokens: List[str] :rtype: int """ s = [] for token in tokens: if token == "+": a = int(s.pop()) b = int(s.pop()) s.append(a+b) elif token == "/": a = int(s.pop()) b = int(s.po...
PYTHON
{ "starter_code": "\nclass Solution:\n def evalRPN(self, tokens: List[str]) -> int:\n ", "url": "https://leetcode.com/problems/evaluate-reverse-polish-notation/" }
d271
train
import math class Solution: def __init__(self): self.happy_string = '' def getHappyString(self, n: int, k: int) -> str: # determine starting character poss_per_group = 2 ** (n - 1) group_num = math.ceil(k / poss_per_group) - 1 starting_char = '' # c...
PYTHON
{ "starter_code": "\nclass Solution:\n def getHappyString(self, n: int, k: int) -> str:\n ", "url": "https://leetcode.com/problems/the-k-th-lexicographical-string-of-all-happy-strings-of-length-n/" }
d274
train
class Solution: dp = {0: 0} def racecar(self, target: int) -> int: if target in self.dp: return self.dp[target] n = target.bit_length() if 2**n - 1 == target: self.dp[target] = n else: self.dp[target] = self.racecar(2**n - 1 - target) + n + 1 ...
PYTHON
{ "starter_code": "\nclass Solution:\n def racecar(self, target: int) -> int:\n ", "url": "https://leetcode.com/problems/race-car/" }
d277
train
class Solution: def findMinStep(self, board, hand): """ :type board: str :type hand: str :rtype: int """ res=float("inf") hmap=collections.defaultdict(int) for c in hand: hmap[c]+=1 res=self.helper(board,hmap) if...
PYTHON
{ "starter_code": "\nclass Solution:\n def findMinStep(self, board: str, hand: str) -> int:\n ", "url": "https://leetcode.com/problems/zuma-game/" }
d280
train
class Solution: def getPermutation(self, n, k): """ :type n: int :type k: int :rtype: str """ nums = list("123456789") k -= 1 factor = 1 for i in range(1, n): factor *= i res = [] for i in reversed(list(...
PYTHON
{ "starter_code": "\nclass Solution:\n def getPermutation(self, n: int, k: int) -> str:\n ", "url": "https://leetcode.com/problems/permutation-sequence/" }
d283
train
class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> int: dp = [[0 for _ in range(len(mat[0]) + 1)]for r in range(len(mat) + 1)] for r in range(1, len(mat) + 1): for c in range(1, len(mat[r-1]) + 1): dp[r][c] += mat[r-1][c-1] ...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:\n ", "url": "https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/" }
d286
train
class Solution: def smallestRangeII(self, A: List[int], K: int) -> int: if not A: return 0 nums = sorted([num + K for num in set(A)], reverse=True) max_num = nums[0] min_num = nums[-1] changed_max = max_num - 2 * K res = max_num - min_num for i in ...
PYTHON
{ "starter_code": "\nclass Solution:\n def smallestRangeII(self, A: List[int], K: int) -> int:\n ", "url": "https://leetcode.com/problems/smallest-range-ii/" }
d289
train
class Solution: def flipLights(self, n, m): """ :type n: int :type m: int :rtype: int """ states = set() for op_odd in [0, 1]: for op_even in [0, 1]: for op_third in [0, 1]: op_all = m - op_odd - op_ev...
PYTHON
{ "starter_code": "\nclass Solution:\n def flipLights(self, n: int, m: int) -> int:\n ", "url": "https://leetcode.com/problems/bulb-switcher-ii/" }
d292
train
class Solution: def numOfSubarrays(self, arr: List[int]) -> int: mod = 10**9+7 odd_presum_cnt = 0 par = 0 for a in arr: par ^= a & 1 if par: odd_presum_cnt += 1 return odd_presum_cnt * (len(arr)+1 - odd_presum_cnt)%mod
PYTHON
{ "starter_code": "\nclass Solution:\n def numOfSubarrays(self, arr: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/" }
d298
train
class Solution: def numTilePossibilities(self, tiles: str) -> int: res = 0 freqs = [f + 1 for f in Counter(tiles).values()] for t in itertools.product(*map(range, freqs)): n = sum(t) subtotal = math.factorial(n) for freq in t: subtotal //= ...
PYTHON
{ "starter_code": "\nclass Solution:\n def numTilePossibilities(self, tiles: str) -> int:\n ", "url": "https://leetcode.com/problems/letter-tile-possibilities/" }
d301
train
class Solution: def leastOpsExpressTarget(self, x: int, target: int) -> int: def dp(i, j): if i==0: return 2*j # if j==0: return 0 if j==1: return 2 if (i, j) in memo: return memo[(i, j)] base = x**i q, r = divmod(j, base) i...
PYTHON
{ "starter_code": "\nclass Solution:\n def leastOpsExpressTarget(self, x: int, target: int) -> int:\n ", "url": "https://leetcode.com/problems/least-operators-to-express-number/" }
d304
train
class Solution: def maxSumAfterPartitioning(self, arr, k): res = [0] for idx, val in enumerate(arr): max_val, cur_val = 0, 0 for i in range(max(0, idx-k+1), idx+1)[::-1]: if arr[i] > max_val: max_val =...
PYTHON
{ "starter_code": "\nclass Solution:\n def maxSumAfterPartitioning(self, arr: List[int], k: int) -> int:\n ", "url": "https://leetcode.com/problems/partition-array-for-maximum-sum/" }
d307
train
class Solution: def combinationSum4(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ cache = {} def f(val): if val == target: return 1 total = 0 remain = target - val ...
PYTHON
{ "starter_code": "\nclass Solution:\n def combinationSum4(self, nums: List[int], target: int) -> int:\n ", "url": "https://leetcode.com/problems/combination-sum-iv/" }
d310
train
from collections import Counter class Solution: def longestArithSeqLength(self, A: List[int]) -> int: c = dict(Counter(A).most_common()) # print(c) m1 = max(c.values()) # A = list(set(A)) # A.sort() index = {} # for i in range(len(A)): # index[A[i]...
PYTHON
{ "starter_code": "\nclass Solution:\n def longestArithSeqLength(self, A: List[int]) -> int:\n ", "url": "https://leetcode.com/problems/longest-arithmetic-subsequence/" }
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
6