description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads. The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path ...
def put(): return map(int, input().split()) def length(i, p, u, v): vis[i] = 1 max_i, max_here_i, sec_max_here_i = 0, 0, 0 for j in tree[i]: if j != p and (i, j) not in [(u, v), (v, u)]: max_j, max_here_j = length(j, i, u, v) if max_here_j >= max_here_i: ...
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VA...
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads. The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path ...
__author__ = "Darren" def solve(): def get_diameter(u): depth, v = dfs(u, set()) return dfs(v, set())[0] def dfs(u, visited): visited.add(u) max_depth, deepest_node = -1, u for v in adj_list[u]: if v not in visited: depth, w = dfs(v, visite...
ASSIGN VAR STRING FUNC_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR ASSIGN...
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads. The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path ...
def dfs(v): for u in graph[v]: if dist[u] == -1: dist[u] = dist[v] + 1 dfs(u) n = int(input()) graph = [[] for i in range(n)] edges = [] for i in range(n - 1): u, v = map(int, input().split()) edges.append((u - 1, v - 1)) graph[u - 1].append(v - 1) graph[v - 1].appe...
FUNC_DEF FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP V...
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads. The «Two Paths» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path ...
n = int(input()) p = [[] for i in range(n + 1)] for j in range(n - 1): a, b = map(int, input().split()) p[a].append(b) p[b].append(a) def g(b, c): x = y = d = 0 for a in p[b]: if a != c: s, z = g(a, b) z, y, x = sorted([x, y, z]) d = max(d, s) return...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR F...
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
class Solution: def isSubsequence(self, s, t): i = 0 if not s: return True m, n = len(t), len(s) for ch in t: if ch == s[i]: if i == n - 1: return True i += 1 return False
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
class Solution: def isSubsequence(self, s, t): length = len(s) idx = 0 for i in range(length): idx = t.find(s[i], idx) if idx == -1: return False idx += 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
class Solution: def isSubsequence(self, s, t): t = iter(t) return all(c in t for c in s) def isSubsequenceFei(self, s, t): p, l = 0, len(s) if l == p: return True for c_t in t: if s[p] == c_t: p += 1 if p == l: ...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
class Solution: def isSubsequence(self, s, t): start = 0 for i in range(len(s)): index = t[start:].find(s[i]) print(index) if index == -1: return False else: index += start start = index + 1 retu...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
class Solution: def isSubsequence(self, s, t): if len(s) > len(t): return False for i in s: if i in t: index = t.find(i) t = t[index + 1 :] else: return False return True
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
class Solution: def isSubsequence(self, s, t): if s == "": return True s_num = 0 temp = s[s_num] for i in t: if i == temp: s_num = s_num + 1 if s_num >= len(s): return True temp = s[s_num] ...
CLASS_DEF FUNC_DEF IF VAR STRING RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
class Solution: def isSubsequence(self, s, t): if len(s) == 0: return True if len(s) > len(t): return False if s[0] not in t: return False else: return self.isSubsequence(s[1:], t[t.index(s[0]) + 1 :])
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string ( A subsequence of a string is a new string which is formed from the original string by del...
class Solution: def isSubsequence(self, s, t): index = 0 ans = True for i in s: if i not in t[index:]: ans = False break else: index += t[index:].index(i) + 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): one, two = 1, 1 for i in range(n - 1): temp = one one = one + two two = temp return one % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def __init__(self): self.h = {} def countWays(self, n): if n == 0: return 1 if n < 0: return 0 elif self.h.get(n, -1) != -1: return self.h[n] self.h[n] = self.countWays(n - 1) + self.countWays(n - 2) return sel...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): ways = [-1] * (n + 1) return self.totalWays(n, ways, n) def totalWays(self, i, ways, n): if i == 0: return 1 elif i < 0: return 0 elif ways[i] != -1: return ways[i] else: m =...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): ans = 1 prev1 = 1 prev2 = 1 for i in range(2, n + 1): ans = prev1 + prev2 prev2 = prev1 prev1 = ans return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): one = 1 two = 1 mod = 1000000007 for i in range(2, n + 1): two, one = one + two, two return two % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): if n == 0 or n == 1: return 1 else: arr = [1] * (n + 1) for i in range(2, n + 1): arr[i] = arr[i - 1] + arr[i - 2] return arr[-1] % (10**9 + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
mod = 10**9 + 7 class Solution: def countWays(self, n): if n is 1: return 1 prev2 = 1 prev1 = 2 for i in range(2, n): cur = (prev2 + prev1) % mod prev2 = prev1 prev1 = cur return prev1
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def rec(self, i, dp): if i == -1: return 0 if i == 0: return 1 if dp[i] != -1: return dp[i] dp[i] = (self.rec(i - 1, dp) + self.rec(i - 2, dp)) % (10**9 + 7) return dp[i] def countWays(self, n): dp = [-1] * (n ...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETU...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): a = 1 b = 2 res = 0 if n == 1 or n == 2: return n while n > 2: n -= 1 res = (a + b) % (10**9 + 7) a = b b = res return res % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): mod = 10**9 + 7 arr = [1, 1] for i in range(2, n + 1): ele = (arr[i - 1] % mod + arr[i - 2] % mod) % mod arr.append(ele) return arr[n]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): if n == 1 or n == 2: return 1 if n == 1 else 2 mod = 10**9 + 7 ans = [(0) for _ in range(n)] ans[0] = 1 ans[1] = 2 for i in range(2, n): ans[i] = ans[i - 1] + ans[i - 2] return ans[n - 1] % mod
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BI...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): dp = [] ans = 0 for i in range(n + 1): if i == 0 or i == 1: dp.append(1) continue dp.append(dp[i - 1] + dp[i - 2]) return dp[-1] % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): dp = [] mod = 1000000000 + 7 dp.append(1) for i in range(1, n): dp.append((dp[i - 1] % mod + dp[i - 2] % mod) % mod) return dp[-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
mod = 10**9 + 7 class Solution: def helper(self, n, i, dp): if i == n: return 1 if i > n: return 0 if dp[i + 1] == -1: l = self.helper(n, i + 1, dp) dp[i + 1] = l else: l = dp[i + 1] if dp[i + 2] == -1: ...
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): if n == 1: return 1 if n == 2: return 2 dp = [] dp.append(1) dp.append(2) for i in range(2, n): dp.append(dp[i - 1] + dp[i - 2]) return dp[-1] % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): cache = {} def count(i): if i in cache: return cache[i] if i <= 2: return i res = count(i - 1) + count(i - 2) cache[i] = res return res return count(n) % 100...
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): _m = 10**9 + 7 if n <= 1: return n _p1 = 1 _p2 = 1 for _ in range(2, n + 1): _c = _p1 + _p2 _p2 = _p1 _p1 = _c return _p1 % _m
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): req = n - 1 prev, cur = 1, 1 tot = 2 if n == 1: return 1 if n == 2: return 2 for i in range(req - 2): newstep = prev + cur prev = cur cur = newstep tot += ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER R...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def sol(self, idx, dp): m = 1000000007 if idx <= 1: dp[idx] = 1 if dp[idx] != -1: return dp[idx] dp[idx] = (self.sol(idx - 1, dp) + self.sol(idx - 2, dp)) % m return dp[idx] def countWays(self, n): dp = [-1] * (n + 1) ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def driver(self, n): if not self.table[n - 1] == -1: return self.table[n - 1] else: ans = self.driver(n - 1) + self.driver(n - 2) self.table[n - 1] = ans return ans def countWays(self, n): self.table = [-1] * n if ...
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): if n == 1: return 1 prev2 = 1 prev1 = 2 for i in range(n - 2): s = prev1 + prev2 prev2 = prev1 prev1 = s return prev1 % 1000000007
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): dp = [(-1) for i in range(n + 1)] dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = dp[i - 1] + dp[i - 2] return dp[n] % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): if n in [1, 2]: return n a, b = 1, 2 for i in range(3, n + 1): temp = (a + b) % (10**9 + 7) a, b = b, temp return temp
CLASS_DEF FUNC_DEF IF VAR LIST NUMBER NUMBER RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): dp = [-1] * (n + 1) return self.solve(n, dp) def solve(self, n, dp): if n == 0: return 1 if n < 0: return 0 if dp[n] != -1: return dp[n] dp[n] = self.solve(n - 1, dp) + self.solve(n - 2,...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR BIN_OP BIN...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): l = [(0) for x in range(n + 1)] l[1] = 1 if n > 1: l[1], l[2] = 1, 2 for i in range(3, n + 1): l[i] = (l[i - 1] + l[i - 2]) % (10**9 + 7) return l[n] % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
mod = 1000000000.0 + 7 class Solution: def countWays(self, n): prev2 = 1 prev1 = 1 for i in range(2, n + 1): cur = (prev2 + prev1) % mod prev2 = prev1 prev1 = cur return int(prev1)
ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def recCountWays(self, index, dp): if index <= 1: return 1 if dp[index] != -1: return dp[index] downOneIndex = self.recCountWays(index - 1, dp) downTwoIndex = 0 if index > 1: downTwoIndex = self.recCountWays(index - 2, dp) ...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): dp = [-1] * (n + 1) dp[n] = 1 dp[n - 1] = 1 for i in range(n - 2, -1, -1): dp[i] = dp[i + 1] + dp[i + 2] return dp[0] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): l = [1, 2] for i in range(2, n): l.append(l[i - 1] + l[i - 2]) return l[n - 1] % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: c = 0 dp = [-1] * 10000 def gen(self, n, count): if count == n: return 1 if count == n - 1: return 1 if count < n and self.dp[count] != -1: return self.dp[count] l = self.gen(n, count + 1) r = self.gen(n, count + 2)...
CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def mul(self, A, B, MOD): K = len(A) C = [[(0) for i in range(K)] for j in range(K)] for i in range(1, K): for j in range(1, K): for k in range(1, K): C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD return C def powe...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMB...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): mod = 1000000007 dp = [-1] * (n + 5) def solve(n, dp): if n <= 2: dp[n] = n return dp[n] if dp[n] != -1: return dp[n] dp[n] = (solve(n - 1, dp) % mod + solve(n - 2, d...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR RET...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): mod = 1000000007 if n == 1: return 1 if n == 2: return 2 s1, s2 = 1, 2 for i in range(3, n + 1): s3 = s1 + s2 s2, s1 = s3, s2 return s3 % mod
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def fun(self, n, dp): if n == 0: return 1 if n < 0: return 0 if dp[n] != -1: return dp[n] oneStep = self.fun(n - 1, dp) twoStep = self.fun(n - 2, dp) dp[n] = (oneStep + twoStep) % (10**9 + 7) return dp[n] ...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR ...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
mod = 10**9 + 7 class Solution: def countWays(self, n): dp = [-1] * (n + 1) def memo(n): if n == 0 or n == 1: return 1 if dp[n] != -1: return dp[n] dp[n] = (memo(n - 1) + memo(n - 2)) % mod return dp[n] % mod ...
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR RET...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): a = 0 b = 1 for i in range(n): a, b = b, (a + b) % (10**9 + 7) return b
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def solve(self, n, dp): if n < 0: return 0 if dp[n] != -1: return dp[n] if n >= 2: dp[n] = self.solve(n - 1, dp) + self.solve(n - 2, dp) return dp[n] def countWays(self, n): if n <= 2 and n >= 0: return n ...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def recCountWays(self, index, dp): if index <= 1: return 1 if dp[index] != -1: return dp[index] downOneIndex = self.recCountWays(index - 1, dp) downTwoIndex = 0 if index > 1: downTwoIndex = self.recCountWays(index - 2, dp) ...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): dp = [-1] * (n + 1) return self.ways(n, dp) def ways(self, n, dp): if n <= 1: return 1 if dp[n] != -1: return dp[n] val = (self.ways(n - 1, dp) + self.ways(n - 2, dp)) % (10**9 + 7) dp[n] = val ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR RET...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): if n < 1: return 1 count = [(0) for _ in range(n + 1)] count[0] = count[1] = 1 for stair in range(2, n + 1): for step in range(1, 3): count[stair] += count[stair - step] count[stair] %= 1...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
import sys sys.setrecursionlimit(10**4) class Solution: def countWays(self, n): i = 0 count = 0 flag = 0 mem = n * [-1] return self.helper(n, i, count, mem) % (10**9 + 7) def helper(self, n, i, count, mem): if i == n: count = count + 1 ...
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR IF VAR VAR RETURN NUMBER IF...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): if n <= 2: return n step1 = 1 step2 = 2 div = 10**9 + 7 for i in range(n - 2): ans = (step1 + step2) % div step1 = step2 step2 = ans return step2
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): f = 1 s = 2 if n == 1: return f m = 10**9 + 7 for i in range(3, n + 1): cur = (s % m + f % m) % m f = s s = cur return s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): d = {} def dfs(n): if n not in d: if n <= 1: d[n] = 1 else: d[n] = (dfs(n - 1) + dfs(n - 2)) % (10**9 + 7) return d[n] return dfs(n)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def __init__(self): self.MODULAR_CONSTANT = 10**9 + 7 def countWays(self, n): dp = {} dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = (dp[i - 1] + dp[i - 2]) % self.MODULAR_CONSTANT return dp[n] % self.MODULAR_CONSTANT def ...
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUM...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): prev2 = 0 prev1 = 1 for i in range(1, n + 1): cur = (prev1 + prev2) % (10**9 + 7) prev2 = prev1 prev1 = cur return prev1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): fib1 = 1 fib2 = 2 if n == 1: return 1 if n == 2: return 2 total = 0 for ctr in range(2, n): total = fib1 + fib2 fib1 = fib2 fib2 = total return total % (10**9 ...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def sol(self, n): m = 1000000007 prev2 = 1 prev = 1 for i in range(2, n + 1): curr = (prev + prev2) % m prev2 = prev prev = curr return prev def countWays(self, n): return self.sol(n)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): if n == 0 or n == 1: return 1 dp = [(0) for i in range(n + 1)] dp[1] = 1 dp[0] = 1 for i in range(1, n + 1): dp[i] = int( (dp[i - 1] % (1000000000.0 + 7) + dp[i - 2] % (1000000000.0 + 7)) ...
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER NUMBER BIN_OP VAR BIN_OP ...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
mod = 10**9 + 7 class Solution: def helper(self, dp, n): if n == 2: return 2 elif n == 1: return 1 if n in dp: return dp[n] else: dp[n] = (self.helper(dp, n - 1) + self.helper(dp, n - 2)) % mod return dp[n] def count...
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VA...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): def fun(n): mod = 10**9 + 7 if n == 0: return 1 if n == 1: return 1 if arr[n] != -1: return arr[n] % mod else: arr[n] = (fun(n - 1) % mod + fu...
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR ASSIGN VAR L...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def sol(self, dp, n): m = 1000000007 dp[0] = 1 dp[1] = 1 for i in range(2, n + 1): dp[i] = (dp[i - 1] + dp[i - 2]) % m return dp[len(dp) - 1] def countWays(self, n): dp = [-1] * (n + 1) return self.sol(dp, n)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FU...
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def countWays(self, n): first, second = 1, 1 for i in range(2, n + 1): first, second = second, first + second return second % (10**9 + 7)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER
There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Count the number of ways, the person can reach the top (order does matter). Example 1: Input: n = 4 Output: 5 Explanation: You can reach 4th stair in 5 ways. Way 1: Climb 2 stairs at a...
class Solution: def recur(self, n, cache): if n in cache: return cache[n] if n == 0 or n == 1: cache[n] = 1 return cache[n] cache[n] = self.recur(n - 1, cache) + self.recur(n - 2, cache) return cache[n] def countWays(self, n): dp = [(...
CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBE...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if N >= K + W - 1: return 1 if N < K: return 0 if K == 0: return 1 probs = [0] * (W + 2) probs[0] = 1 probs[1] = 1 / W for i in range(1, K): ...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP V...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if K == 0: return 1 dp = [0] * (W + K) dp[0] = 1 sliding_sum = 1 for i in range(1, K + W): dp[i] = sliding_sum / W if i < K: sliding_sum += dp[i] ...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NU...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: dp = [0] * (N + W) for i in range(K, N + 1): dp[i] = 1 S = min(W, N - K + 1) for i in range(K - 1, -1, -1): dp[i] = S / W S += dp[i] - dp[i + W] return dp[0]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VA...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if K == 0: return 1 running_sum = 1 P = [1] for i in range(1, W): P.append(1 / W * running_sum) running_sum += P[-1] for i in range(W, K): P.append(1 / W * r...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIG...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution1: def new21Game(self, N: int, K: int, W: int) -> float: dp = [None] * (K + W) s = 0 for i in range(K, K + W): dp[i] = 1 if i <= N else 0 s += dp[i] for i in range(K - 1, -1, -1): dp[i] = s / W s = s - dp[i + W] + dp[i] ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VA...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if N > K + W - 1 or K == 0: return 1 dp = [0] * (K + W) dp[1] = 1 / W for i in range(2, K + 1): if i == W + 1: dp[i] = dp[i - 1] + (dp[W] - 1) / W else: ...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VA...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if K == 0: return 1.0 if K - 1 + W <= N: return 1.0 prob = [0.0] * (K + W) avg = 0 for i in range(W): prob[K + i] = float(K + i <= N) avg += prob[K + i] / W ...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUM...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: dp = [0.0] * (N + W + 1) for k in range(K, N + 1): dp[k] = 1.0 S = sum(dp[i] for i in range(K, K + W + 1)) for k in reversed(list(range(K))): dp[k] = S / float(W) S += dp[k] - d...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR ...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if K == 0: return 1 dp = [0] cur, temp = 1, 1 / W for i in range(1, W + 1): dp.append(cur / W) if i < K: cur += dp[-1] total = sum(dp[1 : min(W + 1, K)])...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER F...
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal proba...
class Solution: def new21Game(self, N: int, K: int, W: int) -> float: if N < K: return 0 if N > K - 1 + W: return 1 dp = [0] * (K + W) for k in range(K, N + 1): dp[k] = 1 S = N - K + 1 for k in range(K - 1, -1, -1): dp[...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VA...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def zeta(s): n = len(s) z = [0] * n l, r = 0, 0 for i in range(1, n): if i <= r: z[i] = min(r - i + 1, z[i - l]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 return z s =...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VA...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
import sys input = sys.stdin.readline s = input().rstrip() n = len(s) i = 1 j = 0 a = [0] * n a[0] = n while i < n: while i + j < n and s[j] == s[i + j]: j += 1 a[i] = j if j == 0: i += 1 continue k = 1 while i + k < n and k + a[k] < j: a[i + k] = a[k] k += 1...
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR WHILE VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHI...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
s = input() n = len(s) z = [0] * n l = 0 r = 0 for i in range(1, n): if s[i] == s[0]: if i < r: j = i + min(z[i - l], r - i) while j < n and s[j] == s[j - i]: j += 1 z[i] = j - i if j > r: l = i r = j els...
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN V...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def Count(s, t): res = 0 for i in range(len(s) - len(t) + 1): if s[i : i + len(t)] == t: res += 1 return res s = input() n = len(s) p = [0] * n z = [0] * n ans = [0] * n for i in range(1, n): p[i] = p[i - 1] while p[i] > 0 and s[i] != s[p[i]]: p[i] = p[p[i] - 1] if ...
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUM...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def z_advanced(s): Z = [0] * len(s) Z[0] = len(s) rt = 0 lt = 0 for k in range(1, len(s)): if k > rt: n = 0 while n + k < len(s) and s[n] == s[n + k]: n += 1 Z[k] = n if n > 0: lt = k rt = k + n -...
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIG...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
dep = dict() def kmp(st): tam = len(st) pi = [0] * tam for i in range(1, tam): j = pi[i - 1] while j > 0 and st[j] != st[i]: j = pi[j - 1] if st[j] == st[i]: pi[i] = j + 1 dep[pi[i]] = pi[pi[i] - 1] return pi s = input() arr = kmp(s) points = [...
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR N...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def preZ(s): n = len(s) z = [0] * n z[0] = n r = 0 if n == 1: return z while r + 1 < n and s[r] == s[r + 1]: r += 1 z[1] = r l = 1 if r > 0 else 0 for k in range(2, n): bl = r + 1 - k gl = z[k - l] if gl < bl: z[k] = z[k - l] ...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_O...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def getLPS(s2, l): lps = [0] * l i = 1 j = 0 while i < l: if s2[i] == s2[j]: lps[i] = j + 1 j += 1 i += 1 elif j != 0: j = lps[j - 1] else: lps[i] = 0 i += 1 return lps s = input() n = len(s) p = getLPS...
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN V...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
from itertools import accumulate def zfunc(s): n = len(s) z = [0] * n left = right = 0 for i in range(1, n): if i <= right: z[i] = min(z[i - left], right - i + 1) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > right: ...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR V...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
s = " " + input() n = len(s) r, c = [-1] * n, [1] * n for i in range(1, n): r[i] = r[i - 1] + 1 while r[i] and s[r[i]] != s[i]: r[i] = r[r[i] - 1] + 1 d, n = [], n - 1 for i in range(n, 1, -1): c[r[i]] += c[i] while n > 0: d.append(str(n) + " " + str(c[n])) n = r[n] print(len(d)) d.reverse()...
ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIS...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
s = input() n = len(s) p = [0] * n k = 0 for i in range(1, n): while k != 0 and s[k] != s[i]: k = p[k - 1] if s[k] == s[i]: k += 1 p[i] = k a = [] k = n while k != 0: a += [k] k = p[k - 1] c = [0] * (n + 1) for i in range(n): c[p[i]] += 1 for i in range(n - 1, 1, -1): c[p[i -...
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NUMBER VAR LIST VAR ASSIG...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
s = input() n = len(s) if n == 1: print(1) print(1, 1) quit() priya = [-1] * n q = -1 for i in range(1, n): while q >= 0 and s[i] != s[q + 1]: q = priya[q] if s[i] == s[q + 1]: q += 1 priya[i] = q cnt = [1] * n for i in range(n - 1, -1, -1): if priya[i] >= 0: cnt[...
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP V...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def computeLPSArray(pat, M, lps): len = 0 lps[0] i = 1 while i < M: if pat[i] == pat[len]: len += 1 lps[i] = len i += 1 elif len != 0: len = lps[len - 1] else: lps[i] = 0 i += 1 return lps def count_sub...
FUNC_DEF ASSIGN VAR NUMBER EXPR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
s = str(input()) lps = [0] * 100005 dp = [0] * 100005 ada = [0] * 100005 tunda = [0] * 100005 n = len(s) i = 1 j = 0 lps[0] = 0 while i < n: if s[i] == s[j]: j += 1 lps[i] = j i += 1 elif j == 0: lps[i] = 0 i += 1 else: j = lps[j - 1] for i in range(n - 1, -1,...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
s = input() n = len(s) p = [0] * (n + 1) ans = [0] * (n + 1) j = 0 li = [n] for i in range(1, n): while j > 0 and s[i] != s[j]: j = p[j - 1] if s[i] == s[j]: j += 1 p[i] = j for i in range(0, n): ans[p[i]] += 1 for i in range(n - 1, -1, -1): ans[p[i - 1]] += ans[i] for i in range(0, ...
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASS...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def compute_z(data): z = [(0) for _ in range(len(data))] z[0] = len(data) l = 0 r = 0 for i in range(1, len(data)): if i <= r: z[i] = min(z[i - l], r - i + 1) while i + z[i] < len(data) and data[z[i]] == data[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: ...
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
s = str(input()) n = len(s) z = [0] li = [-1] d = dict() ans = set() lis = [] def zf(s): l = r = 0 for i in range(1, n): z.append(0) if i <= r: z[-1] = min(z[i - l], r - i + 1) while i + z[i] < n and s[i + z[i]] == s[z[i]]: z[i] += 1 if i + z[i] - 1 > r:...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR BIN...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
from itertools import accumulate def z_algorithm(s): n = len(s) l, d = 1, 0 ans = [0] * n ans[0] = n while l < n: while l + d < n and s[d] == s[l + d]: d += 1 ans[l] = d if d == 0: l += 1 continue k = 1 while l + k < n and...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR WHILE VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def prefix_function(s): n = len(s) pi = [0] * n for i in range(1, n): j = pi[i - 1] while j > 0 and s[j] != s[i]: j = pi[j - 1] pi[i] = j + (s[j] == s[i]) return pi def solve(s): n = len(s) pi = prefix_function(s) j = n prefixes = [] while j: ...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
s = input() n = len(s) m = [0] * (n + 1) z = [0] * (n + 1) l = 0 r = 0 li = [n] for i in range(1, n): if i <= r: z[i] = min(r - i + 1, z[i - l]) while i + z[i] < n and s[z[i]] == s[i + z[i]]: z[i] += 1 if i + z[i] - 1 > r: l = i r = i + z[i] - 1 m[z[i]] += 1 if i + z[...
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP V...
You have a string s = s_1s_2...s_{|}s|, where |s| is the length of string s, and s_{i} its i-th character. Let's introduce several definitions: A substring s[i..j] (1 ≤ i ≤ j ≤ |s|) of string s is string s_{i}s_{i} + 1...s_{j}. The prefix of string s of length l (1 ≤ l ≤ |s|) is string s[1..l]. The suffix of strin...
def calculate_z_array(s): n = len(s) z = [0] * n z[0] = n l = 0 r = 0 for i in range(1, n): if i >= l and i <= r: z[i] = min(z[i - l], r - i + 1) while i + z[i] < n and s[z[i]] == s[z[i] + i]: z[i] += 1 if i + z[i] - 1 < n and i + z[i] - 1 > r: ...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR V...
Alice and Bob are playing a game. Initially, they are given a non-empty string $s$, consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty. Alice starts, then they alternate moves. In one move, a player takes either the first or the last let...
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): s = input()[:-1] n = len(s) l, r = 0, n - 1 while l < r: if s[l] == s[r]: l += 1 r -= 1 else: break s = s[l : r + 1] res = "Draw" for i in range(1, len(s), 2): ...
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR ...
Alice and Bob are playing a game. Initially, they are given a non-empty string $s$, consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty. Alice starts, then they alternate moves. In one move, a player takes either the first or the last let...
for _ in range(int(input())): s = input() l = 0 r = len(s) - 1 c = 0 while l < r and s[l] == s[r]: l += 1 r -= 1 for i in range(l, r, 2): if s[i] == s[i + 1]: continue c = 1 if c == 0: print("Draw") else: print("Alice")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CA...
Alice and Bob are playing a game. Initially, they are given a non-empty string $s$, consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty. Alice starts, then they alternate moves. In one move, a player takes either the first or the last let...
for _ in range(int(input())): n = input() while n and n[-1] == n[0]: n = n[1:-1] x = 1 for i in range(0, len(n), 2): x *= n[i] == n[i + 1] print(["Alice", "Draw"][x])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST STRING STRING VAR