description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | def checkWordBreak(s, wordDict, idx, ans, arr):
if idx == len(s):
ans.append([] + arr)
return
for i in range(idx, len(s)):
newStr = s[idx : i + 1]
arr.append(newStr)
if newStr in wordDict:
checkWordBreak(s, wordDict, i + 1, ans, arr)
arr.pop()
newStr = s[: i + 1 - idx]
class Solution:
def wordBreak(self, n, dict, s):
ans = []
checkWordBreak(s, dict, 0, ans, [])
ansArr = []
for i in range(len(ans)):
tr = ""
for j in range(len(ans[i])):
if j != 0:
tr += " " + ans[i][j]
else:
tr += ans[i][j]
ansArr.append(tr)
return ansArr | FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP STRING VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
dictionary = {item: (True) for item in dict}
dp = [list() for i in range(len(s))]
for i in range(len(s)):
word = ""
for j in range(i, -1, -1):
word = s[j] + word
if dictionary.get(word):
if j == 0:
dp[i].append(word)
elif dp[j - 1]:
for sen in dp[j - 1]:
new_sen = sen + " " + word
dp[i].append(new_sen)
return dp[len(s) - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, d, s):
d = set(d)
ans = []
cans = []
def rec(i, w):
if i == len(s):
if w == "":
ans.append(" ".join(cans))
return
w1 = w + s[i]
rec(i + 1, w1)
if w1 in d:
cans.append(w1)
rec(i + 1, "")
cans.pop()
rec(0, "")
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | def func(s, dic, temp, ans):
if not s:
ans.append(temp[:])
return
for i in range(1, len(s) + 1):
if s[:i] in dic:
func(s[i:], dic, temp + [s[:i]], ans)
class Solution:
def wordBreak(self, n, dic, s):
ans = []
func(s, dic, [], ans)
re = []
for i in ans:
re.append(" ".join(i))
return re | FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR LIST VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR LIST VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def __init__(self):
self.ans = []
def wordBreak(self, n, dict, s):
self.util(n, dict, list(s), [], [])
return self.ans
def util(self, n, dict, s, word, listOfWords):
if s == [] and word == []:
self.ans.append(" ".join(listOfWords))
return
if s == []:
return
word.append(s[0])
if "".join(word) in dict:
listOfWords.append("".join(word))
self.util(n, dict, s[1:], [], listOfWords)
if "".join(word) in dict:
listOfWords.pop()
self.util(n, dict, s[1:], word, listOfWords)
word.pop()
return | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR LIST LIST RETURN VAR FUNC_DEF IF VAR LIST VAR LIST EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN IF VAR LIST RETURN EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER LIST VAR IF FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR RETURN |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dictionary, line):
dic = {}
for i in dictionary:
dic[i] = 1
def manu(dic, word, f, a):
if word == "":
a.append(" ".join(f))
return True
x = False
for i in range(len(word)):
if word[: i + 1] in dic:
f.append(word[: i + 1])
x = manu(dic, word[i + 1 :], f, a) or x
f.pop()
return x
a = []
manu(dic, line, [], a)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR LIST VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def util(self, s, d, ans):
global a
if len(s) == 0:
a[ans] = 1
return
for k in range(1, len(s) + 1):
if d.get(s[:k]) != None:
if len(ans) == 0:
self.util(s[k:], d, ans + s[:k])
else:
self.util(s[k:], d, ans + " " + s[:k])
return
def wordBreak(self, n, dict, s):
d = {}
global a
a = {}
ans = []
for i in dict:
d[i] = 1
self.util(s, d, "")
for i in a.keys():
ans.append(i)
return ans | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NONE IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR STRING VAR VAR RETURN FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class trieNode:
def __init__(self):
self.children = [None] * 26
self.isEnd = False
class trie:
def __init__(self):
self.root = trieNode()
def insert(self, word):
r = self.root
cnt = 1
for i in word:
if r.children[ord(i) - ord("a")] == None:
r.children[ord(i) - ord("a")] = trieNode()
r = r.children[ord(i) - ord("a")]
if cnt == len(word):
r.isEnd = True
cnt += 1
def solver(root, string, ans, curr, i):
r = root
if i >= len(string):
ans.append(curr.strip())
s = ""
while i < len(string):
if root != None:
s += string[i]
if (
root.children[ord(string[i]) - ord("a")]
and root.children[ord(string[i]) - ord("a")].isEnd
):
solver(r, string, ans, curr + " " + s, i + 1)
else:
break
root = root.children[ord(string[i]) - ord("a")]
i += 1
class Solution:
def wordBreak(self, n, dictionary, s):
T = trie()
for i in dictionary:
T.insert(i)
ans = []
solver(T.root, s, ans, "", 0)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NONE ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR STRING NUMBER RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
N = len(s)
dp = [[] for i in range(N + 1)]
dp[0].append("")
for i in range(1, N + 1):
temp = ""
lis = []
for j in range(i, 0, -1):
temp = s[j - 1] + temp
if temp in dict:
for k in dp[j - 1]:
lis.append(k + " " + temp)
dp[i].extend(lis)
ans = []
for i in dp[N]:
ans.append(i[1:])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
mp = set()
for i in dict:
mp.add(i)
res = []
def findsent(i, st, sent, s, mp):
if i >= len(s):
if st == "":
res.append(sent)
return
if st + s[i] in mp:
if len(sent) > 0:
findsent(i + 1, "", sent + " " + st + s[i], s, mp)
else:
findsent(i + 1, "", st + s[i], s, mp)
findsent(i + 1, st + s[i], sent, s, mp)
findsent(0, "", "", s, mp)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP BIN_OP BIN_OP VAR STRING VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER STRING STRING VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
dp = {}
def word_break(s):
if s in dp:
return dp[s]
res = []
for word in dict:
if s[: len(word)] == word:
if len(s) == len(word):
res.append(s)
else:
tmp = word_break(s[len(word) :])
for t in tmp:
res.append(word + " " + t)
dp[s] = res
return res
return word_break(s) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, d, s, till=[]):
if s == "":
return [" ".join(till)]
re = []
for x in range(1, len(s) + 1):
select = s[:x]
if select in d:
re += self.wordBreak(n, d, s[x:], [*till, s[:x]])
return re | CLASS_DEF FUNC_DEF LIST IF VAR STRING RETURN LIST FUNC_CALL STRING VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR LIST VAR VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
def solve(arr, st, l, res):
if st == len(s):
res.append(l[:])
for word in arr:
if s[st : st + len(word)] == word:
l.append(word)
solve(arr, st + len(word), l, res)
l.pop()
res = []
solve(dict, 0, [], res)
res = [" ".join(x) for x in res]
return res | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER LIST VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def word_machingII(self, str1, dict1, st, res, list1):
if st == len(str1):
res.append(list1[:-1])
return
for end in range(st, len(str1) + 1):
if str1[st:end] in dict1:
self.word_machingII(str1, dict1, end, res, list1 + str1[st:end] + " ")
def wordBreak(self, n, dict, s):
res = []
dict1 = set(dict)
st = 0
list1 = ""
self.word_machingII(s, dict1, st, res, list1)
return res | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR STRING FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
dict_set = set(dict)
dp = [False] * (len(s) + 1)
dp[0] = True
for i in range(1, len(s) + 1):
for j in range(i):
if dp[j] and s[j:i] in dict_set:
dp[i] = True
break
res = []
self.backtrack(s, len(s), dict_set, [], res, dp)
return res
def backtrack(self, s, end, dict_set, path, res, dp):
if end == 0:
res.append(" ".join(path[::-1]))
return
for i in range(end):
if dp[i]:
word = s[i:end]
if word in dict_set:
path.append(word)
self.backtrack(s, i, dict_set, path, res, dp)
path.pop() | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR LIST VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
n = len(s)
res = []
def rec(i=0, curr=[]):
if i >= n:
res.append(" ".join(curr))
temp = ""
for idx in range(i, n):
temp += s[idx]
if temp in dict:
curr.append(temp)
rec(idx + 1, curr)
curr.pop()
return
rec()
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF NUMBER LIST IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dic, s):
m = len(s)
l = [[(True if j == 0 else False) for j in range(m + 1)] for i in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, m + 1):
x = len(dic[i - 1])
if x > j:
continue
elif dic[i - 1] == s[j - x : j]:
l[i][j] = True
a = []
def dfs(c, p):
if c == 0:
a.append(" ".join(p[::-1]))
return
for r in range(1, n + 1):
if l[r][c]:
p.append(dic[r - 1])
dfs(c - len(dic[r - 1]), p)
p.pop(-1)
dfs(m, [])
return a | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR LIST RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
return self.break_sentence(dict, s)
def break_sentence(self, words, sentence, slist=None, collect=None):
if slist is None:
slist = []
if collect is None:
collect = []
if len(sentence) == 0:
collect.append(" ".join(slist))
return
for i in range(1, len(sentence) + 1):
if sentence[:i] in words:
slist.append(sentence[:i])
self.break_sentence(words, sentence[i:], slist, collect)
slist.pop()
return collect | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF NONE NONE IF VAR NONE ASSIGN VAR LIST IF VAR NONE ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, _dict, s):
words = set(_dict)
ans = []
slen = len(s)
def backtrack(starti, arr):
nonlocal ans, s, slen, words
if starti == slen:
ans.append(" ".join(arr))
for endi in range(starti, slen):
if s[starti : endi + 1] in words:
arr.append(s[starti : endi + 1])
backtrack(endi + 1, arr)
arr.pop()
backtrack(0, [])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
def breakutil(s, x, res):
for i in range(1, x + 1):
if s[:i] in l:
if i == x:
ans.append(res + s[:i])
return
breakutil(s[i:], x - i, res + s[:i] + " ")
ans = []
l = dict
breakutil(s, len(s), "")
return ans | CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR STRING ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
res = []
path = []
def backtrack(j):
if j == len(s):
res.append(" ".join(path))
for i in range(n):
wordLength = len(dict[i])
if s[j : j + wordLength] != dict[i]:
continue
path.append(dict[i])
backtrack(j + wordLength)
path.pop()
backtrack(0)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
dic = {}
for ele in dict:
dic[ele] = 1
ans = []
li = []
self.helper(dic, s, ans, li, len(s))
return ans
def helper(self, dic, s, ans, li, l):
if l == 0:
ans.append(" ".join(ele for ele in li))
return
for i in range(1, l + 1):
left, right = s[:i], s[i:]
if left in dic:
li.append(left)
self.helper(dic, right, ans, li, len(right))
li.pop()
return | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
d = {}
for i in dict:
d[i] = d.get(i, 0) + 1
final = []
temp = ""
self.solutions(s, d, n, final, temp)
return final
def solutions(self, s, d, n, final, temp):
if s == "":
final.append(temp[:-1])
return
for i in range(len(s)):
if d.get(s[: i + 1], 0) != 0:
self.solutions(s[i + 1 :], d, n, final, temp + s[: i + 1] + " ") | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR STRING EXPR FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER STRING |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
self.sentences = []
self.dict = set(dict)
self.s = s
self.n = len(s)
self.makeSentences(0, "", [])
return self.sentences
def makeSentences(self, index, word, sentence):
if index >= self.n:
if word == "":
self.sentences.append(" ".join(sentence))
return
word += self.s[index]
if word in self.dict:
self.makeSentences(index + 1, "", sentence + [word])
self.makeSentences(index + 1, word, sentence) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING LIST RETURN VAR FUNC_DEF IF VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, d, s):
d = set(d)
pre = set()
for word in d:
for i in range(1, len(word) + 1):
pre.add(word[:i])
def count(s):
ans = []
for i in range(1, len(s) + 1):
word = s[:i]
if word not in pre:
break
if word in d:
if i == len(s):
return ans + [word]
l = count(s[i:])
for sentence in l:
ans.append(word + " " + sentence)
return ans
return count(s) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
list = []
sett = set(dict)
Solution.traverse(0, list, s, sett, "")
return list
@staticmethod
def traverse(index, list, s, sett, str):
if index >= len(s):
list.append(str)
else:
for i in range(index, len(s)):
if s[index : i + 1] in sett:
Solution.traverse(
i + 1,
list,
s,
sett,
str + ("" if str == "" else " ") + s[index : i + 1],
) | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR STRING RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR STRING STRING STRING VAR VAR BIN_OP VAR NUMBER VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def solve(self, ans, curr, i, dict, s):
temp = s[:i]
if i == len(s):
if temp in dict:
ans.append(" ".join(curr + [temp]))
return
if temp not in dict:
self.solve(ans, curr, i + 1, dict, s)
return
curr.append(temp)
self.solve(ans, curr, 0, dict, s[i:])
curr.pop()
self.solve(ans, curr, i + 1, dict, s)
return
def wordBreak(self, n, dict, s):
ans = []
curr = []
self.solve(ans, curr, 0, dict, s)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP VAR LIST VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
res = []
def backtrack(bag, s_):
nonlocal res
if len(s_) == 0:
res.append(" ".join(bag[:]))
return
for i in range(0, len(s_)):
prefix = str(s_[: i + 1])
if prefix in dict:
suffix = s_[i + 1 :]
bag.append(prefix)
backtrack(bag, suffix)
bag.pop()
s_ = s
backtrack([], s_)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
def func(ind, nn, res, ans, dict):
if ind == nn:
res.append(ans[:-1])
return
for i in range(ind, nn + 1):
if s[ind:i] in dict:
func(i, nn, res, ans + s[ind:i] + " ", dict)
res = []
ans = ""
nn = len(s)
dict = set(dict)
func(0, nn, res, ans, dict)
return res | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR STRING VAR ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def __init__(self):
self.ans = []
def wordBreak(self, n, Dict, s):
l = len(s)
dp = [0] * l
for i in range(l):
arr = []
for j in range(i + 1, l + 1):
if s[i:j] in Dict:
arr.append(s[i:j])
dp[i] = arr
sol = [0] * l
for i in range(l - 1, -1, -1):
ans = []
if dp[i]:
for word in dp[i]:
if i + len(word) == l:
ans.append(word)
elif sol[i + len(word)]:
for w in sol[i + len(word)]:
ans.append(word + " " + w)
sol[i] = ans
return sol[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST IF VAR VAR FOR VAR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
sentence = []
sentences = []
self.wordBreakUtil(s, 0, sentence, sentences, dict, len(s))
sentences = [" ".join(word) for word in sentences]
return sentences
def wordBreakUtil(self, input, prev, sentence, sentences, dict, n):
if prev == n:
sentences.append(sentence[:])
return True
for i in range(prev, n + 1):
if input[prev:i] in dict:
sentence.append(input[prev:i])
self.wordBreakUtil(input, i, sentence, sentences, dict, n)
sentence.pop()
return False | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN NUMBER |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, d, s):
ans = []
m = len(s)
flag = [[(False) for _ in range(m)] for _ in range(m)]
for i in range(m):
for j in range(m):
if s[i : j + 1] in d:
flag[i][j] = True
def solve(start, ans, temp, m):
if start >= m:
ans.append(" ".join(temp.copy()))
return
for i in range(m):
if flag[start][i] == True:
temp.append("".join(s[start : i + 1]))
solve(i + 1, ans, temp, m)
temp.pop()
return
temp = []
solve(0, ans, temp, m)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST 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 IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordbreak(self, n, dict, s):
if s == "":
if self.temp == "":
return
self.ans.append(self.temp[:-1])
for i in range(len(s)):
if s[: i + 1] in dict:
self.temp += s[: i + 1]
self.temp += " "
self.wordbreak(n, dict, s[i + 1 :])
self.temp = self.temp[:-1]
self.temp = self.temp[: -(i + 1)]
def wordBreak(self, n, dict, s):
self.ans = []
self.temp = ""
self.wordbreak(n, dict, s)
return self.ans | CLASS_DEF FUNC_DEF IF VAR STRING IF VAR STRING RETURN EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
ind = 0
ans = ""
res = []
def getAns(ind, ans, res):
if ind == len(s):
res.append(ans)
return
for i in range(ind, len(s)):
if s[ind : i + 1] in dict:
getAns(
i + 1, ans + ("" if ans == "" else " ") + s[ind : i + 1], res
)
getAns(0, ans, res)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR STRING STRING STRING VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
dict = set(dict)
m = len(s)
ans = []
cans = []
def rec(i, w):
if i == m:
if w == "":
ans.append(" ".join(cans))
return
temp = w + s[i]
if temp in dict:
cans.append(temp)
rec(i + 1, "")
cans.pop()
rec(i + 1, temp)
rec(0, "")
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF IF VAR VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
wordset = set(dict)
res = []
def word(ind=0, path=[]):
if ind == len(s):
res.append(" ".join(path))
for j in range(ind + 1, len(s) + 1):
words = s[ind:j]
if words in wordset:
word(j, path + [words])
word()
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF NUMBER LIST IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR EXPR FUNC_CALL VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def search(self, word, dict):
if word in dict:
return True
return False
def solve(self, start, end, s, dict, words, ans):
if start > end:
ans.append(words[:])
return
for i in range(start, end + 1):
if self.search(s[start : i + 1], dict):
words.append(s[start : i + 1])
self.solve(i + 1, end, s, dict, words, ans)
words.pop()
def wordBreak(self, n, dict, s):
ans = []
words = []
self.solve(0, len(s) - 1, s, dict, words, ans)
ans = list(map(lambda x: " ".join(x), ans))
return ans | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, words, s):
ans = []
rec(s, words, [], ans)
return ans
def rec(s, words, acc, ans):
if not s:
ans.append(" ".join(acc))
return
for w in words:
if s.startswith(w):
acc.append(w)
rec(s[len(w) :], words, acc, ans)
acc.pop() | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR LIST VAR RETURN VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
res = []
def backtrack(pos, sub, s):
if pos == len(s):
copy = " ".join(sub)
res.append(copy)
return
for idx in range(pos, len(s)):
temp = s[pos : idx + 1]
if temp not in dict:
continue
sub.append(temp)
backtrack(idx + 1, sub, s)
sub.pop()
backtrack(0, [], s)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
n = len(s)
ans = []
def solve(idx, curr, n):
if idx == n:
ans.append(" ".join(curr.copy()))
return
for i in range(idx, n + 1):
substr = s[idx : i + 1]
if substr in dict:
curr.append(substr)
solve(i + 1, curr, n)
curr.pop()
solve(0, [], n)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER LIST VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
sents = []
def wordBreak(self, n, dictionary, line):
self.sents = []
r = ""
self.sol(line, dictionary, 0, r)
return self.sents
def sol(self, line, dic, i, r):
if i == len(line):
if r[0] == " ":
r = r[1:]
self.sents.append(r)
return
for j in dic:
l = len(j)
x = line[i : i + l].find(j)
if x > -1:
self.sol(line, dic, i + l, r + " " + j) | CLASS_DEF ASSIGN VAR LIST FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR STRING VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, wordDict, s):
ans = []
def backtrack(idx, word, spaces):
if idx == len(s) and len(word) - spaces == len(s):
ans.append(word[1:])
return
if idx > len(s):
return
for w in wordDict:
if idx + len(w) <= len(s):
brokenWord = s[idx : idx + len(w)]
if brokenWord == w:
backtrack(idx + len(w), word + " " + brokenWord, spaces + 1)
backtrack(idx + 1, word, spaces)
backtrack(0, "", 0)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN IF VAR FUNC_CALL VAR VAR RETURN FOR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR STRING VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER STRING NUMBER RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
ans = []
def recurbreak(string, m, result):
for i in range(1, m + 1):
prefix = string[:i]
if prefix in dict:
if i == m:
result += prefix
ans.append(result)
return
recurbreak(string[i:], m - i, result + prefix + " ")
recurbreak(s, len(s), "")
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, d, s):
def WordBreak(indx, n, dict, s, curr, ans):
if indx == n:
ans.append(curr)
return
str = ""
for i in range(indx, n):
str += s[i]
if str in dict:
ch = ""
if i + 1 != n:
ch = " "
WordBreak(i + 1, n, dict, s, curr + str + ch, ans)
dict = {}
for i in range(n):
dict[d[i]] = True
ans = []
WordBreak(0, len(s), dict, s, "", ans)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR STRING VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Node:
def __init__(self, val):
self.val = val
self.children = [None for _ in range(26)]
self.eow = False
class Trie:
def __init__(self):
self.root = Node(-1)
def insert(self, word, node=None, i=0):
if node is None:
node = self.root
c = word[i]
o = ord(c) - ord("a")
if node.children[o] is None:
node.children[o] = Node(c)
if i == len(word) - 1:
node.children[o].eow = True
else:
self.insert(word, node.children[o], i + 1)
def query(self, word, node=None, i=0):
if node is None:
node = self.root
c = word[i]
o = ord(c) - ord("a")
if node.children[o] is None:
return 2
elif i < len(word) - 1:
return self.query(word, node.children[o], i + 1)
elif node.children[o].eow:
return 0
else:
return 1
def printVal(self, node=None, prefix=""):
if node is None:
node = self.root
for child in node.children:
if child is not None:
self.printVal(child, prefix + " ")
class Solution:
def wordBreak(self, n, dic, s):
trie = Trie()
for word in dic:
trie.insert(word)
res = self.helper(s, trie)
return res
def helper(self, s, trie, start=0):
res = []
for i in range(start + 1, len(s) + 1):
word = s[start:i]
query_res = trie.query(word)
if query_res == 0:
if i == len(s):
res.append(word)
return res
for sub_res in self.helper(s, trie, i):
res.append(word + " " + sub_res)
elif query_res == 1:
continue
else:
break
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF NONE NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF NONE NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR NONE RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF NONE STRING IF VAR NONE ASSIGN VAR VAR FOR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR BIN_OP VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR STRING VAR IF VAR NUMBER RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | def word_break_util(input_string, result_arr: [], input_dict: {}, output_string):
if len(input_string) == 0:
if output_string != "":
result_arr.append(output_string)
return
n = len(input_string)
for i in range(0, n):
word = input_string[0 : i + 1]
if word in input_dict:
word_break_util(
input_string[i + 1 :],
result_arr,
input_dict,
output_string + str(word) + "-",
)
def word_break_main(input_string: [], input_dict: {}):
result_arr = []
word_break_util(input_string, result_arr, input_dict, "")
final_arr = []
for data in result_arr:
result_words = data.split("-")[:-1]
final_arr.append(" ".join(result_words))
return final_arr
class Solution:
def wordBreak(self, n, dict, s):
return word_break_main(s, dict) | FUNC_DEF LIST DICT IF FUNC_CALL VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING FUNC_DEF LIST DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
def wordbreakUtil(ind, dict, s, temp):
if ind == len(s):
res.append(temp[:])
return
t = ""
for i in range(ind, len(s)):
t += s[i]
if t in dict:
temp.append(t)
wordbreakUtil(i + 1, dict, s, temp)
temp.pop()
return
res = []
wordbreakUtil(0, dict, s, [])
ans = []
for x in res:
ans.append(" ".join(x))
return ans | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER VAR VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | def words(dict, s, cur, ans):
if s == "":
ans.append(cur[0 : len(cur) - 1])
for i in range(1, len(s) + 1):
if s[0:i] in dict:
words(dict, s[i:], cur + s[0:i] + " ", ans)
class Solution:
def wordBreak(self, n, dict, s):
ans = []
words(dict, s, "", ans)
return ans | FUNC_DEF IF VAR STRING EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR STRING VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR STRING VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, store, s):
return self.helper(s, store, "")
def helper(self, s, store, output):
if not s:
return [output[:-1]]
result = []
for item in store:
n = len(item)
if s[:n] == item:
result += self.helper(s[n:], store, output + s[:n] + " ")
return result | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR STRING FUNC_DEF IF VAR RETURN LIST VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR STRING RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Node:
def __init__(self):
self.map = {}
self.isWord = False
class Trie:
def __init__(self):
self.head = Node()
self.ans = []
def insert(self, word):
temp = self.head
for i in word:
if i not in temp.map:
temp.map[i] = Node()
temp = temp.map[i]
temp.isWord = True
def check(self, word, s=""):
if not word:
self.ans.append(s.strip())
return
temp = self.head
for i in range(len(word)):
if word[i] in temp.map:
temp = temp.map[word[i]]
if temp.isWord:
self.check(word[i + 1 :], s + " " + word[: i + 1])
else:
return
return
class Solution:
def wordBreak(self, n, wordDict, s):
trie = Trie()
for word in wordDict:
trie.insert(word)
trie.check(s)
return trie.ans | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR STRING VAR BIN_OP VAR NUMBER RETURN RETURN CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
def backtrack(s, n, dict, index, output):
if index == len(s):
result.append(" ".join(output))
return
for word in dict:
if index + len(word) <= len(s) and s[index : index + len(word)] == word:
output.append(word)
backtrack(s, n, dict, index + len(word), output)
output.pop()
result = []
backtrack(s, n, dict, 0, [])
return result | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN FOR VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR NUMBER LIST RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def search(self, word, dict):
if word in dict:
return True
return False
def solve(self, start, end, s, word_map, words, ans):
if start > end:
ans.append(words[:])
return
for i in range(start, end + 1):
if word_map.get(s[start : i + 1]):
words.append(s[start : i + 1])
self.solve(i + 1, end, s, word_map, words, ans)
words.pop()
def solve(self, start, s, word_map, words, ans):
if start == len(s):
ans.append(words[:-1])
return
for i in range(start, len(s)):
if word_map.get(s[start : i + 1]):
self.solve(i + 1, s, word_map, words + s[start : i + 1] + " ", ans)
def wordBreak(self, n, dict, s):
ans = []
words = []
word_map = {word: (1) for word in dict}
self.solve(0, s, word_map, "", ans)
return ans | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR STRING VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
ans = []
def func(i, st):
if i == len(s):
ans.append(st[:-1])
return
for j in range(i, len(s) + 1):
if s[i:j] in dict:
func(j, st + s[i:j] + " ")
func(0, "")
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, m, dic, s):
global ans
ans = []
n = len(s)
solve(dic, n, s, 0, "")
return ans
def solve(dic, n, s, i, res):
if i == n:
ans.append(res.strip())
return
for j in range(i, n):
wd = s[i : j + 1]
if wd in dic:
solve(dic, n, s, j + 1, res + wd + " ") | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER STRING RETURN VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR STRING |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dict, s):
return self.explore(s, dict, {})
def explore(self, target, wordbank, memo):
if target == "":
return [""]
if target in memo:
return memo[target]
res = []
for word in wordbank:
if target.startswith(word):
suffix = target[len(word) :]
suffix_ways = self.explore(suffix, wordbank, memo)
for ways in suffix_ways:
res.append(" ".join([word, ways]).strip())
memo[target] = res
return res | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR DICT FUNC_DEF IF VAR STRING RETURN LIST STRING IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL STRING LIST VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given a string s and a dictionary of words dict of length n, add spaces in s to construct a sentence where each word is a valid dictionary word. Each dictionary word can be used more than once. Return all such possible sentences.
Follow examples for better understanding.
Example 1:
Input: s = "catsanddog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: (cats and dog)(cat sand dog)
Explanation: All the words in the given
sentences are present in the dictionary.
Example 2:
Input: s = "catsandog", n = 5
dict = {"cats", "cat", "and", "sand", "dog"}
Output: Empty
Explanation: There is no possible breaking
of the string s where all the words are present
in dict.
Your Task:
You do not need to read input or print anything. Your task is to complete the function wordBreak() which takes n, dict and s as input parameters and returns a list of possible sentences. If no sentence is possible it returns an empty list.
Expected Time Complexity: O(N^{2}*n) where N = |s|
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ n ≤ 20
1 ≤ dict[i] ≤ 15
1 ≤ |s| ≤ 500 | class Solution:
def wordBreak(self, n, dicti, s):
wordSet = set(dicti)
res = []
def backtrack(i, j, temp):
if i == len(s) - 1 or j == len(s) - 1:
if s[i : j + 1] in wordSet:
z = temp + " " + s[i : j + 1]
res.append(z[1:])
else:
return
if s[i : j + 1] in wordSet:
backtrack(j + 1, j + 1, temp + " " + s[i : j + 1])
if j < len(s) - 1:
backtrack(i, j + 1, temp)
backtrack(0, 0, "")
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR STRING VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER STRING RETURN VAR |
Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.
He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space):
### ..# .#. #..
.#. ### .#. ###
.#. ..# ### #..
Simon faced a quite natural challenge: placing in the given n × m cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.
Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him?
Input
The only line contains two space-separated integers n and m — the sizes of the warehouse (1 ≤ n, m ≤ 9).
Output
In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next n lines print m characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them.
Examples
Input
3 3
Output
1
AAA
.A.
.A.
Input
5 6
Output
4
A..C..
AAAC..
ABCCCD
.B.DDD
BBB..D
Input
2 2
Output
0
..
.. | n, m = map(int, input().split())
swapped = False
if n < m:
n, m = m, n
swapped = True
ans = ""
if n == 1 and m == 1:
ans = "0\n."
if n == 2 and m == 1:
ans = "0\n.\n."
if n == 2 and m == 2:
ans = "0\n..\n.."
if n == 3 and m == 1:
ans = "0\n.\n.\n."
if n == 3 and m == 2:
ans = "0\n..\n..\n.."
if n == 3 and m == 3:
ans = "1\n.A.\n.A.\nAAA"
if n == 4 and m == 1:
ans = "0\n.\n.\n.\n."
if n == 4 and m == 2:
ans = "0\n..\n..\n..\n.."
if n == 4 and m == 3:
ans = "1\n.A.\n.A.\nAAA\n..."
if n == 4 and m == 4:
ans = "2\n...A\nBAAA\nBBBA\nB..."
if n == 5 and m == 1:
ans = "0\n.\n.\n.\n.\n."
if n == 5 and m == 2:
ans = "0\n..\n..\n..\n..\n.."
if n == 5 and m == 3:
ans = "2\n..A\nAAA\n.BA\n.B.\nBBB"
if n == 5 and m == 4:
ans = "2\n.A..\n.A..\nAAAB\n.BBB\n...B"
if n == 5 and m == 5:
ans = "4\nBBB.A\n.BAAA\nDB.CA\nDDDC.\nD.CCC"
if n == 6 and m == 1:
ans = "0\n.\n.\n.\n.\n.\n."
if n == 6 and m == 2:
ans = "0\n..\n..\n..\n..\n..\n.."
if n == 6 and m == 3:
ans = "2\n.A.\n.A.\nAAA\n.B.\n.B.\nBBB"
if n == 6 and m == 4:
ans = "3\n.A..\n.A..\nAAAB\nCBBB\nCCCB\nC..."
if n == 6 and m == 5:
ans = "4\n.A..B\n.ABBB\nAAACB\n.D.C.\n.DCCC\nDDD.."
if n == 6 and m == 6:
ans = "5\n.A..B.\n.ABBB.\nAAACB.\nECCCD.\nEEECD.\nE..DDD"
if n == 7 and m == 1:
ans = "0\n.\n.\n.\n.\n.\n.\n."
if n == 7 and m == 2:
ans = "0\n..\n..\n..\n..\n..\n..\n.."
if n == 7 and m == 3:
ans = "3\n..A\nAAA\nB.A\nBBB\nBC.\n.C.\nCCC"
if n == 7 and m == 4:
ans = "4\n...A\nBAAA\nBBBA\nB..C\nDCCC\nDDDC\nD..."
if n == 7 and m == 5:
ans = "5\n.A..B\n.ABBB\nAAACB\n.CCC.\n.D.CE\n.DEEE\nDDD.E"
if n == 7 and m == 6:
ans = "6\n.A..B.\n.ABBB.\nAAACB.\nEEECCC\n.EDC.F\n.EDFFF\n.DDD.F"
if n == 7 and m == 7:
ans = "8\nB..ACCC\nBBBA.C.\nBDAAACE\n.DDDEEE\nGDHHHFE\nGGGH.F.\nG..HFFF"
if n == 8 and m == 1:
ans = "0\n.\n.\n.\n.\n.\n.\n.\n."
if n == 8 and m == 2:
ans = "0\n..\n..\n..\n..\n..\n..\n..\n.."
if n == 8 and m == 3:
ans = "3\n.A.\n.A.\nAAA\n..B\nBBB\n.CB\n.C.\nCCC"
if n == 8 and m == 4:
ans = "4\n.A..\n.A..\nAAAB\nCBBB\nCCCB\nCD..\n.D..\nDDD."
if n == 8 and m == 5:
ans = "6\n.A..B\n.ABBB\nAAACB\nDDDC.\n.DCCC\nFD.E.\nFFFE.\nF.EEE"
if n == 8 and m == 6:
ans = "7\n.A..B.\n.ABBB.\nAAA.BC\nDDDCCC\n.DGGGC\n.DFGE.\nFFFGE.\n..FEEE"
if n == 8 and m == 7:
ans = "9\nB..ACCC\nBBBA.C.\nBDAAACE\n.DDDEEE\nFD.IIIE\nFFFHIG.\nFHHHIG.\n...HGGG"
if n == 9 and m == 8:
ans = """12
.A..BDDD
.ABBBCD.
AAAEBCD.
FEEECCCG
FFFEHGGG
FKKKHHHG
.IKLH.J.
.IKLLLJ.
IIIL.JJJ"""
if n == 8 and m == 8:
ans = "10\n.A..BCCC\n.A..B.C.\nAAABBBCD\nE.GGGDDD\nEEEGJJJD\nEF.GIJH.\n.FIIIJH.\nFFF.IHHH"
if n == 9 and m == 9:
ans = """13
AAA.BCCC.
.ABBB.CD.
.AE.BFCD.
EEEFFFDDD
G.E.HFIII
GGGJHHHI.
GK.JHL.IM
.KJJJLMMM
KKK.LLL.M"""
if n == 9 and m == 1:
ans = "0\n.\n.\n.\n.\n.\n.\n.\n.\n."
if n == 9 and m == 2:
ans = "0\n..\n..\n..\n..\n..\n..\n..\n..\n.."
if n == 9 and m == 3:
ans = "4\n..A\nAAA\nB.A\nBBB\nB.C\nCCC\n.DC\n.D.\nDDD"
if n == 9 and m == 4:
ans = "5\n.A..\n.A..\nAAAB\nCBBB\nCCCB\nC..D\nEDDD\nEEED\nE..."
if n == 9 and m == 5:
ans = "7\n.A..B\n.ABBB\nAAACB\nDCCC.\nDDDCF\nDEFFF\n.E.GF\nEEEG.\n..GGG"
if n == 9 and m == 6:
ans = "8\n.A..B.\n.ABBB.\nAAACB.\nECCCD.\nEEECD.\nE.GDDD\n.FGGGH\n.FGHHH\nFFF..H"
if n == 9 and m == 7:
ans = "10\n.ACCC.B\n.A.CBBB\nAAACD.B\n...EDDD\nFEEED.G\nFFFEGGG\nFHJJJIG\n.H.J.I.\nHHHJIII"
if swapped:
cnt = ans.split("\n")[0]
lst = ans.split("\n")[1:]
ans = ""
for j in range(m):
for i in range(n):
ans = ans + lst[i][j]
ans += "\n"
ans = cnt + "\n" + ans
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR STRING VAR EXPR FUNC_CALL VAR VAR |
Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse.
He's got problems with some particularly large piece of equipment, which is, of course, turboplows. The problem is that when a turboplow is stored, it takes up not some simply rectangular space. It takes up a T-shaped space like on one of the four pictures below (here character "#" stands for the space occupied by the turboplow and character "." stands for the free space):
### ..# .#. #..
.#. ### .#. ###
.#. ..# ### #..
Simon faced a quite natural challenge: placing in the given n × m cells warehouse the maximum number of turboplows. As one stores the turboplows, he can rotate them in any manner (so that they take up the space like on one of the four pictures above). However, two turboplows cannot "overlap", that is, they cannot share the same cell in the warehouse.
Simon feels that he alone cannot find the optimal way of positioning the plugs in the warehouse that would maximize their quantity. Can you help him?
Input
The only line contains two space-separated integers n and m — the sizes of the warehouse (1 ≤ n, m ≤ 9).
Output
In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next n lines print m characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of turboplows in your scheme) to mark place for the corresponding turboplows considering that they are positioned in the optimal manner in the warehouse. The order in which you number places for the turboplows does not matter. If there are several optimal solutions for a warehouse of the given size, print any of them.
Examples
Input
3 3
Output
1
AAA
.A.
.A.
Input
5 6
Output
4
A..C..
AAAC..
ABCCCD
.B.DDD
BBB..D
Input
2 2
Output
0
..
.. | r = {}
r[1, 1] = "0\n.\n"
r[1, 2] = "0\n..\n"
r[1, 3] = "0\n...\n"
r[1, 4] = "0\n....\n"
r[1, 5] = "0\n.....\n"
r[1, 6] = "0\n......\n"
r[1, 7] = "0\n.......\n"
r[1, 8] = "0\n........\n"
r[1, 9] = "0\n.........\n"
r[2, 2] = "0\n..\n..\n"
r[2, 3] = "0\n...\n...\n"
r[2, 4] = "0\n....\n....\n"
r[2, 5] = "0\n.....\n.....\n"
r[
2, 6
] = """0
......
......
"""
r[
2, 7
] = """0
.......
.......
"""
r[
2, 8
] = """0
........
........
"""
r[
2, 9
] = """0
.........
.........
"""
r[
3, 3
] = """1
.A.
.A.
AAA
"""
r[
3, 4
] = """1
.A..
.A..
AAA.
"""
r[
3, 5
] = """2
BBBA.
.B.A.
.BAAA
"""
r[
3, 6
] = """2
.B.A..
.B.AAA
BBBA..
"""
r[
3, 7
] = """3
A.BBBC.
AAAB.C.
A..BCCC
"""
r[
3, 8
] = """3
.BCCC.A.
.B.CAAA.
BBBC..A.
"""
r[
3, 9
] = """4
BBBDAAA.C
.B.D.ACCC
.BDDDA..C
"""
r[
4, 4
] = """2
BBB.
.BA.
.BA.
.AAA
"""
r[
4, 5
] = """2
....A
.BAAA
.BBBA
.B...
"""
r[
4, 6
] = """3
..A...
.CAAAB
.CABBB
CCC..B
"""
r[
4, 7
] = """4
...ACCC
BAAADC.
BBBADC.
B..DDD.
"""
r[
4, 8
] = """4
BBB.A...
.BD.AAAC
.BD.ACCC
.DDD...C
"""
r[
4, 9
] = """5
...ACCC..
DAAAECBBB
DDDAEC.B.
D..EEE.B.
"""
r[
5, 5
] = """4
D.BBB
DDDB.
DA.BC
.ACCC
AAA.C
"""
r[
5, 6
] = """4
A..DDD
AAA.D.
ABBBDC
..BCCC
..B..C
"""
r[
5, 7
] = """5
.A.EEE.
.AAAEB.
CA.DEB.
CCCDBBB
C.DDD..
"""
r[
5, 8
] = """6
A..D.FFF
AAADDDF.
AB.DECF.
.BEEECCC
BBB.EC..
"""
r[
5, 9
] = """7
..CAAAB..
CCC.AGBBB
F.CDAGBE.
FFFDGGGE.
F.DDD.EEE
"""
r[
6, 6
] = """5
AAA...
.ABCCC
.AB.C.
DBBBCE
DDDEEE
D....E
"""
r[
6, 7
] = """6
..A...B
AAA.BBB
E.ADDDB
EEECDF.
ECCCDF.
...CFFF
"""
r[
6, 8
] = """7
F.DDD..G
FFFDBGGG
F.ADBBBG
AAAEBC..
..AE.CCC
..EEEC..
"""
r[
6, 9
] = """8
F..AAAGGG
FFFCA.BG.
FCCCA.BG.
HHHCDBBBE
.HDDD.EEE
.H..D...E
"""
r[
7, 7
] = """8
H..AEEE
HHHA.E.
HDAAAEC
.DDDCCC
BDFFFGC
BBBF.G.
B..FGGG
"""
r[
7, 8
] = """9
F.III..G
FFFIAGGG
FH.IAAAG
.HHHADDD
EHBBBCD.
EEEB.CD.
E..BCCC.
"""
r[
7, 9
] = """10
...BJJJ.A
.BBBGJAAA
E..BGJC.A
EEEGGGCCC
EHDDDFC.I
.H.D.FIII
HHHDFFF.I
"""
r[
8, 8
] = """10
A..BBB..
AAAJBIII
AH.JB.I.
.HJJJGI.
HHHC.GGG
ECCCDG.F
EEECDFFF
E..DDD.F
"""
r[
8, 9
] = """12
DDDLLL..H
.DC.LGHHH
.DC.LGGGH
ECCCBGKKK
EEEABBBK.
EI.ABJ.KF
.IAAAJFFF
III.JJJ.F
"""
r[
9, 9
] = """13
K.DDDEMMM
KKKD.E.M.
KA.DEEEMF
.AAALGFFF
HALLLGGGF
HHHBLGCCC
HI.B..JC.
.IBBB.JC.
III..JJJ.
"""
n, m = map(int, input().split())
if n < m:
print(r[n, m]),
else:
s = r[m, n].strip().split("\n")
print(s[0])
s = s[1:]
s = zip(*s)
for t in s:
print("".join(t)) | ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | R = lambda: map(int, input().split())
I = 10**10
n = int(input())
s, c = list(R()), list(R())
r = min(
c[j]
+ min([I] + [c[i] for i in range(j) if s[i] < s[j]])
+ min([I] + [c[i] for i in range(j + 1, n) if s[i] > s[j]])
for j in range(n)
)
print((r, -1)[r > I]) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
a, b = 1000000000000000.0, 1000000000000000.0
ans = 1000000000000000.0
for i in range(1, n):
a, b = 1000000000000000.0, 1000000000000000.0
for j in range(0, i):
if s[i] > s[j]:
a = min(a, c[j])
for j in range(i + 1, n):
if s[j] > s[i]:
b = min(b, c[j])
ans = min(ans, c[i] + a + b)
if ans == 1000000000000000.0:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL 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 VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
INF = 10**18
dp2 = [INF] * n
for i in range(n):
for j in range(0, i):
if s[i] > s[j] and dp2[i] > c[i] + c[j]:
dp2[i] = c[i] + c[j]
dp3 = [INF] * n
for i in range(n):
for j in range(0, i):
if s[i] > s[j] and dp3[i] > c[i] + dp2[j]:
dp3[i] = c[i] + dp2[j]
ans = min(dp3)
if ans == INF:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL 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 BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
num = float("inf")
rec1 = [float("inf")] * n
rec2 = [float("inf")] * n
for i in range(n):
for j in range(i + 1, n):
if s[i] < s[j] and rec1[i] > c[j]:
rec1[i] = c[j]
for i in range(1, n):
for j in range(i):
if s[j] < s[i] and rec2[i] > c[j]:
rec2[i] = c[j]
for i in range(n):
num = min(num, rec1[i] + c[i] + rec2[i])
if num == float("inf"):
print(-1)
else:
print(num) | ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
dp = [[float("inf") for _ in range(4)] for _ in range(n)]
for i in range(n):
dp[i][1] = c[i]
for i in range(n):
for j in range(1, 4):
for r in range(i):
if s[r] < s[i]:
dp[i][j] = min(dp[i][j], c[i] + dp[r][j - 1])
ans = min(i[3] for i in dp)
if ans == float("inf"):
ans = -1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().strip().split()))
c = list(map(int, input().strip().split()))
dp = [float("inf") for i in range(n)]
for i in range(n):
for j in range(i):
if s[j] < s[i]:
dp[i] = min(dp[i], c[i] + c[j])
dp2 = [float("inf") for i in range(n)]
for i in range(n):
for j in range(i):
if s[j] < s[i]:
dp2[i] = min(dp2[i], dp[j] + c[i])
ans = min(dp2)
if ans == float("inf"):
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | def solve(n, s, c):
INF = float("inf")
ans = INF
for i in range(n):
first = INF
for j in range(i):
if s[j] < s[i]:
first = min(first, c[j])
if first == INF:
continue
second = INF
for j in range(i + 1, n):
if s[i] < s[j]:
second = min(second, c[j])
if second == INF:
continue
ans = min(ans, first + c[i] + second)
ans = -1 if ans == INF else ans
print(ans)
n = int(input())
s = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
solve(n, s, c) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | import sys
def get_ints():
return list(map(int, sys.stdin.readline().strip().split()))
def solve(N, S, C):
dp = [float("inf")] * N
for i in range(1, N):
for j in range(i):
if S[j] < S[i]:
dp[i] = min(dp[i], C[j] + C[i])
dp2 = [float("inf")] * N
for i in range(N - 1, 0, -1):
for j in range(i + 1, N, 1):
if S[i] < S[j]:
dp2[j] = min(dp2[j], dp[i] + C[j])
ans = min(dp2)
if ans == float("inf"):
return -1
return ans
N = int(input())
S = get_ints()
C = get_ints()
print(solve(N, S, C)) | IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
l = list(map(int, input().split()))
l1 = list(map(int, input().split()))
ans = []
j = 1
while j < n - 1:
m1 = m2 = 2000000000.0
i = j - 1
k = j + 1
while i >= 0:
if l[i] < l[j]:
m1 = min(m1, l1[i])
i -= 1
while k < n:
if l[k] > l[j]:
m2 = min(m2, l1[k])
k += 1
x = m1 + m2 + l1[j]
ans.append(x)
j += 1
a = min(ans)
if a >= 2000000000.0:
print(-1)
else:
print(a) | ASSIGN VAR FUNC_CALL 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 ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
dp = [float("inf")] * n
for i in range(1, n):
mn = float("inf")
for j in range(i):
if s[i] > s[j]:
mn = min(mn, c[i] + c[j])
dp[i] = mn
res = float("inf")
for i in range(1, n):
for j in range(i):
if s[i] > s[j]:
res = min(res, c[i] + dp[j])
if res == float("inf"):
res = -1
print(res) | ASSIGN VAR FUNC_CALL 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 BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
minCost = float("inf")
for j in range(1, n - 1):
curr = s[j]
minLs = float("inf")
minMax = float("inf")
for i in range(j):
if s[i] < curr:
minLs = min(minLs, c[i])
for i in range(j + 1, n):
if s[i] > curr:
minMax = min(minMax, c[i])
if minLs == float("inf") or minMax == float("inf"):
continue
minCost = min(minCost, c[j] + minLs + minMax)
print(-1 if minCost == float("inf") else minCost) | ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | ans = float("inf")
n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
for i in range(n):
b = -1
c_ = c[i]
for j in range(i):
if s[j] >= s[i]:
continue
if b == -1 or c[b] > c[j]:
b = j
if b == -1:
continue
c_ += c[b]
b = -1
for j in range(i + 1, n):
if s[j] <= s[i]:
continue
if b == -1 or c[b] > c[j]:
b = j
if b == -1:
continue
c_ += c[b]
ans = min(ans, c_)
if ans == float("inf"):
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | R = lambda: [int(i) for i in input().split()]
I = 10**9
n = int(input())
s, c = R(), R()
r = I
for j in range(n):
t1 = I
for i in range(j):
if s[i] < s[j]:
t1 = min(t1, c[i])
t2 = I
for i in range(j + 1, n):
if s[i] > s[j]:
t2 = min(t2, c[i])
r = min(c[j] + t1 + t2, r)
if r < I:
print(r)
else:
print("-1") | ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
ss = list(map(int, input().strip().split()))
cs = list(map(int, input().strip().split()))
dp = [([float("inf")] * 4) for _ in range(n + 1)]
for i in range(1, n + 1):
dp[i][1] = cs[i - 1]
for i in range(2, n + 1):
for j in range(2, 4):
s, c = ss[i - 1], cs[i - 1]
for k in range(1, i):
if ss[k - 1] < s:
dp[i][j] = min(dp[i][j], dp[k][j - 1] + c)
mmin = float("inf")
for i in range(1, 1 + n):
mmin = min(mmin, dp[i][3])
if mmin == float("inf"):
print(-1)
else:
print(mmin) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | class SegTree:
def __init__(self, init_val, ide_ele, segfunc):
self.n = len(init_val)
self.num = 2 ** (self.n - 1).bit_length()
self.ide_ele = ide_ele
self.segfunc = segfunc
self.seg = [ide_ele] * 2 * self.num
for i in range(self.n):
self.seg[i + self.num] = init_val[i]
for i in range(self.num - 1, 0, -1):
self.seg[i] = self.segfunc(self.seg[2 * i], self.seg[2 * i + 1])
def update(self, k, x):
k += self.num
self.seg[k] = x
while k:
k = k >> 1
self.seg[k] = self.segfunc(self.seg[2 * k], self.seg[2 * k + 1])
def query(self, l, r):
if r <= l:
return self.ide_ele
l += self.num
r += self.num
lres = self.ide_ele
rres = self.ide_ele
while l < r:
if r & 1:
r -= 1
rres = self.segfunc(self.seg[r], rres)
if l & 1:
lres = self.segfunc(lres, self.seg[l])
l += 1
l = l >> 1
r = r >> 1
res = self.segfunc(lres, rres)
return res
def __str__(self):
arr = [self.query(i, i + 1) for i in range(self.n)]
return str(arr)
n = int(input())
S = list(map(int, input().split()))
C = list(map(int, input().split()))
SA = list(set(S))
SA = sorted(SA)
d = {}
for i, s in enumerate(SA):
d[s] = i
S = [d[s] for s in S]
L = [0] * n
R = [0] * n
INF = 10**18
N = len(d)
seg = SegTree([INF] * (N + 1), INF, min)
seg.update(S[0], C[0])
for i in range(1, n - 1):
s = S[i]
L[i] = seg.query(0, s)
seg.update(s, C[i])
seg = SegTree([INF] * (N + 1), INF, min)
seg.update(S[-1], C[-1])
for i in reversed(range(1, n - 1)):
s = S[i]
R[i] = seg.query(s + 1, seg.n)
seg.update(s, C[i])
ans = INF
for i in range(1, n - 1):
ans = min(ans, L[i] + C[i] + R[i])
if ans >= INF:
print(-1)
else:
print(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
dp2 = [float("inf") for i in range(n)]
dp3 = [float("inf") for i in range(n)]
for i in range(1, n):
j = i - 1
while j >= 0:
if s[j] < s[i]:
dp2[i] = min(c[i] + c[j], dp2[i])
j -= 1
ans = float("inf")
for i in range(2, n):
j = i - 1
while j >= 0:
if s[j] < s[i]:
dp3[i] = min(dp3[i], c[i] + dp2[j])
ans = min(ans, dp3[i])
j -= 1
print(ans if ans != float("inf") else -1) | ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
S = [int(x) for x in input().split(" ")]
C = [int(x) for x in input().split(" ")]
dp = [float("inf")] * n
ans = float("inf")
for i in range(n):
for j in range(i + 1, n):
if S[j] > S[i] and C[j] < dp[i]:
dp[i] = C[j]
for i in range(n):
for j in range(i + 1, n):
if S[j] > S[i]:
ans = min(ans, C[i] + C[j] + dp[j])
if ans == float("inf"):
ans = -1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | def main():
n, inf, res = int(input()), 9000000000.0, []
ss = list(map(float, input().split()))
cc = list(map(float, input().split()))
tmp = [(ss[0], cc[0])]
for s, c in zip(ss[1:-1], cc[1:-1]):
res.append(min((b for a, b in tmp if a < s), default=inf) + c)
tmp.append((s, c))
res.reverse()
tmp = [(ss[-1], cc[-1])]
for i, s, c in zip(range(n), ss[-2:0:-1], cc[-2:0:-1]):
res[i] += min((b for a, b in tmp if a > s), default=inf)
tmp.append((s, c))
r = min(res)
print(int(r) if r < inf else -1)
main() | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER LIST 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 NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | class Display:
def __init__(self, index, size, cost):
self.index = index
self.size = size
self.cost = cost
self.totalcost = cost
self.next = None
self.level = 0
def nextcycle(displays, level):
for j in range(n):
displays[j].next = None
for k in range(j + 1, n):
if displays[j].size < displays[k].size and displays[k].level == level:
if (
displays[j].next == None
or displays[k].totalcost < displays[j].next.totalcost
):
displays[j].next = displays[k]
if displays[j].next != None:
displays[j].totalcost = displays[j].cost + displays[j].next.totalcost
displays[j].level = displays[j].next.level + 1
n = int(input())
sizes = input().split(" ")
costs = input().split(" ")
displays = []
i = 0
while i < n:
displays.append(Display(i, int(sizes[i]), int(costs[i])))
i += 1
for c in range(2):
nextcycle(displays, c)
bestcost = float("inf")
for display in displays:
if display.level == 2 and display.totalcost < bestcost:
bestcost = display.totalcost
print(-1 if bestcost == float("inf") else bestcost) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NONE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s1 = list(map(int, input().split()))
s2 = list(map(int, input().split()))
cn = 300000001
ext = 0
for i in range(1, n - 1):
indj = s1[i]
vali, valk, valj = 100000001, 100000001, s2[i]
for j in range(0, i):
if s1[j] < indj:
if s2[j] < vali:
vali = s2[j]
for j in range(i + 1, n):
if s1[j] > indj:
if s2[j] < valk:
valk = s2[j]
if vali + valk + valj < cn:
cn = vali + valk + valj
if vali == 100000001 or valk == 100000001:
ext += 1
if ext == n - 2:
print(-1)
else:
print(cn) | ASSIGN VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = 10**10
for j in range(1, n - 1):
now_ans = c[j]
money = 10**10
for i in range(j):
if s[i] < s[j]:
money = min(money, c[i])
if money == 10**10:
pass
now_ans += money
money = 10**10
for i in range(j + 1, n):
if s[i] > s[j]:
money = min(money, c[i])
if money == 10**10:
pass
now_ans += money
ans = min(ans, now_ans)
if ans == 10**10:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL 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 BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
arr = list(map(int, input().split()))
cost = list(map(int, input().split()))
ans = 10**10
for i in range(1, n - 1):
cur = arr[i]
l, r = 10**10, 10**10
for j in range(i - 1, -1, -1):
if arr[j] < cur:
l = min(l, cost[j])
for j in range(i + 1, n):
if arr[j] > cur:
r = min(r, cost[j])
ans = min(ans, cost[i] + l + r)
if ans == 10**10:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL 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 BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = (10**9 + 1) * 3
m = 10**9 + 1
m1 = 10**9 + 1
t = 0
for i in range(0, n):
m = 10**9 + 1
m1 = 10**9 + 1
su = b[i]
for j in range(0, i):
if a[j] < a[i]:
m = min(m, b[j])
for j in range(i + 1, n):
if a[j] > a[i]:
m1 = min(m1, b[j])
if m != 10**9 + 1 and m1 != 10**9 + 1:
t = 1
ans = min(ans, su + m + m1)
if t == 1:
print(ans)
else:
print(-1) | ASSIGN VAR FUNC_CALL 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 BIN_OP BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | from sys import stdin, stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int, stdin.readline().split()))
for _ in range(1):
n = nmbr()
a = lst()
b = lst()
ans = PI = float("inf")
dp = [[PI for _ in range(4)] for _ in range(n)]
for i in range(n):
dp[i][1] = b[i]
for j in range(i):
if a[j] < a[i]:
dp[i][2] = min(dp[i][2], dp[j][1] + b[i])
dp[i][3] = min(dp[i][3], dp[j][2] + b[i])
ans = min(ans, dp[i][3])
print(ans if ans != PI else -1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | class node:
def __init__(self):
self.id = 0
self.c = 0
self.s = 0
n = int(input())
p = [node() for i in range(n)]
for i in range(n):
p[i].id = i
a = list(map(int, input().split()))
for i in range(n):
p[i].s = a[i]
a = list(map(int, input().split()))
for i in range(n):
p[i].c = a[i]
p.sort(key=lambda x: x.s)
ans = int(10000000000.0)
for i in range(1, n - 1):
t1 = int(10000000000.0)
t2 = int(10000000000.0)
for j in range(i):
if p[j].id < p[i].id and p[j].s < p[i].s:
t1 = min(t1, p[j].c)
for j in range(i + 1, n):
if p[j].id > p[i].id and p[j].s > p[i].s:
t2 = min(t2, p[j].c)
ans = min(ans, t1 + t2 + p[i].c)
if ans == int(10000000000.0):
print(-1)
else:
print(ans) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | from sys import stdin, stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int, stdin.readline().split()))
for _ in range(1):
n = nmbr()
a = lst()
b = lst()
dp = [0] * n
for i in range(n):
v = float("inf")
for j in range(i + 1, n):
if a[j] > a[i]:
v = min(v, b[i] + b[j])
dp[i] = v
for i in range(n):
v = float("inf")
for j in range(i + 1, n):
if a[j] > a[i]:
v = min(v, b[i] + dp[j])
dp[i] = v
ans = min(dp)
print(ans if ans != float("inf") else -1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
j1 = [float("inf")] * n
j2 = [float("inf")] * n
for i in range(n):
for j in range(i + 1, n):
if a[i] < a[j]:
j1[j] = min(j1[j], b[i] + b[j])
for i in range(n):
for j in range(i + 1, n):
if a[i] < a[j]:
j2[i] = min(j2[i], b[i] + b[j])
ans = min([(x[0] + x[1] - b[i]) for i, x in enumerate(zip(j1, j2))])
if ans == float("inf"):
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL 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 BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
l = list(map(int, input().split()))
v = list(map(int, input().split()))
d = dict()
for i in range(n):
take = []
for j in range(i + 1, n):
if l[i] < l[j]:
take.append(v[j])
d[i] = sorted(take)
mini = 99999999999
for i in range(n):
sum1 = v[i]
for j in range(i + 1, n):
if l[i] < l[j]:
sum1 = v[j] + v[i]
if len(d[j]) != 0:
sum1 = v[i] + v[j] + d[j][0]
if mini > sum1:
mini = sum1
if mini == 99999999999:
print("-1")
else:
print(mini) | ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | from sys import stdin
n = int(stdin.readline())
size = list(map(int, stdin.readline().split()))
price = list(map(int, stdin.readline().split()))
temp = [3 * 10**8 + 1] * n
ans = [3 * 10**8 + 1] * n
for i in range(n):
for j in range(i + 1, n):
if size[i] >= size[j]:
continue
temp[j] = min(temp[j], price[i] + price[j])
for i in range(n):
for j in range(i + 1, n):
if size[i] >= size[j]:
continue
ans[j] = min(ans[j], temp[i] + price[j])
res = 3 * 10**8 + 1
for i in range(n):
if res > ans[i]:
res = ans[i]
if res == 3 * 10**8 + 1:
print("-1")
else:
print(res) | ASSIGN VAR FUNC_CALL 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 BIN_OP LIST BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | def fxn1(arr, ls, j, m1=10**9):
for i in range(0, j):
if arr[i] < arr[j]:
m1 = min(m1, ls[i])
return m1
def fxn2(arr, ls, j, m2=10**9):
for k in range(j + 1, len(arr)):
if arr[k] > arr[j]:
m2 = min(m2, ls[k])
return m2
t1 = 10**9
t2 = 10**9
z = 10**9
n = int(input())
arr = list(map(int, input().split()))
ls = list(map(int, input().split()))
for j in range(1, n - 1):
t1 = fxn1(arr, ls, j)
t2 = fxn2(arr, ls, j)
z = min(ls[j] + t1 + t2, z)
if z == 10**9:
print(-1)
else:
print(z) | FUNC_DEF BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | from sys import stdin, stdout
inf = 3000000000.0
n = int(input())
s = [int(x) for x in input().strip().split(" ")]
c = [int(x) for x in input().strip().split(" ")]
dp1 = [inf for x in s]
dp2 = [inf for x in s]
for i in range(1, n - 1):
for j in range(0, i):
if s[j] < s[i]:
dp1[i] = min(dp1[i], c[j])
for i in range(1, n - 1):
for j in range(i + 1, n):
if s[j] > s[i]:
dp2[i] = min(dp2[i], c[j])
ans = 3000000000.0
for i in range(1, n - 1):
ans = min(ans, c[i] + dp1[i] + dp2[i])
if ans >= 3000000000.0:
print("-1")
else:
print(ans) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
S = [int(x) for x in input().split(" ")]
C = [int(x) for x in input().split(" ")]
bestCost = float("inf")
for i in range(len(S)):
minLeft = float("inf")
minRight = float("inf")
for j in range(i):
if S[j] < S[i] and C[j] < minLeft:
minLeft = C[j]
if minLeft == float("inf"):
continue
for j in range(i, len(S)):
if S[j] > S[i] and C[j] < minRight:
minRight = C[j]
if minRight == float("inf"):
continue
if minLeft + minRight + C[i] < bestCost:
bestCost = minLeft + minRight + C[i]
if bestCost == float("inf"):
bestCost = -1
print(bestCost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR STRING IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
ar = list(map(int, input().split()))
ar1 = list(map(int, input().split()))
ans3 = 10**18
for i in range(n):
ans1 = 10**9 + 1
ans2 = 10**9 + 1
for j in range(i - 1, -1, -1):
if ar[i] > ar[j]:
ans1 = min(ans1, ar1[j])
for j in range(i + 1, n):
if ar[i] < ar[j]:
ans2 = min(ans2, ar1[j])
if ans1 != 10**9 + 1 and ans2 != 10**9 + 1:
ans3 = min(ans3, ans1 + ans2 + ar1[i])
if ans3 != 10**18:
print(ans3)
else:
print(-1) | ASSIGN VAR FUNC_CALL 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 BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | inf = 1234567890
class fenwick:
def __init__(self, n):
self.n = n
self.f = [inf] * n
def get(self, x):
i = x
ret = inf
while i >= 0:
ret = min(ret, self.f[i])
i = (i & i + 1) - 1
return ret
def add(self, x, y):
i = x
while i < self.n:
self.f[i] = min(self.f[i], y)
i = i | i + 1
n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
map_trick = {}
for x in s:
map_trick[x] = 0
map_trick[x - 1] = 0
m = 0
keys = sorted(list(map_trick.keys()))
for key in keys:
map_trick[key] = m
m += 1
a = list(zip(s, c))
ans = inf
fen = fenwick(8000)
for i in range(n):
for j in range(i + 1, n):
size_first, cost_first = a[i]
size_second, cost_second = a[j]
if size_first < size_second:
ans = min(
ans, cost_first + cost_second + fen.get(map_trick[size_first] - 1)
)
fen.add(map_trick[size_first], cost_first)
print(ans if ans < inf else -1) | ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL 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 DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | import sys
n = int(input().strip())
sizes = list(map(int, input().strip().split()))
cost = list(map(int, input().strip().split()))
tot = []
for i in range(n):
tot.append([sizes[i], cost[i]])
ret = False
lcomp = []
for j in range(len(tot)):
if j > 0 and j < len(tot) - 1:
temp1 = tot[:j]
temp2 = tot[j + 1 :]
mi_1 = sys.maxsize
ret1 = False
for i in range(len(temp1)):
if temp1[i][0] < tot[j][0]:
mi_1 = min(mi_1, temp1[i][1])
ret1 = True
mi_2 = sys.maxsize
ret2 = False
for k in range(len(temp2)):
if temp2[k][0] > tot[j][0]:
mi_2 = min(mi_2, temp2[k][1])
ret2 = True
if ret1 and ret2:
ret = True
lcomp.append(mi_1 + tot[j][1] + mi_2)
if ret:
print(min(lcomp))
else:
print(-1) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
S = [int(x) for x in input().split()]
s = tuple(S)
c = tuple(int(x) for x in input().split())
S.sort()
def min(x, y):
if x < y:
return x
else:
return y
def rank(x):
l = 0
r = n - 1
while l < r:
mid = l + r >> 1
if S[mid] < x:
l = mid + 1
else:
r = mid
return l
rk = tuple(rank(x) for x in s)
Min = [(10000000000) for i in range(30000)]
def insert(x, l, r, p, v):
Min[x] = min(Min[x], v)
if l == r:
return
mid = l + r >> 1
if p > mid:
insert(x << 1 | 1, mid + 1, r, p, v)
else:
insert(x << 1, l, mid, p, v)
def query(x, l, r, L, R):
if l >= L and r <= R:
return Min[x]
mid = l + r >> 1
ans = 10000000000
if L <= mid:
ans = min(ans, query(x << 1, l, mid, L, R))
if R > mid:
ans = min(ans, query(x << 1 | 1, mid + 1, r, L, R))
return ans
dp = list(c)
Min = [(10000000000) for i in range(30000)]
for i in range(0, n):
insert(1, 0, n - 1, rk[i], dp[i])
dp[i] = c[i]
if rk[i] == 0:
dp[i] += 10000000000
else:
dp[i] += query(1, 0, n - 1, 0, rk[i] - 1)
Min = [(10000000000) for i in range(30000)]
for i in range(0, n):
insert(1, 0, n - 1, rk[i], dp[i])
dp[i] = c[i]
if rk[i] == 0:
dp[i] += 10000000000
else:
dp[i] += query(1, 0, n - 1, 0, rk[i] - 1)
ans = 10000000000
for i in dp:
ans = min(ans, i)
if ans > 3000000000:
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | def main():
n = int(input())
s = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
Max = 2 * sum(c)
f = [x for x in c]
for _ in range(2):
for i in range(n - 1, -1, -1):
min_fj = Max
for j in range(i):
if s[j] < s[i]:
min_fj = min(f[j], min_fj)
if min_fj == Max:
f[i] = Max
else:
f[i] = min_fj + c[i]
min_cost = min(f)
if min_cost == Max:
print(-1)
else:
print(min_cost)
def __starting_point():
main()
__starting_point() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = float("inf")
dp = [[float("inf") for x in range(3)] for u in range(len(s) + 1)]
for i in range(len(s)):
dp[i][0] = c[i]
for j in range(1, 3):
dp[i][j] = float("inf")
for k in range(i):
if s[k] < s[i]:
dp[i][j] = min(dp[i][j], dp[k][j - 1] + c[i])
ans = min(ans, dp[i][2])
if ans == float("inf"):
print(-1)
else:
print(ans) | ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | import sys
inf = float("inf")
abc = "abcdefghijklmnopqrstuvwxyz"
abd = {
"a": 0,
"b": 1,
"c": 2,
"d": 3,
"e": 4,
"f": 5,
"g": 6,
"h": 7,
"i": 8,
"j": 9,
"k": 10,
"l": 11,
"m": 12,
"n": 13,
"o": 14,
"p": 15,
"q": 16,
"r": 17,
"s": 18,
"t": 19,
"u": 20,
"v": 21,
"w": 22,
"x": 23,
"y": 24,
"z": 25,
}
mod, MOD = 1000000007, 998244353
vow = ["a", "e", "i", "o", "u"]
dx, dy = [-1, 1, 0, 0], [0, 0, 1, -1]
def get_array():
return list(map(int, sys.stdin.readline().strip().split()))
def get_ints():
return map(int, sys.stdin.readline().strip().split())
def input():
return sys.stdin.readline().strip()
n = int(input())
Arr = get_array()
cost = get_array()
flag = 0
min_total = inf
for i in range(1, n - 1):
mini_i = cost[i]
mini_j = inf
mini_k = inf
for j in range(i):
if Arr[j] < Arr[i]:
mini_j = min(mini_j, cost[j])
for k in range(i + 1, n):
if Arr[k] > Arr[i]:
mini_k = min(mini_k, cost[k])
if mini_i != inf and mini_j != inf and mini_k != inf:
flag = 1
min_total = min(min_total, mini_i + mini_j + mini_k)
if flag == 0:
print(-1)
else:
print(min_total) | IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | INF = 10000000000
n = int(input())
s = list(map(int, input().split()))
c = list(map(int, input().split()))
min_v = INF
for j in range(1, n):
left = INF
for i in range(j):
if s[j] > s[i]:
left = min(left, c[i] + c[j])
curr = INF
for k in range(j + 1, n):
if s[k] > s[j]:
curr = min(curr, left + c[k])
min_v = min(min_v, curr)
if min_v != INF:
print(min_v)
else:
print(-1) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL 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 VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | def read_line():
return [int(x) for x in input().split()]
def read_int():
return int(input())
def solve():
n = read_int()
a = read_line()
b = read_line()
res = 1e18
for j in range(n):
cost = [1e18, 1e18]
for i in range(0, j):
if a[i] < a[j]:
cost[0] = min(cost[0], b[i])
for k in range(j, n):
if a[j] < a[k]:
cost[1] = min(cost[1], b[k])
res = min(res, cost[0] + cost[1] + b[j])
print(res if res != 1e18 else -1)
t = 1
while t > 0:
solve()
t -= 1 | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are $n$ displays placed along a road, and the $i$-th of them can display a text with font size $s_i$ only. Maria Stepanovna wants to rent such three displays with indices $i < j < k$ that the font size increases if you move along the road in a particular direction. Namely, the condition $s_i < s_j < s_k$ should be held.
The rent cost is for the $i$-th display is $c_i$. Please determine the smallest cost Maria Stepanovna should pay.
-----Input-----
The first line contains a single integer $n$ ($3 \le n \le 3\,000$) — the number of displays.
The second line contains $n$ integers $s_1, s_2, \ldots, s_n$ ($1 \le s_i \le 10^9$) — the font sizes on the displays in the order they stand along the road.
The third line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^8$) — the rent costs for each display.
-----Output-----
If there are no three displays that satisfy the criteria, print -1. Otherwise print a single integer — the minimum total rent cost of three displays with indices $i < j < k$ such that $s_i < s_j < s_k$.
-----Examples-----
Input
5
2 4 5 4 10
40 30 20 10 40
Output
90
Input
3
100 101 100
2 4 5
Output
-1
Input
10
1 2 3 4 5 6 7 8 9 10
10 13 11 14 15 12 13 13 18 13
Output
33
-----Note-----
In the first example you can, for example, choose displays $1$, $4$ and $5$, because $s_1 < s_4 < s_5$ ($2 < 4 < 10$), and the rent cost is $40 + 10 + 40 = 90$.
In the second example you can't select a valid triple of indices, so the answer is -1. | import sys
imput = sys.stdin.readline
class SegmentTree:
def __init__(self, n):
self.n = n
self.INF = 2**31 - 1
self.size = 1
while self.size < n:
self.size *= 2
self.node = [self.INF] * (2 * self.size - 1)
def update(self, i, val):
i += self.size - 1
self.node[i] = val
while i > 0:
i = (i - 1) // 2
self.node[i] = min(self.node[2 * i + 1], self.node[2 * i + 2])
def get_min(self, begin, end):
begin += self.size - 1
end += self.size - 1
s = self.INF
while begin < end:
if end - 1 & 1:
end -= 1
s = min(s, self.node[end])
if begin - 1 & 1:
s = min(s, self.node[begin])
begin += 1
begin = (begin - 1) // 2
end = (end - 1) // 2
return s
def compress(array):
array2 = sorted(set(array))
memo = {value: index for index, value in enumerate(array2)}
for i in range(len(array)):
array[i] = memo[array[i]]
return array
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
st = SegmentTree(n)
a = compress(a)
ans = [0] * n
for i in range(n):
ans[i] = st.get_min(0, a[i])
st.update(a[i], min(b[i], st.get_min(a[i], a[i] + 1)))
st = SegmentTree(n)
for i in range(n)[::-1]:
ans[i] += st.get_min(a[i] + 1, n)
st.update(a[i], min(b[i], st.get_min(a[i], a[i] + 1)))
for i in range(n):
ans[i] += b[i]
min_ans = min(ans)
if min_ans < 2**31 - 1:
print(min_ans)
else:
print(-1) | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.