description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell).
You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following.
Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell.
Example 1:
Input:
N = 8
arr[] = {3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 21, 9}
Output: 3
Explanation:
The given board is the board shown
in the figure. For the above board
output will be 3.
a) For 1st throw get a 2.
b) For 2nd throw get a 6.
c) For 3rd throw get a 2.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10
1 ≤ arr[i] ≤ 30 | class Pos:
def __init__(self, pos, dist):
self.pos = pos
self.dist = dist
class Solution:
def findNextMove(self, arr, pos):
while pos in arr and arr.index(pos) % 2 == 0:
pos_index = arr.index(pos)
pos = arr[pos_index + 1]
return pos
def minThrow(self, N, arr):
if N == 0:
return -1
q = [(1, 0)]
visited = [0] * 31
visited[1] = 1
while len(q):
current_node, curr_dist = q.pop(0)
if current_node == 30:
return curr_dist
for i in range(1, min(current_node + 7, 31)):
if not visited[i]:
neig_pos = self.findNextMove(arr, i)
visited[neig_pos] = 1
q.append((neig_pos, curr_dist + 1))
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF WHILE VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell).
You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following.
Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell.
Example 1:
Input:
N = 8
arr[] = {3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 21, 9}
Output: 3
Explanation:
The given board is the board shown
in the figure. For the above board
output will be 3.
a) For 1st throw get a 2.
b) For 2nd throw get a 6.
c) For 3rd throw get a 2.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10
1 ≤ arr[i] ≤ 30 | class Solution:
def minThrow(self, n, arr):
index = [-1] * 31
visit = [-1] * 31
for i in range(0, len(arr), 2):
index[arr[i]] = arr[i + 1]
que = []
que.append(1)
count = 0
while len(que) > 0:
l = len(que)
while l > 0:
l -= 1
el = que.pop(0)
if visit[el] != -1:
continue
else:
visit[el] = 1
if el == 30:
return count
for i in range(el + 1, el + 7, 1):
if i > 30:
break
if index[i] == -1:
que.append(i)
else:
que.append(index[i])
count += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell).
You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following.
Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell.
Example 1:
Input:
N = 8
arr[] = {3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 21, 9}
Output: 3
Explanation:
The given board is the board shown
in the figure. For the above board
output will be 3.
a) For 1st throw get a 2.
b) For 2nd throw get a 6.
c) For 3rd throw get a 2.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10
1 ≤ arr[i] ≤ 30 | class Solution:
def minThrow(self, N, arr):
def get_neighbours(i):
neighbours = []
for val in range(i + 1, i + 7):
if val > 30:
continue
neighbours.append(ladder_snake_map.get(val, val))
return neighbours
ladder_snake_map = {}
i = 0
while i < 2 * N:
ladder_snake_map[arr[i]] = arr[i + 1]
i += 2
visited = [(False) for i in range(30)]
q = [(ladder_snake_map.get(1, 1), 0)]
while q:
val, tries = q.pop(0)
if val == 30:
return tries
for neighbour in get_neighbours(val):
if visited[neighbour - 1]:
continue
visited[neighbour - 1] = True
q.append((neighbour, tries + 1))
return -1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell).
You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following.
Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell.
Example 1:
Input:
N = 8
arr[] = {3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 21, 9}
Output: 3
Explanation:
The given board is the board shown
in the figure. For the above board
output will be 3.
a) For 1st throw get a 2.
b) For 2nd throw get a 6.
c) For 3rd throw get a 2.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10
1 ≤ arr[i] ≤ 30 | class Solution:
def minThrow(self, N, arr):
d = {}
for i in range(N):
d[arr[2 * i]] = arr[2 * i + 1]
q = []
v = set()
v.add(1)
q.append((1, 0))
while q:
a, b = q.pop(0)
if a == 30:
return b
for i in range(1, 7):
if a + i <= 30:
if a + i in d and d[a + i] not in v:
q.append((d[a + i], b + 1))
v.add(d[a + i])
elif a + i not in d and a + i not in v:
q.append((a + i, b + 1))
v.add(a + i)
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER |
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell).
You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following.
Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell.
Example 1:
Input:
N = 8
arr[] = {3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 21, 9}
Output: 3
Explanation:
The given board is the board shown
in the figure. For the above board
output will be 3.
a) For 1st throw get a 2.
b) For 2nd throw get a 6.
c) For 3rd throw get a 2.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10
1 ≤ arr[i] ≤ 30 | class Solution:
def minThrow(self, n, arr):
N = 30
dp = [1 << 32] * (N + 1)
dp[0] = 0
dp[1] = 0
ladder = {}
snake = {}
for i in range(0, 2 * n, 2):
if arr[i] < arr[i + 1]:
if arr[i + 1] in ladder:
if arr[ladder[arr[i + 1]] - 1] > arr[i]:
ladder[arr[i + 1]] = i + 1
else:
ladder[arr[i + 1]] = i + 1
else:
snake[arr[i]] = i
for i in range(2, N + 1):
if i in snake:
continue
elif i in ladder:
dp[i] = dp[arr[ladder[i] - 1]]
for j in range(1, 7):
if i - j >= 1:
dp[i] = min(dp[i], dp[i - j] + 1)
return dp[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER |
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell).
You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following.
Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell.
Example 1:
Input:
N = 8
arr[] = {3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 21, 9}
Output: 3
Explanation:
The given board is the board shown
in the figure. For the above board
output will be 3.
a) For 1st throw get a 2.
b) For 2nd throw get a 6.
c) For 3rd throw get a 2.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10
1 ≤ arr[i] ≤ 30 | class Solution:
def minThrow(self, N, arr):
l = []
s = arr
s1 = []
for i in range(0, len(s), 2):
if s[i] < s[i + 1]:
l.append(s[i])
l.append(s[i + 1])
else:
s1.append(s[i])
for i in range(0, len(l), 2):
if i < len(l) - 2 and l[i + 1] > l[i + 2]:
if l[i + 1] - l[i] < l[i + 3] - l[i + 2]:
l[i] = -1
l[i + 1] = -1
l1 = []
for i in l:
if i != -1:
l1.append(i)
l = l1
ans = 1
count = 0
while ans < 30:
maxvalue = ans
for i in range(0, len(l), 2):
if l[i] <= ans + 6 and l[i] >= ans:
maxvalue = max(maxvalue, l[i + 1])
if maxvalue < ans + 6:
for k in range(6, -1, -1):
if ans + k not in s1:
maxvalue = ans + k
break
count += 1
ans = maxvalue
return count | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell).
You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following.
Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell.
Example 1:
Input:
N = 8
arr[] = {3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 21, 9}
Output: 3
Explanation:
The given board is the board shown
in the figure. For the above board
output will be 3.
a) For 1st throw get a 2.
b) For 2nd throw get a 6.
c) For 3rd throw get a 2.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10
1 ≤ arr[i] ≤ 30 | class Solution:
def minThrow(self, N, arr):
mp = {}
for i in range(N):
start = arr[2 * i]
end = arr[2 * i + 1]
mp[start] = end
q = []
visited = [False] * 31
q.append([1, 0])
visited[1] = True
while q:
curr_cell, steps = q.pop(0)
if curr_cell == 30:
return steps
for dice in range(1, 7):
new_cell = curr_cell + dice
if new_cell in mp:
new_cell = mp[new_cell]
if new_cell <= 30 and not visited[new_cell]:
q.append([new_cell, steps + 1])
visited[new_cell] = True
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER |
Given a 5x6 snakes and ladders board, find the minimum number of dice throws required to reach the destination or last cell (30^{th} cell) from the source (1st cell).
You are given an integer N denoting the total number of snakes and ladders and an array arr[] of 2*N size where 2*i and (2*i + 1)^{th} values denote the starting and ending point respectively of i^{th }snake or ladder. The board looks like the following.
Note: Assume that you have complete control over the 6 sided dice. No ladder starts from 1st cell.
Example 1:
Input:
N = 8
arr[] = {3, 22, 5, 8, 11, 26, 20, 29,
17, 4, 19, 7, 27, 1, 21, 9}
Output: 3
Explanation:
The given board is the board shown
in the figure. For the above board
output will be 3.
a) For 1st throw get a 2.
b) For 2nd throw get a 6.
c) For 3rd throw get a 2.
Your Task:
You do not need to read input or print anything. Your task is to complete the function minThrow() which takes N and arr as input parameters and returns the minimum number of throws required to reach the end of the game.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10
1 ≤ arr[i] ≤ 30 | class Solution:
def minThrow(self, N, arr):
snakes = {}
ladder = {}
for i in range(0, 2 * N, 2):
if arr[i] < arr[i + 1]:
ladder[arr[i]] = arr[i + 1]
else:
snakes[arr[i]] = arr[i + 1]
queue = [(1, 0)]
while len(queue):
pos, move = queue.pop(0)
if pos == 30:
return move
for i in range(1, 7):
temp = pos + i
if temp <= 30:
if temp in snakes:
continue
if temp in ladder:
queue.append((ladder[temp], move + 1))
else:
queue.append((temp, move + 1))
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
m = len(grid) - 1
n = len(grid[0]) - 1
dic = dict()
s = self.minPathSumHelper(grid, 0, 0, m, n, dic)
return s
def minPathSumHelper(self, grid, i, j, m, n, dic):
if (i, j, m, n) in dic:
return dic[i, j, m, n]
if i > m or j > n:
return 0
elif i == m:
dic[i, j, m, n] = (
self.minPathSumHelper(grid, i, j + 1, m, n, dic) + grid[i][j]
)
return dic[i, j, m, n]
elif j == n:
dic[i, j, m, n] = (
self.minPathSumHelper(grid, i + 1, j, m, n, dic) + grid[i][j]
)
return dic[i, j, m, n]
else:
dic[i, j, m, n] = (
min(
self.minPathSumHelper(grid, i + 1, j, m, n, dic),
self.minPathSumHelper(grid, i, j + 1, m, n, dic),
)
+ grid[i][j]
)
return dic[i, j, m, n] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR VAR VAR |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
m = len(grid)
n = len(grid[0])
s = [[(0) for j in range(n)] for i in range(m)]
s[0][0] = grid[0][0]
for i in range(1, m):
s[i][0] = s[i - 1][0] + grid[i][0]
for j in range(1, n):
s[0][j] = s[0][j - 1] + grid[0][j]
for i in range(1, m):
for j in range(1, n):
s[i][j] = min(s[i - 1][j] + grid[i][j], s[i][j - 1] + grid[i][j])
return s[m - 1][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
M = len(grid)
if M == 0:
return 0
N = len(grid[0])
if N == 0:
return 0
INF = float("inf")
mem = {}
def min_path(i, j):
if i == M - 1 and j == N - 1:
return grid[i][j]
if (i, j) in mem:
return mem[i, j]
min_sum = INF
if i < M - 1:
min_sum = min_path(i + 1, j)
if j < N - 1:
min_sum = min(min_sum, min_path(i, j + 1))
mem[i, j] = grid[i][j] + min_sum
return mem[i, j]
return min_path(0, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
if not grid:
return 0
row_count = len(grid)
col_count = len(grid[0])
for i in range(1, row_count):
grid[i][0] += grid[i - 1][0]
for i in range(1, col_count):
grid[0][i] += grid[0][i - 1]
for row in range(1, row_count):
for col in range(1, col_count):
grid[row][col] += min(grid[row - 1][col], grid[row][col - 1])
return grid[-1][-1] | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
height = len(grid)
width = len(grid[0])
step_num = height + width - 2
for step in range(1, step_num + 1):
for row in range(height):
col = step - row
if 0 <= row < height and 0 <= col < width:
if not row:
grid[row][col] += grid[row][col - 1]
elif not col:
grid[row][col] += grid[row - 1][col]
else:
grid[row][col] += min(grid[row][col - 1], grid[row - 1][col])
return grid[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
col = len(grid[0])
row = len(grid)
minSum = [[(0) for x in range(col)] for y in range(row)]
for i in range(row):
for j in range(col):
add = 0
if i - 1 >= 0 and j - 1 >= 0:
add = min(minSum[i - 1][j], minSum[i][j - 1])
elif i - 1 >= 0:
add = minSum[i - 1][j]
elif j - 1 >= 0:
add = minSum[i][j - 1]
minSum[i][j] = grid[i][j] + add
return minSum[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
m, n = len(grid), len(grid[0])
dp = [0] + [float("inf")] * (n - 1)
for i in range(m):
dp[0] = dp[0] + grid[i][0]
for j in range(1, n):
dp[j] = min(dp[j], dp[j - 1]) + grid[i][j]
return dp[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution(object):
def minPathSum(self, grid):
max_row = len(grid) - 1
max_col = len(grid[0]) - 1
helper_grid = [([0] * len(grid[0])) for _ in range(len(grid))]
helper_grid[max_row][max_col] = grid[max_row][max_col]
for i in range(max_col - 1, -1, -1):
helper_grid[max_row][i] = grid[max_row][i] + helper_grid[max_row][i + 1]
for i in range(max_row - 1, -1, -1):
helper_grid[i][max_col] = grid[i][max_col] + helper_grid[i + 1][max_col]
for col in range(max_col - 1, -1, -1):
for row in range(max_row - 1, -1, -1):
helper_grid[row][col] = grid[row][col] + min(
helper_grid[row + 1][col], helper_grid[row][col + 1]
)
return helper_grid[0][0] | CLASS_DEF VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
m = len(grid)
n = len(grid[0])
if m == 0 or n == 0:
return 0
memory = [[(0) for _ in range(n)] for _ in range(m)]
def minSum(grid, x, y, n, m):
if x == 0 and y == 0:
return grid[0][0]
if x < 0 or y < 0:
return float("inf")
if memory[y][x] > 0:
return memory[y][x]
memory[y][x] = grid[y][x] + min(
minSum(grid, x - 1, y, n, m), minSum(grid, x, y - 1, n, m)
)
return memory[y][x]
return minSum(grid, n - 1, m - 1, n, m) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
if not grid:
return 0
row, col = len(grid), len(grid[0])
dp = [(0) for _ in range(col)]
dp[0] = grid[0][0]
for i in range(1, col):
dp[i] = dp[i - 1] + grid[0][i]
for i in range(1, row):
for j in range(0, col):
dp[j] = (
dp[j] + grid[i][j] if j == 0 else min(dp[j - 1], dp[j]) + grid[i][j]
)
return dp[-1] | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
xx = len(grid) - 1
yy = len(grid[0]) - 1
gridv = [[(0) for j in range(len(grid[0]))] for i in range(len(grid))]
gridv[xx][yy] = grid[xx][yy]
for i in range(xx, -1, -1):
for j in range(yy, -1, -1):
if i == xx:
if j == yy:
gridv[i][j] = grid[xx][yy]
else:
gridv[i][j] = grid[i][j] + gridv[i][j + 1]
elif j == yy:
if i == xx:
gridv[i][j] = grid[xx][yy]
else:
gridv[i][j] = grid[i][j] + gridv[i + 1][j]
else:
gridv[i][j] = min(
gridv[i + 1][j] + grid[i][j], gridv[i][j + 1] + grid[i][j]
)
print(grid)
print(gridv)
return gridv[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
if not grid:
return 0
row_count = len(grid)
col_count = len(grid[0])
dp = [[(0) for _ in range(col_count)] for _ in range(row_count)]
dp[0][0] = grid[0][0]
if row_count == col_count and col_count == 1:
return dp[-1][-1]
for row in range(row_count):
for col in range(col_count):
if row == 0 and col >= 1:
dp[row][col] = dp[row][col - 1] + grid[row][col]
elif col == 0 and row >= 1:
dp[row][col] = dp[row - 1][col] + grid[row][col]
else:
dp[row][col] = (
min(dp[row - 1][col], dp[row][col - 1]) + grid[row][col]
)
return dp[-1][-1] | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR NUMBER NUMBER |
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. | class Solution:
def minPathSum(self, grid):
m = len(grid)
n = len(grid[0])
dp = [grid[0][j] for j in range(n)]
for j in range(1, n):
dp[j] += dp[j - 1]
for i in range(1, m):
for j in range(n):
if j == 0:
dp[j] += grid[i][j]
else:
dp[j] = min(grid[i][j] + dp[j - 1], grid[i][j] + dp[j])
return dp[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR RETURN VAR NUMBER |
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w.
Now given all the cities and fights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.
Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
Example 2:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.
Note:
The number of nodes n will be in range [1, 100], with nodes labeled from 0 to n - 1.
The size of flights will be in range [0, n * (n - 1) / 2].
The format of each flight will be (src, dst, price).
The price of each flight will be in the range [1, 10000].
k is in the range of [0, n - 1].
There will not be any duplicated flights or self cycles. | class Solution:
def slidingPuzzle(self, board):
step = 0
board = tuple(map(tuple, board))
q = [board]
memo = set([board])
while q:
q0 = []
for b in q:
if b == ((1, 2, 3), (4, 5, 0)):
return step
for x in range(2):
for y in range(3):
if b[x][y]:
continue
for dx, dy in zip((1, 0, -1, 0), (0, 1, 0, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx < 2 and 0 <= ny < 3:
nb = list(map(list, b))
nb[x][y], nb[nx][ny] = nb[nx][ny], nb[x][y]
nb = tuple(map(tuple, nb))
if nb not in memo:
memo.add(nb)
q0.append(nb)
q = q0
step += 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR LIST VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR FOR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w.
Now given all the cities and fights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.
Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
Example 2:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.
Note:
The number of nodes n will be in range [1, 100], with nodes labeled from 0 to n - 1.
The size of flights will be in range [0, n * (n - 1) / 2].
The format of each flight will be (src, dst, price).
The price of each flight will be in the range [1, 10000].
k is in the range of [0, n - 1].
There will not be any duplicated flights or self cycles. | class Solution:
def slidingPuzzle(self, board):
possible_moves = {
(0): [1, 3],
(1): [0, 2, 4],
(2): [1, 5],
(3): [0, 4],
(4): [1, 3, 5],
(5): [2, 4],
}
state = "".join([str(x) for x in board[0]]) + "".join(
[str(x) for x in board[1]]
)
seen = {state: 0}
q = [[state, state.find("0")]]
while q and q[0][0] != "123450":
cur = q.pop(0)
for move in possible_moves[cur[1]]:
new_state = [int(x) for x in cur[0]]
new_state[cur[1]], new_state[move] = new_state[move], new_state[cur[1]]
new_state = "".join([str(x) for x in new_state])
if new_state not in seen:
seen[new_state] = seen[cur[0]] + 1
q.append([new_state, move])
return -1 if not q else seen[q[0][0]] | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR DICT VAR NUMBER ASSIGN VAR LIST LIST VAR FUNC_CALL VAR STRING WHILE VAR VAR NUMBER NUMBER STRING ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR NUMBER VAR VAR NUMBER NUMBER |
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w.
Now given all the cities and fights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.
Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
Example 2:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.
Note:
The number of nodes n will be in range [1, 100], with nodes labeled from 0 to n - 1.
The size of flights will be in range [0, n * (n - 1) / 2].
The format of each flight will be (src, dst, price).
The price of each flight will be in the range [1, 10000].
k is in the range of [0, n - 1].
There will not be any duplicated flights or self cycles. | class Solution:
def slidingPuzzle(self, board):
q = collections.deque()
steps = collections.defaultdict(int)
start = tuple(board[0] + board[1])
steps[start] = 0
q.append(start)
target = 1, 2, 3, 4, 5, 0
while q:
bd = q.popleft()
if bd == target:
return steps[bd]
ind = bd.index(0)
i, j = ind // 3, ind % 3
for m, n in [[i - 1, j], [i + 1, j], [i, j - 1], [i, j + 1]]:
if 0 <= m < 2 and 0 <= n < 3:
move = list(bd)
move[ind], move[m * 3 + n] = move[m * 3 + n], move[ind]
new_bd = tuple(move)
if new_bd not in steps or steps[new_bd] > steps[bd] + 1:
q.append(new_bd)
steps[new_bd] = steps[bd] + 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER IF NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER |
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w.
Now given all the cities and fights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.
Example 1:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 1
Output: 200
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
Example 2:
Input:
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
src = 0, dst = 2, k = 0
Output: 500
Explanation:
The graph looks like this:
The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.
Note:
The number of nodes n will be in range [1, 100], with nodes labeled from 0 to n - 1.
The size of flights will be in range [0, n * (n - 1) / 2].
The format of each flight will be (src, dst, price).
The price of each flight will be in the range [1, 10000].
k is in the range of [0, n - 1].
There will not be any duplicated flights or self cycles. | class Solution:
def __init__(self):
self.win = [[1, 2, 3], [4, 5, 0]]
self.lose = [
[[1, 2, 3], [5, 4, 0]],
[[2, 1, 3], [4, 5, 0]],
[[1, 3, 2], [4, 5, 0]],
[[3, 0, 1], [4, 2, 5]],
[[4, 2, 3], [1, 5, 0]],
[[1, 5, 3], [4, 2, 0]],
]
def slidingPuzzle(self, board):
s = 0
if board == self.win:
return 0
to = [board]
path = [board]
while True:
s += 1
t = []
for x in to:
tmp = self.move(x)
for z in tmp:
if z not in path:
path.append(z)
t.append(z)
for y in t:
if y == self.win:
return s
else:
for z in self.lose:
if z == y:
return -1
to = t
return -1
def same(self, x, y):
for i in range(2):
for j in range(3):
if x[i][j] != y[i][j]:
return False
return True
def copy(self, x):
r = []
for y in x:
r.append(y.copy())
return r
def move(self, bd):
p = self.find(bd)
r = []
low = p[0] > 0
le = p[1] == 0
re = p[1] == 2
if not le:
to = self.copy(bd)
to[p[0]][p[1]] = bd[p[0]][p[1] - 1]
to[p[0]][p[1] - 1] = 0
r.append(to)
if not re:
to = self.copy(bd)
to[p[0]][p[1]] = bd[p[0]][p[1] + 1]
to[p[0]][p[1] + 1] = 0
r.append(to)
if low:
to = self.copy(bd)
to[p[0]][p[1]] = bd[p[0] - 1][p[1]]
to[p[0] - 1][p[1]] = 0
r.append(to)
else:
to = self.copy(bd)
to[p[0]][p[1]] = bd[p[0] + 1][p[1]]
to[p[0] + 1][p[1]] = 0
r.append(to)
return r
def find(self, bd):
for i, x in enumerate(bd):
if 0 in x:
return i, x.index(0) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR WHILE NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR RETURN VAR FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF NUMBER VAR RETURN VAR FUNC_CALL VAR NUMBER |
Read problem statements in [Russian], [Bengali], and [Mandarin Chinese] as well.
You are given an undirected graph with $N$ nodes (numbered $1$ through $N$) and $M$ edges. Each edge connects two distinct nodes. However, there may be multiple edges connecting the same pairs of nodes, and they are considered to be distinct edges. A lowercase English letter is written in each node.
You are also given a string $S$ with length $L$. A *beautiful path* is a sequence of $L-1$ edges such that there is a sequence of $L$ nodes with the following properties:
for each valid $i$, the $i$-th edge connects the $i$-th and $(i+1)$-th of these nodes
for each valid $i$, the $i$-th character of $S$ is written in the $i$-th of these nodes
There are no other restrictions — a path may visit nodes or edges any number of times in any order.
Determine the number of beautiful paths in the graph. Since the answer can be very large, compute it modulo $10^{9}+7$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains three space-separated integers $N$, $M$ and $L$.
The second line contains a single string $S$ with length $L$.
The third line contains a single string with length $N$. For each valid $i$, the $i$-th character of the string is the letter written in the $i$-th node.
Two lines follow. The first line contains $M$ integers $u_{1},\ldots,u_{m}$. The second lines contains $M$ integers, $v_{1},\ldots,v_{m}$. This denotes that there is an edge connecting nodes $u_{i}$ and to $v_{i}$. These edges are distinct, even though they may connect the same pair of nodes.
------ Output ------
For each test case, print a single line containing one integer — the number of beautiful paths modulo $10^{9}+7$.
------ Constraints ------
$1 ≤ T ≤ 200$
$2 ≤ N ≤ 10^{3}$
$1 ≤ M ≤ 10^{3}$
$2 ≤ L ≤ 20$
$S$ contains only lowercase English letters
$1 ≤ u, v ≤ N$
$u \neq v$
------ Subtasks ------
Subtask #1 (100 points): original constraints
----- Sample Input 1 ------
2
4 4 3
aac
aaca
1 2 2 1
2 3 4 2
2 1 2
aa
aa
1
2
----- Sample Output 1 ------
3
1
----- explanation 1 ------
Example case 1: The three beautiful paths (sequences of edges) are:
- $(1,2)$, passing through nodes $(1,2,3)$ in this order
- $(4,2)$, also passing through nodes $(1,2,3)$
- $(3,2)$, passing through nodes $(4,2,3)$
Example case 2: There is only one beautiful path, which contains only edge $1$. Note that for this path (sequence of edges), there are two valid sequences of nodes: $(1,2)$ and $(2,1)$. | t = int(input())
mod = 1000000007
for _ in range(t):
n, m, l = list(map(int, input().split()))
s = input()
v = input()
e1 = list(map(int, input().split()))
e2 = list(map(int, input().split()))
adj = [[] for _ in range(n + 1)]
count = {}
for i in range(m):
adj[e1[i] - 1].append(e2[i] - 1)
adj[e2[i] - 1].append(e1[i] - 1)
temp = [e1[i] - 1, e2[i] - 1]
temp.sort()
if tuple(temp) not in count:
count[tuple(temp)] = 0
count[tuple(temp)] += 1
dp = [[(0) for _ in range(l)] for _ in range(n)]
for j in range(0, n):
if s[0] == v[j]:
dp[j][0] = 1
for j in range(1, l):
for i in range(0, n):
if s[j] != v[i]:
continue
for k in adj[i]:
dp[i][j] = (dp[i][j] + dp[k][j - 1]) % mod
ans = 0
for i in range(0, n):
ans = (ans + dp[i][l - 1]) % mod
if min(s) == max(s):
for i in range(0, n):
for j in range(i + 1, n):
if v[i] == v[j] and (i, j) in count:
ans = (ans - pow(count[i, j], l - 1, mod) + mod) % mod
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Russian], [Bengali], and [Mandarin Chinese] as well.
You are given an undirected graph with $N$ nodes (numbered $1$ through $N$) and $M$ edges. Each edge connects two distinct nodes. However, there may be multiple edges connecting the same pairs of nodes, and they are considered to be distinct edges. A lowercase English letter is written in each node.
You are also given a string $S$ with length $L$. A *beautiful path* is a sequence of $L-1$ edges such that there is a sequence of $L$ nodes with the following properties:
for each valid $i$, the $i$-th edge connects the $i$-th and $(i+1)$-th of these nodes
for each valid $i$, the $i$-th character of $S$ is written in the $i$-th of these nodes
There are no other restrictions — a path may visit nodes or edges any number of times in any order.
Determine the number of beautiful paths in the graph. Since the answer can be very large, compute it modulo $10^{9}+7$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains three space-separated integers $N$, $M$ and $L$.
The second line contains a single string $S$ with length $L$.
The third line contains a single string with length $N$. For each valid $i$, the $i$-th character of the string is the letter written in the $i$-th node.
Two lines follow. The first line contains $M$ integers $u_{1},\ldots,u_{m}$. The second lines contains $M$ integers, $v_{1},\ldots,v_{m}$. This denotes that there is an edge connecting nodes $u_{i}$ and to $v_{i}$. These edges are distinct, even though they may connect the same pair of nodes.
------ Output ------
For each test case, print a single line containing one integer — the number of beautiful paths modulo $10^{9}+7$.
------ Constraints ------
$1 ≤ T ≤ 200$
$2 ≤ N ≤ 10^{3}$
$1 ≤ M ≤ 10^{3}$
$2 ≤ L ≤ 20$
$S$ contains only lowercase English letters
$1 ≤ u, v ≤ N$
$u \neq v$
------ Subtasks ------
Subtask #1 (100 points): original constraints
----- Sample Input 1 ------
2
4 4 3
aac
aaca
1 2 2 1
2 3 4 2
2 1 2
aa
aa
1
2
----- Sample Output 1 ------
3
1
----- explanation 1 ------
Example case 1: The three beautiful paths (sequences of edges) are:
- $(1,2)$, passing through nodes $(1,2,3)$ in this order
- $(4,2)$, also passing through nodes $(1,2,3)$
- $(3,2)$, passing through nodes $(4,2,3)$
Example case 2: There is only one beautiful path, which contains only edge $1$. Note that for this path (sequence of edges), there are two valid sequences of nodes: $(1,2)$ and $(2,1)$. | import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")
inp = lambda: list(map(int, sys.stdin.readline().rstrip("\r\n").split()))
mod = 10**9 + 7
Mod = 998244353
INF = float("inf")
tc = 1
tc = int(input())
for test in range(1, tc + 1):
n, m, l = inp()
s = str(input())
a = str(input())
g = [[] for i in range(n)]
dp = [([0] * l) for i in range(n)]
b, c = inp(), inp()
for i, j in zip(b, c):
i -= 1
j -= 1
g[i].append(j)
g[j].append(i)
for i in range(n):
if a[i] == s[0]:
dp[i][0] = 1
for i in range(1, l):
for node in range(n):
if a[node] == s[i]:
for edge in g[node]:
dp[node][i] += dp[edge][i - 1]
dp[node][i] %= mod
ans = 0
for i in range(n):
ans += dp[i][-1]
ans %= mod
if len(set(s)) == 1:
mat = [([0] * n) for i in range(n)]
for i in range(m):
mat[b[i] - 1][c[i] - 1] += 1
mat[c[i] - 1][b[i] - 1] += 1
for i in range(n):
for j in range(i + 1, n):
if mat[i][j] > 0 and a[i] == s[0] and a[j] == s[0]:
ans = (ans + mod - pow(mat[i][j], l - 1, mod)) % mod
print(ans) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
visited = []
for i in range(len(Str)):
start = i
end = i
while start >= 0 and end < len(Str) and Str[start] == Str[end]:
if Str[start : end + 1] not in visited:
visited.append(Str[start : end + 1])
start -= 1
end += 1
else:
start -= 1
end += 1
start = i
end = i + 1
while start >= 0 and end < len(Str) and Str[start] == Str[end]:
if Str[start : end + 1] not in visited:
visited.append(Str[start : end + 1])
start -= 1
end += 1
else:
start -= 1
end += 1
return len(visited) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
seen = set()
def getPalCounts(l, r):
count = 0
while l >= 0 and r < len(s) and s[l] == s[r]:
cur = s[l : r + 1]
if cur not in seen:
count += 1
seen.add(cur)
l -= 1
r += 1
return count
res = 0
for i in range(len(s)):
res += getPalCounts(i, i)
res += getPalCounts(i, i + 1)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
m = dict()
n = len(s)
R = [[(0) for x in range(n + 1)] for x in range(2)]
s = "@" + s + "#"
for j in range(2):
rp = 0
R[j][0] = 0
i = 1
while i <= n:
while s[i - rp - 1] == s[i + j + rp]:
rp += 1
R[j][i] = rp
k = 1
while R[j][i - k] != rp - k and k < rp:
R[j][i + k] = min(R[j][i - k], rp - k)
k += 1
rp = max(rp - k, 0)
i += k
s = s[1 : len(s) - 1]
m[s[0]] = 1
for i in range(1, n):
for j in range(2):
for rp in range(R[j][i], 0, -1):
m[s[i - rp - 1 : i - rp - 1 + 2 * rp + j]] = 1
m[s[i]] = 1
return len(m) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
def countPalindrome(left, right):
nonlocal unique_palindrome
while left >= 0 and right < len(Str) and Str[left] == Str[right]:
unique_palindrome.add(Str[left : right + 1])
left -= 1
right += 1
unique_palindrome = set()
for i in range(len(Str)):
countPalindrome(i, i)
countPalindrome(i, i + 1)
return len(unique_palindrome) | CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
n = len(s)
result = set()
for i in range(n):
left = right = i
while left >= 0 and right < n and s[left] == s[right]:
result.add(s[left : right + 1])
left -= 1
right += 1
left = i
right = i + 1
while left >= 0 and right < n and s[left] == s[right]:
result.add(s[left : right + 1])
left -= 1
right += 1
return len(result) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
n = len(s)
se = set()
for i in range(n):
j = i - 1
k = i + 1
r = s[i]
se.add(r)
while j >= 0 and k < n and s[j] == s[k]:
r = s[j] + r + s[k]
se.add(r)
j -= 1
k += 1
j = i
k = i + 1
r = ""
while j >= 0 and k < n and s[j] == s[k]:
r = s[j] + r + s[k]
se.add(r)
j -= 1
k += 1
return len(se) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
res = set()
i = 0
n = len(Str)
while i < n:
j = i - 1
k = i + 1
res.add(Str[i])
while j >= 0 and k < n and Str[j] == Str[k]:
res.add(Str[j : k + 1])
j -= 1
k += 1
j = i
k = i + 1
while j >= 0 and k < n and Str[j] == Str[k]:
res.add(Str[j : k + 1])
j -= 1
k += 1
i += 1
return len(res) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def fetch_palindromes(self, s, left, right):
result = []
while left >= 0 and right < len(s) and s[left] == s[right]:
result.append(s[left : right + 1])
left -= 1
right += 1
return result
def palindromeSubStrs(self, Str):
result = []
for i in range(len(Str)):
result.extend(self.fetch_palindromes(Str, i, i))
result.extend(self.fetch_palindromes(Str, i, i + 1))
return len(set(result)) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
dct = {}
i = 0
n = len(Str)
while i < n:
dct[Str[i]] = dct.get(Str, 0) + 1
prev = i - 1
next = i + 1
while prev >= 0 and next < n:
if Str[prev] == Str[next]:
dct[Str[prev : next + 1]] = dct.get(Str[prev : next + 1], 0) + 1
prev -= 1
next += 1
else:
break
prev1 = i
next1 = i + 1
while prev1 >= 0 and next1 < n:
if Str[prev1] == Str[next1]:
dct[Str[prev1 : next1 + 1]] = dct.get(Str[prev1 : next1 + 1], 0) + 1
prev1 -= 1
next1 += 1
else:
break
i += 1
return len(dct) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
res = 0
dic = {}
dic = set()
for i in range(len(s)):
l, r = i, i
while l >= 0 and r < len(s) and s[r] == s[l]:
if s[l : r + 1] not in dic:
res += 1
dic.add(s[l : r + 1])
l -= 1
r += 1
l, r = i, i + 1
while l >= 0 and r < len(s) and s[r] == s[l]:
if s[l : r + 1] not in dic:
res += 1
dic.add(s[l : r + 1])
l -= 1
r += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
substrings = set()
substrings.union(set([ch for ch in Str]))
for i in range(0, len(Str)):
left = i
right = i
while left >= 0 and right <= len(Str) - 1 and Str[left] == Str[right]:
substrings.add(Str[left : right + 1])
left -= 1
right += 1
left = i
right = i + 1
while left >= 0 and right <= len(Str) - 1 and Str[left] == Str[right]:
substrings.add(Str[left : right + 1])
left -= 1
right += 1
return len(substrings) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
l = len(Str)
d = {}
for i in range(l):
left = i
right = i
while left >= 0 and right < l and Str[left] == Str[right]:
d[Str[left : right + 1]] = 1
left -= 1
right += 1
left = i
right = i + 1
while left >= 0 and right < l and Str[left] == Str[right]:
d[Str[left : right + 1]] = 1
left -= 1
right += 1
i += 1
return len(d) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, str):
l = len(str)
ls = [(0) for i in range(2 * l + 2)]
a = "@#"
for i in str:
a += i
a += "#"
a += "@"
s = set()
for i in range(2, l * 2 + 2):
while a[i - ls[i] - 1] == a[i + ls[i] + 1] and (
a[i - ls[i] - 1] != "@" or a[i + ls[i] + 1] != "@"
):
ls[i] += 1
if a[i] == "#" and ls[i] != 0:
m = i // 2 - ls[i] // 2
n = i // 2 + ls[i] // 2
b = str[m:n]
s.add(b)
elif a[i] != "#" and ls[i] != 0:
m = i // 2 - ls[i] // 2 - 1
n = i // 2 + ls[i] // 2
b = str[m:n]
s.add(b)
if a[i] != "#" and a[i] != "@" and ls[i] != 0:
s.add(a[i])
if "" in s:
s.remove("")
return len(s)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
Str = input()
solObj = Solution()
print(solObj.palindromeSubStrs(Str)) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER STRING VAR BIN_OP BIN_OP VAR VAR VAR NUMBER STRING VAR VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF STRING VAR EXPR FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
bag = set()
for i in range(len(Str)):
l = r = i
bag.update(self.findPalindrome(Str, l, r))
l = i
r = i + 1
bag.update(self.findPalindrome(Str, l, r))
return len(bag)
def findPalindrome(self, s, l, r):
res = []
while l >= 0 and r < len(s) and s[l] == s[r]:
res.append(s[l : r + 1])
l -= 1
r += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
res = 0
charSet = set()
for i in range(len(Str)):
l, r = i, i
while l >= 0 and r < len(Str) and Str[l] == Str[r]:
charSet.add(Str[l : r + 1])
res += 1
l -= 1
r += 1
l, r = i, i + 1
while l >= 0 and r < len(Str) and Str[l] == Str[r]:
charSet.add(Str[l : r + 1])
res += 1
l -= 1
r += 1
return len(charSet) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
ans = set()
def helper(i, j):
c = 0
while i >= 0 and j < len(s) and s[i] == s[j]:
ans.add(s[i : j + 1])
i -= 1
j += 1
for i in range(len(s)):
helper(i, i)
helper(i, i + 1)
return len(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
d = {}
n = len(Str)
for i in range(n):
l = i
r = i + 1
p = i
q = i
while l >= 0 and r < n and Str[l] == Str[r]:
p = l
q = r
l -= 1
r += 1
s1 = Str[p : q + 1]
if s1 not in d:
d[s1] = 1
s1 = Str[p : q + 1]
if s1 not in d:
d[s1] = 1
l = i
r = i
p = i
q = i
while l >= 0 and r < n and Str[l] == Str[r]:
p = l
q = r
l -= 1
r += 1
s1 = Str[p : q + 1]
if s1 not in d:
d[s1] = 1
s1 = Str[p : q + 1]
if s1 not in d:
d[s1] = 1
return len(d)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
Str = input()
solObj = Solution()
print(solObj.palindromeSubStrs(Str)) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, S):
s = set()
N = len(S)
ans = 0
for k in range(N):
i = k
j = k
while i >= 0 and j < N and S[i] == S[j]:
if S[i : j + 1] not in s:
s.add(S[i : j + 1])
ans += 1
i -= 1
j += 1
for k in range(N - 1):
i = k
j = k + 1
while i >= 0 and j < N and S[i] == S[j]:
if S[i : j + 1] not in s:
s.add(S[i : j + 1])
ans += 1
i -= 1
j += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
l = []
n = s
res = n[0]
m = 0
dp = [([0] * len(n)) for _ in range(len(n))]
for i in range(len(n)):
dp[i][i] = 1
res = n[i : i + 1]
if res not in l:
l.append(res)
for i in range(len(n) - 1):
if n[i] == n[i + 1]:
dp[i][i + 1] = 1
res = n[i : i + 2]
if res not in l:
l.append(res)
for j in range(2, len(n)):
for i in range(len(n) - j):
if n[i] == n[i + j] and dp[i + 1][i + j - 1] == 1:
dp[i][i + j] = dp[i + 1][i + j - 1]
res = n[i : i + j + 1]
if res not in l:
l.append(res)
return len(l) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
ans, p = set(s), len(s)
for i in range(p):
a, b = i - 1, i + 1
while a >= 0 and b < p:
if s[a] == s[b]:
ans.add(s[a : b + 1])
else:
break
a -= 1
b += 1
a, b = i, i + 1
while a >= 0 and b < p:
if s[a] == s[b]:
ans.add(s[a : b + 1])
else:
break
a -= 1
b += 1
return len(ans)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
Str = input()
solObj = Solution()
print(solObj.palindromeSubStrs(Str)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
s = Str
start = 0
st = set()
for i in range(len(s)):
st.add(s[i])
for i in range(1, len(s)):
l = i - 1
r = i
while l >= 0 and r < len(s) and s[l] == s[r]:
start = l
st.add(s[start : r + 1])
l -= 1
r += 1
l = i - 1
r = i + 1
while l >= 0 and r < len(s) and s[l] == s[r]:
start = l
st.add(s[start : r + 1])
l -= 1
r += 1
return len(st) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
n = len(s)
ss = set()
for i in range(n - 1):
left = i
right = i + 1
while left >= 0 and right < n:
if s[left] == s[right]:
ss.add(s[left : right + 1])
left -= 1
right += 1
else:
break
for i in range(n):
left = i - 1
right = i + 1
while left >= 0 and right < n:
if s[left] == s[right]:
ss.add(s[left : right + 1])
left -= 1
right += 1
else:
break
for ele in s:
ss.add(ele)
return len(ss) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
def find_palindromes_in_sub_string(input, j, k, visited):
count = 0
while j >= 0 and k < len(input):
if input[j] != input[k]:
break
if input[j : k + 1] not in visited:
visited[input[j : k + 1]] = True
count += 1
j -= 1
k += 1
return count
count = -1
visited = {}
count = 0
for i in range(0, len(Str)):
count += find_palindromes_in_sub_string(Str, i - 1, i, visited)
count += find_palindromes_in_sub_string(Str, i, i, visited)
return count | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def find(self, Str, low, high, pal):
while low >= 0 and high <= len(Str) - 1 and Str[low] == Str[high]:
pal.add(Str[low : high + 1])
low = low - 1
high = high + 1
def palindromeSubStrs(self, Str):
pal = set()
for i in range(len(Str)):
self.find(Str, i, i, pal)
self.find(Str, i, i + 1, pal)
return len(pal) | CLASS_DEF FUNC_DEF WHILE VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
s = set()
for i in range(len(Str)):
x = y = i
while x >= 0 and y < len(Str) and Str[x] == Str[y]:
t = Str[x : y + 1]
if t:
s.add(t)
x -= 1
y += 1
x = i
y = i + 1
while x >= 0 and y < len(Str) and Str[x] == Str[y]:
t = Str[x : y + 1]
if t:
s.add(t)
x -= 1
y += 1
return len(s) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, st):
s = set()
n = len(st)
ans = 0
for i in range(n):
l = i
r = i + 1
while l >= 0 and r <= n:
string = st[l:r]
if string != string[::-1]:
break
s.add(string)
l -= 1
r += 1
for i in range(n):
l = i
r = i + 2
while l >= 0 and r <= n:
string = st[l:r]
if string != string[::-1]:
break
s.add(string)
l -= 1
r += 1
return len(s) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, Str):
lst = []
for index in range(len(Str)):
findPali(index, index, Str, lst)
findPali(index, index + 1, Str, lst)
return len(set(lst))
def findPali(start, end, str, lst):
while start >= 0 and end < len(str) and str[start] == str[end]:
start -= 1
end += 1
lst.append(str[start + 1 : end]) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Given a string str of lowercase ASCII characters, Count the number of distinct continuous palindromic sub-strings which are present in the string str.
Example 1:
Input:
str = "abaaa"
Output:
5
Explanation : These are included in answer:
"a","aa","aaa","aba","b"
Example 2:
Input
str = "geek"
Output:
4
Explanation : Below are 4 palindrome sub-strings
"e","ee","g","k"
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromeSubStrs() which takes the string str as input parameter and returns the total number of distinct continuous palindromic sub-strings in str.
Expected Time Complexity : O(N^{2}logN)
Expected Auxilliary Space : O(N^{2})
Constraints:
1 ≤ N ≤ 3*10^{3}, where N is the length of the string str. | class Solution:
def palindromeSubStrs(self, s):
n = len(s)
p = [([False] * n) for i in range(n)]
d = {}
r = 0
for i in s:
if i not in d:
d[i] = 1
r += 1
for i in range(n):
p[i][i] = True
for i in range(n - 1):
if s[i] == s[i + 1]:
p[i][i + 1] = True
if s[i : i + 2] not in d:
d[s[i : i + 2]] = 1
r += 1
for x in range(2, n):
for i in range(n - x):
j = x + i
if s[i] == s[j] and p[i + 1][j - 1]:
p[i][j] = True
if s[i : j + 1] not in d:
d[s[i : j + 1]] = 1
r += 1
return r | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
n = len(A)
dp = [([2] * n) for i in range(n)]
m = dict()
for i in range(n):
m[A[i]] = i
ans = 0
for i in range(n):
for j in range(i + 1, n):
a_k = A[j] - A[i]
if a_k > A[i]:
break
k = m.get(a_k, None)
if k is not None and k < i:
dp[i][j] = dp[k][i] + 1
ans = max(ans, dp[i][j])
return ans if ans >= 3 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NONE IF VAR NONE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
res = 0
A_dict = {}
for x in A:
A_dict[x] = A_dict.get(x, 0) + 1
for i in range(len(A) - 1):
for j in range(i + 1, len(A)):
a, b = A[i], A[j]
c = a + b
length = 0
while c in A_dict:
length += 1
a, b = b, c
c = a + b
res = max(res, length)
return res + 2 if res > 0 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
unique = set(A)
max_len = 0
for i in range(len(A) - 2):
for j in range(i + 1, len(A) - 1):
f, s = A[i], A[j]
length = 2
while f + s in unique:
f, s = s, f + s
length += 1
max_len = max(max_len, length)
return 0 if max_len == 2 else max_len | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
S = set(A)
maxLen = 0
n = len(A)
for i in range(0, n):
for j in range(i + 1, n):
x = A[j]
y = A[i] + A[j]
length = 2
while y in S:
z = x + y
x = y
y = z
length += 1
maxLen = max(maxLen, length)
return maxLen if maxLen >= 3 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
mapA = {n: i for i, n in enumerate(A)}
l = ans = 0
for i in range(len(A)):
for j in range(i + 1, len(A)):
x, y = A[j], A[i] + A[j]
l = 2
while y in mapA:
x, y = y, x + y
l += 1
ans = max(ans, l)
return ans if ans >= 3 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
max_l = 0
list_poss = [{} for i in range(len(A))]
for i in range(len(A) - 1):
for j in range(i + 1):
if not A[i + 1] + A[j] in list_poss[i + 1]:
list_poss[i + 1][A[i + 1] + A[j]] = 2
if A[i + 1] in list_poss[j]:
if not A[i + 1] + A[j] in list_poss[i + 1]:
list_poss[i + 1][A[i + 1] + A[j]] = list_poss[j][A[i + 1]] + 1
elif list_poss[j][A[i + 1]] + 1 > list_poss[i + 1][A[i + 1] + A[j]]:
list_poss[i + 1][A[i + 1] + A[j]] = list_poss[j][A[i + 1]] + 1
if list_poss[i + 1][A[i + 1] + A[j]] > max_l:
max_l = list_poss[i + 1][A[i + 1] + A[j]]
return max_l | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
max_length = 0
S = set(A)
for i in range(len(A)):
for j in range(i + 1, len(A)):
x, y = A[i], A[j]
expected = x + y
length = 2
while expected in S:
x = y
y = expected
expected = x + y
length += 1
max_length = max(max_length, length)
return max_length if max_length >= 3 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | 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):
max_len = 2
F = [x1, x2]
xi = x1 + x2
while xi in setA:
max_len += 1
F.append(xi)
xi = F[-2] + F[-1]
if max_len == 6:
print(F)
return max_len
max_len = 2
setA = set(A)
for i in range(len(A)):
for j in range(i + 1, len(A)):
x1, x2 = A[i], A[j]
if x1 * C1[max_len] + x2 * C2[max_len] > A[-1]:
break
max_len = max(max_len, getLLFS(x1, x2))
if max_len < 3:
return 0
return max_len | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST VAR VAR WHILE VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
n = len(A)
dp = [([2] * n) for _ in range(n)]
loc = {A[0]: 0, A[1]: 1}
longest = 0
for k in range(2, n):
loc[A[k]] = k
for j in range(k):
target = A[k] - A[j]
if target in loc and loc[target] < j:
dp[j][k] = dp[loc[target]][j] + 1
longest = max(longest, dp[j][k])
return longest | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
cache = set(A)
dp = collections.defaultdict(int)
for i in range(len(A)):
for j in range(i + 1, len(A)):
if A[j] - A[i] in cache and A[j] - A[i] < A[i]:
dp[A[i], A[j]] = dp.get((A[j] - A[i], A[i]), 2) + 1
return max(list(dp.values()) + [0]) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
longest = 0
ss = set(A)
for i, x in enumerate(A):
for j, y in enumerate(A[i + 1 :]):
tmp = x + y
if tmp in ss:
ret = [x, y]
t = x
while True:
if t + y in ss:
t, y = y, t + y
ret.append(y)
else:
break
if longest < len(ret):
longest = len(ret)
return longest | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR WHILE NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
idxs = {x: i for i, x in enumerate(A)}
longest = defaultdict(lambda: 2)
res = 0
for k, val in enumerate(A):
for j in range(k):
i = idxs.get(val - A[j], None)
if i is not None and i < j:
candidate = longest[j, k] = longest[i, j] + 1
res = max(res, candidate)
return res if res >= 3 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NONE IF VAR NONE VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
m = {}
for i in range(len(A)):
m[A[i]] = i
n = len(A)
dp = [([2] * n) for _ in range(n)]
res = 0
for j in range(n):
for k in range(j + 1, n):
ai = A[k] - A[j]
if ai >= A[j]:
break
if ai not in m:
continue
i = m[ai]
dp[j][k] = dp[i][j] + 1
res = max(res, dp[j][k])
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
fibHash = {}
ans = 0
n = len(A)
for i in range(1, n):
for j in reversed(range(i)):
f1 = A[i] - A[j]
size = fibHash[A[j], A[i]] = fibHash.get((f1, A[j]), 1) + 1
ans = max(ans, size)
return ans if ans > 2 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
st = set(A)
lenth = 0
dq = collections.deque()
for j in range(1, len(A) - 1):
for i in range(j):
if A[i] + A[j] in st:
lenth = 3
dq.append([3, A[j], A[i] + A[j]])
while dq:
sz = len(dq)
for _ in range(sz):
s = dq.popleft()
if s[1] + s[2] in st:
s[0], s[1], s[2] = s[0] + 1, s[2], s[1] + s[2]
lenth = max(lenth, s[0])
dq.append(s)
return lenth | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER VAR VAR BIN_OP VAR VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
nums = {}
for i, num in enumerate(A):
nums[num] = i
def dfs(prev, cur, visit):
if (prev, cur) in visit:
return visit[prev, cur]
if prev == -1:
res = 0
for i in range(len(A)):
for j in range(i + 1, len(A)):
if A[i] + A[j] in nums:
res = max(res, 1 + dfs(i, j, visit))
return res
if A[prev] + A[cur] in nums:
visit[prev, cur] = 1 + dfs(cur, nums[A[prev] + A[cur]], visit)
else:
visit[prev, cur] = 1
return visit[prev, cur]
return dfs(-1, -1, {}) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR RETURN VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER DICT VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
val_to_idx = {val: idx for idx, val in enumerate(A)}
def maxLevel(A: List[int], sum_idx: int, var2_idx: int, level: int) -> int:
if var2_idx == 0:
return level
val_to_search = A[sum_idx] - A[var2_idx]
returned_idx = val_to_idx.get(val_to_search, None)
if returned_idx is not None and returned_idx < var2_idx:
return maxLevel(A, var2_idx, returned_idx, level + 1)
return level
longest = 0
for i in reversed(list(range(len(A)))):
for j in reversed(list(range(i))):
result_length = maxLevel(A, i, j, 2)
longest = max(longest, result_length)
if longest < 3:
return 0
return longest | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF VAR VAR VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NONE IF VAR NONE VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
d = set()
n = len(A)
for c in A:
d.add(c)
max_l = 0
for i in range(n - 2):
for j in range(i + 1, n - 1):
a = A[i]
b = A[j]
count = 0
while a + b in d:
count += 1
a, b = b, a + b
max_l = max(max_l, count + 2)
if max_l >= 3:
return max_l
return 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR RETURN NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, lst: List[int]) -> int:
m = 0
dp = {}
for i in range(len(lst)):
for j in range(i):
j1 = j, lst[i]
i1 = i, lst[i] + lst[j]
if j1 in dp:
dp[i1] = 1 + dp[j1]
else:
dp[i1] = 2
m = max(m, dp[i1])
if m < 3:
return 0
return m | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
dp = {}
dp[A[0], A[1]] = 2
curmax = 2
for i in range(2, len(A)):
for j in range(i):
prev = dp[A[i] - A[j], A[j]] if (A[i] - A[j], A[j]) in dp else 1
dp[A[j], A[i]] = 1 + prev
curmax = max(curmax, 1 + prev)
if curmax < 3:
return 0
return curmax | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
if len(A) < 3:
return 0
dic = {}
m = max(A)
for i in range(len(A)):
dic[A[i]] = 1
m = 0
for i in range(len(A)):
for j in range(i + 1, len(A)):
c = 2
a = A[i]
b = A[j]
try:
while dic[a + b] == 1:
temp = b
b = a + b
a = temp
c += 1
except KeyError:
m = max(m, c)
m = max(m, c)
if m < 3:
return 0
return m | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
index = {}
for i in range(len(A)):
index[A[i]] = i
record = {}
def dp(i, j):
if (i, j) in record:
return record[i, j]
if A[i] + A[j] in index:
res = 1 + dp(j, index[A[i] + A[j]])
record[i, j] = res
return res
else:
return 2
res = 0
for i in range(len(A) - 1):
for j in range(i + 1, len(A)):
res = max(res, dp(i, j))
if res >= 3:
return res
else:
return 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
n = len(A)
m = dict()
for i, a in enumerate(A):
m[a] = i
res = 0
dp = [[(2) for i in range(n)] for j in range(n)]
for j in range(n):
for k in range(j + 1, n):
a_i = A[k] - A[j]
if a_i >= A[j]:
break
if a_i in m:
i = m[a_i]
dp[j][k] = dp[i][j] + 1
res = max(res, dp[j][k])
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
dp = [([2] * len(A)) for _ in range(len(A))]
d = {}
for i, num in enumerate(A):
d[num] = i
res = 2
for i in range(len(A)):
for j in range(i + 1, len(A)):
diff = A[j] - A[i]
if diff in d and d[diff] < i:
dp[i][j] = dp[d[diff]][i] + 1
res = max(res, dp[i][j])
return res if res >= 3 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
if len(A) <= 2:
return 0
N = len(A)
ans = 0
dp = dict()
dp[A[1], A[0]] = 2
for i in range(2, N):
for j in range(i):
if (A[j], A[i] - A[j]) in dp:
z = dp[A[j], A[i] - A[j]]
ans = max(z + 1, ans)
dp[A[i], A[j]] = z + 1
else:
dp[A[i], A[j]] = 2
return ans | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
sA = set(A)
B = Counter()
ans = 0
for i in reversed(range(len(A))):
a = A[i]
for b in A[i + 1 :]:
c = a + b
if c in sA:
B[a, b] = B[b, c] + 1
ans = max(ans, B[a, b] + 2)
if c > A[-1]:
break
return ans if ans >= 3 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
exists = set(A)
max_length = 2
for i in range(len(A)):
for j in range(i + 1, len(A)):
a = A[i]
b = A[j]
curr_length = 2
while a + b in exists:
curr_length += 1
a, b = b, a + b
max_length = max(max_length, curr_length)
if max_length <= 2:
return 0
return max_length | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, nums: List[int]) -> int:
n, ans = len(nums), 0
idx = {n: i for i, n in enumerate(nums)}
dp = [([0] * n) for _ in range(n)]
for j in range(n):
dp[0][j] = 2
dp[j][j] = 1
for i in range(1, n):
for j in range(i + 1, n):
dp[i][j] = 2
if nums[i] > nums[j] // 2 and nums[j] - nums[i] in idx:
ii = idx[nums[j] - nums[i]]
dp[i][j] = dp[ii][i] + 1
ans = max(ans, dp[i][j])
return ans if ans > 2 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
record = {}
for i, a in enumerate(A):
record[a] = i
adj = {}
for i in range(len(A) - 2):
for j in range(i + 1, len(A) - 1):
if A[i] + A[j] in record:
adj[i, j] = j, record[A[i] + A[j]]
mem = {}
def helper(i, j):
if (i, j) in mem:
return mem[i, j]
elif (i, j) in adj:
res = 1 + helper(adj[i, j][0], adj[i, j][1])
mem[i, j] = res
return res
else:
mem[i, j] = 2
return 2
res = 0
for i in range(len(A) - 2):
for j in range(i + 1, len(A) - 1):
res = max(res, helper(i, j))
if res >= 3:
return res
else:
return 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
index = {x: i for i, x in enumerate(A)}
m = collections.defaultdict(lambda: 2)
res = 0
for i2 in range(2, len(A)):
for i0 in range(i2):
x1 = A[i2] - A[i0]
if x1 <= A[i0]:
break
if x1 in index:
i1 = index[x1]
m[i1, i2] = m[i0, i1] + 1
res = max(res, m[i1, i2])
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
setA = set(A)
answer = 0
max_ = A[-1]
visited = set()
queue = collections.deque()
for i in range(len(A) - 1):
curr1 = A[i]
for j in range(i + 1, len(A)):
curr2 = A[j]
if curr1 + curr2 in setA:
queue.append((curr1, curr2))
while queue:
num1, num2 = queue.popleft()
if (num1, num2) in visited:
continue
visited.add((num1, num2))
sum_ = num1 + num2
l = 2
while sum_ in setA and sum_ <= max_:
num1, num2 = num2, sum_
sum_ = num1 + num2
visited.add((num1, num2))
l += 1
answer = max(answer, l)
return answer | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
num_set = set(A)
used = set()
max_len = 2
for x in range(len(A)):
a = A[x]
for y in range(x + 1, len(A)):
i, j = a, A[y]
if (i, j) not in used:
used.add((i, j))
count = 2
while i + j in num_set:
count += 1
i, j = j, i + j
used.add((i, j))
max_len = max(count, max_len)
return 0 if max_len == 2 else max_len | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
indexes = {A[i]: i for i in range(len(A))}
dp = [[(2) for i in range(len(A))] for j in range(len(A))]
z = 0
for i in range(1, len(A)):
for j in range(0, i):
idx = indexes.get(A[i] + A[j], -1)
if idx == -1:
continue
else:
dp[i][idx] = dp[j][i] + 1
z = max(max(dp[i]), z)
if z < 3:
return 0
return z | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
index = {A[i]: i for i in range(len(A))}
longest = {}
ans = 0
length = len(A)
for i in range(length):
for j in range(i):
x = A[i] - A[j]
m = index.get(x)
if m is not None and m < j:
longest[j, i] = (
longest.get((m, j)) + 1
if longest.get((m, j)) is not None
else 3
)
ans = max(ans, longest[j, i])
else:
continue
return ans if ans >= 3 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NONE BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
n = len(A)
if n < 3:
return 0
dp = [[(2) for j in range(n)] for i in range(n)]
ans = 2
hmap = {A[0]: 0, A[1]: 1}
for i in range(2, n):
for j in range(1, i):
pos = hmap.get(A[i] - A[j], -1)
if pos >= 0 and pos < j:
dp[i][j] = max(dp[i][j], dp[j][pos] + 1)
ans = max(ans, dp[i][j])
hmap[A[i]] = i
return ans if ans > 2 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
n = len(A)
A_set = set(A)
res_set = []
res = 0
for i in range(n - 2):
for j in range(i + 1, n):
if A[i] + A[j] in A_set:
res_set.append((A[j], A[i] + A[j]))
if res_set:
res = 3
else:
return 0
while res_set:
length = len(res_set)
while length:
x, y = res_set.pop(0)
if x + y in A_set:
res_set.append((y, x + y))
length -= 1
if res_set:
res += 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER RETURN NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
dp = collections.defaultdict(lambda: 2)
ret, idx = 0, {A[0]: 0}
for j in range(1, len(A) - 1):
idx[A[j]] = j
for i in range(j + 1, len(A)):
diff = A[i] - A[j]
if diff >= A[j]:
break
elif diff not in idx:
continue
dp[j, i] = dp[idx[diff], j] + 1
ret = max(ret, dp[j, i])
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER DICT VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
dp = {}
dp[A[1], A[0] + A[1]] = 2
for i in range(2, len(A)):
for j in range(i):
if (A[j], A[i]) not in dp:
dp[A[i], A[j] + A[i]] = 2
else:
dp[A[i], A[j] + A[i]] = dp[A[j], A[i]] + 1
del dp[A[j], A[i]]
longest = max(dp.values())
return longest if longest > 2 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
dp = {}
for i in range(1, len(A)):
for j in range(i):
dp[A[j], A[i]] = max(
dp.get((A[j], A[i]), 2), dp.get((A[i] - A[j], A[j]), 1) + 1
)
res = max(dp.values())
return res if res > 2 else 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR NUMBER VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
mp = {}
for v in A:
mp[v] = set()
result = 0
for i in range(len(A)):
for j in range(i + 1, len(A)):
a = A[i]
b = A[j]
l = 2
while True:
if b in mp[a]:
break
if l != 2:
mp[a].add(b)
c = a + b
if c not in mp:
break
a, b = b, c
l += 1
if l < 3:
l = 0
else:
result = max(result, l)
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
n = len(A)
dp = [{} for i in range(n)]
result = 0
for i in range(n):
for j in range(i):
prev = A[i] - A[j]
if prev in dp[j]:
l = dp[i][A[j]] = dp[j][prev] + 1
result = max(result, l)
else:
dp[i][A[j]] = 2
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
if not A or len(A) == 0:
return 0
n = len(A)
dp = [([0] * n) for _ in range(n)]
res = 0
for i in range(2, n):
l = 0
r = i - 1
while l < r:
sum_ = A[l] + A[r]
if sum_ > A[i]:
r -= 1
elif sum_ < A[i]:
l += 1
else:
dp[r][i] = dp[l][r] + 1
res = max(res, dp[r][i])
l += 1
r -= 1
if res == 0:
return 0
return res + 2 | CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP VAR NUMBER VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
n = len(A)
dp = collections.defaultdict(int)
ret = 0
a_set = set(A)
for i in range(n):
for j in range(i + 1, n):
dp[A[i], A[j]] = 2
left = A[j] - A[i]
if left > A[i] or left not in a_set:
continue
dp[A[i], A[j]] = max(dp[A[i], A[j]], dp[left, A[i]] + 1)
ret = max(ret, dp[A[i], A[j]])
return ret | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
dp = [collections.defaultdict(int) for _ in range(len(A))]
longest_seq = 0
s = set(A)
for i in range(2, len(A)):
for j in range(i):
if A[i] - A[j] in s and A[i] - A[j] < A[j]:
two_ago = A[i] - A[j]
one_ago = A[j]
if dp[j][two_ago] > 0:
dp[i][one_ago] = dp[j][two_ago] + 1
else:
dp[i][one_ago] = 3
longest_seq = max(longest_seq, dp[i][one_ago])
return longest_seq | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
A sequence X_1, X_2, ..., X_n is fibonacci-like if:
n >= 3
X_i + X_{i+1} = X_{i+2} for all i + 2 <= n
Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A. If one does not exist, return 0.
(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)
Example 1:
Input: [1,2,3,4,5,6,7,8]
Output: 5
Explanation:
The longest subsequence that is fibonacci-like: [1,2,3,5,8].
Example 2:
Input: [1,3,7,11,12,14,18]
Output: 3
Explanation:
The longest subsequence that is fibonacci-like:
[1,11,12], [3,11,14] or [7,11,18].
Note:
3 <= A.length <= 1000
1 <= A[0] < A[1] < ... < A[A.length - 1] <= 10^9
(The time limit has been reduced by 50% for submissions in Java, C, and C++.) | class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
S = set(A)
i = 0
max_len = 0
while i < len(A) - 1:
j = i + 1
while j < len(A):
subSeq = [A[i], A[j]]
while subSeq[-1] + subSeq[-2] in S:
subSeq.append(subSeq[-1] + subSeq[-2])
if len(subSeq) > max_len:
max_len = len(subSeq)
j += 1
i += 1
return max_len | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.