description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problem statements in [Bengali], [Russian], [Mandarin] and [Vietnamese] as well.
A string S whose characters are one of the following 5: (, ), +, - and ?, is said to be a *valid string* if one of the following conditions holds:
S = \ ?
S = (x+y), where x and y are valid strings.
S = (x-y), where x and y are valid strings.
For example, strings ?, (?-?), (?+(?-?)) are valid strings, while strings (?-+), ??, (?), ?-?, (?+?)) are not.
The *power* of a valid string S is defined to be the maximum value attainable by replacing each ? in it by either 0 or 1 and evaluating the resulting expression.
For example, the power of (?-?) is 1 because we can obtain (1 - 0) which evaluates to 1.
You are given a valid string S, and Q queries on it. Each query is represented by two integers L and R (L≤ R).
Your task is to find the power of the substring S_{L}, S_{L+1}, \dots, S_{R}. It is guaranteed that this substring is a valid string.
Note: The input of this problem is large, so use fast input/output methods.
------ Input Format ------
- The first line of input contains an integer T, denoting the number of test cases. The description of T test cases follows:
- The first line of each test case contains the string S.
- The next line contains an integer Q, representing the number of queries to answer.
- Each of the following Q lines contains two space-separated integers, L and R, representing the query.
------ Output Format ------
For each test case, print one line containing Q space-separated integers, the i-th of which represents the answer to the i-th query.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ |S| ≤ 10^{6}$
$1 ≤ Q ≤ 10^{6}$
$1 ≤ L ≤ R ≤ |S|$
$\sum{|S|}$ and $\sum{Q}$ over all test cases does not exceed $10^{6}$.
------ subtasks ------
Subtask #1 (20 points):
$1 ≤ |S| ≤ 1000$
$1 ≤ Q ≤ 1000$
$\sum{|S|}$ and $\sum{Q}$ over all test case does not exceed $10^{4}$.
Subtask #2 (80 points):
Original constraints
----- Sample Input 1 ------
3
(?+?)
2
2 2
1 5
(?-(?-?))
2
1 9
4 8
(?-(?+?))
1
1 9
----- Sample Output 1 ------
1 2
2 1
1
----- explanation 1 ------
Test case 1:
- Query 1: Power of the string $?$ is $1$, as it can be converted to $1$.
- Query 2: Power of the string $(?+?)$ is $2$, as it can be converted to $(1+1)$.
Test case 2:
- Query 1: Power of the string $(?-(?-?))$ is $2$, as it can be converted to $(1-(0-1))$.
- Query 2: Power of the string $(?-?)$ is $1$, as it can be converted to $(1-0)$.
Test case 3:
- Query 1: Power of the string $(?-(?+?))$ is $1$, as it can be converted to $(1-(0+0))$. | import sys
sys.setrecursionlimit(10**8)
def helper(ind, str):
index = ind + 1
max = 0
min = 0
sign = True
while str[index] != ")":
char = str[index]
if char == "?":
if sign:
max += 1
else:
min += 1
elif char == "+":
sign = True
elif char == "-":
sign = False
elif char == "(":
arr = helper(index, str)
if sign:
max += arr[0]
min += arr[1]
else:
max += arr[1]
min += arr[0]
index = arr[2]
index += 1
ind_map[ind] = max
return [max, min, index]
T = int(input())
for i in range(T):
s = input()
ind_map = {}
helper(0, s)
query = int(input())
ans = []
for k in range(query):
ind, fin = map(int, input().split())
if ind == fin:
ans.append(1)
else:
ans.append(ind_map[ind - 1])
print(*ans) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR VAR VAR IF VAR STRING IF VAR VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problem statements in [Bengali], [Russian], [Mandarin] and [Vietnamese] as well.
A string S whose characters are one of the following 5: (, ), +, - and ?, is said to be a *valid string* if one of the following conditions holds:
S = \ ?
S = (x+y), where x and y are valid strings.
S = (x-y), where x and y are valid strings.
For example, strings ?, (?-?), (?+(?-?)) are valid strings, while strings (?-+), ??, (?), ?-?, (?+?)) are not.
The *power* of a valid string S is defined to be the maximum value attainable by replacing each ? in it by either 0 or 1 and evaluating the resulting expression.
For example, the power of (?-?) is 1 because we can obtain (1 - 0) which evaluates to 1.
You are given a valid string S, and Q queries on it. Each query is represented by two integers L and R (L≤ R).
Your task is to find the power of the substring S_{L}, S_{L+1}, \dots, S_{R}. It is guaranteed that this substring is a valid string.
Note: The input of this problem is large, so use fast input/output methods.
------ Input Format ------
- The first line of input contains an integer T, denoting the number of test cases. The description of T test cases follows:
- The first line of each test case contains the string S.
- The next line contains an integer Q, representing the number of queries to answer.
- Each of the following Q lines contains two space-separated integers, L and R, representing the query.
------ Output Format ------
For each test case, print one line containing Q space-separated integers, the i-th of which represents the answer to the i-th query.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ |S| ≤ 10^{6}$
$1 ≤ Q ≤ 10^{6}$
$1 ≤ L ≤ R ≤ |S|$
$\sum{|S|}$ and $\sum{Q}$ over all test cases does not exceed $10^{6}$.
------ subtasks ------
Subtask #1 (20 points):
$1 ≤ |S| ≤ 1000$
$1 ≤ Q ≤ 1000$
$\sum{|S|}$ and $\sum{Q}$ over all test case does not exceed $10^{4}$.
Subtask #2 (80 points):
Original constraints
----- Sample Input 1 ------
3
(?+?)
2
2 2
1 5
(?-(?-?))
2
1 9
4 8
(?-(?+?))
1
1 9
----- Sample Output 1 ------
1 2
2 1
1
----- explanation 1 ------
Test case 1:
- Query 1: Power of the string $?$ is $1$, as it can be converted to $1$.
- Query 2: Power of the string $(?+?)$ is $2$, as it can be converted to $(1+1)$.
Test case 2:
- Query 1: Power of the string $(?-(?-?))$ is $2$, as it can be converted to $(1-(0-1))$.
- Query 2: Power of the string $(?-?)$ is $1$, as it can be converted to $(1-0)$.
Test case 3:
- Query 1: Power of the string $(?-(?+?))$ is $1$, as it can be converted to $(1-(0+0))$. | def wildcard(s):
b = []
m = []
o = []
d = {}
for i in range(len(s)):
if s[i] == "(":
b.append(i + 1)
elif s[i] == "?":
m.append([0, 1])
d[i + 1, i + 1] = 1
elif s[i] == "-" or s[i] == "+":
o.append(s[i])
else:
if o[-1] == "+":
d[b[-1], i + 1] = max(m[-1]) + max(m[-2])
temp = [min(m[-1]) + min(m[-2]), max(m[-1]) + max(m[-2])]
else:
d[b[-1], i + 1] = max(m[-2]) - min(m[-1])
temp = [min(m[-2]) - max(m[-1]), max(m[-2]) - min(m[-1])]
b.pop()
m.pop()
m.pop()
m.append(temp)
o.pop()
return d
for _ in range(int(input())):
s = input()
find = wildcard(s)
new = []
for _ in range(int(input())):
x, y = map(int, input().split())
new.append(find[x, y])
print(*new) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problem statements in [Bengali], [Russian], [Mandarin] and [Vietnamese] as well.
A string S whose characters are one of the following 5: (, ), +, - and ?, is said to be a *valid string* if one of the following conditions holds:
S = \ ?
S = (x+y), where x and y are valid strings.
S = (x-y), where x and y are valid strings.
For example, strings ?, (?-?), (?+(?-?)) are valid strings, while strings (?-+), ??, (?), ?-?, (?+?)) are not.
The *power* of a valid string S is defined to be the maximum value attainable by replacing each ? in it by either 0 or 1 and evaluating the resulting expression.
For example, the power of (?-?) is 1 because we can obtain (1 - 0) which evaluates to 1.
You are given a valid string S, and Q queries on it. Each query is represented by two integers L and R (L≤ R).
Your task is to find the power of the substring S_{L}, S_{L+1}, \dots, S_{R}. It is guaranteed that this substring is a valid string.
Note: The input of this problem is large, so use fast input/output methods.
------ Input Format ------
- The first line of input contains an integer T, denoting the number of test cases. The description of T test cases follows:
- The first line of each test case contains the string S.
- The next line contains an integer Q, representing the number of queries to answer.
- Each of the following Q lines contains two space-separated integers, L and R, representing the query.
------ Output Format ------
For each test case, print one line containing Q space-separated integers, the i-th of which represents the answer to the i-th query.
------ Constraints ------
$1 ≤ T ≤ 1000$
$1 ≤ |S| ≤ 10^{6}$
$1 ≤ Q ≤ 10^{6}$
$1 ≤ L ≤ R ≤ |S|$
$\sum{|S|}$ and $\sum{Q}$ over all test cases does not exceed $10^{6}$.
------ subtasks ------
Subtask #1 (20 points):
$1 ≤ |S| ≤ 1000$
$1 ≤ Q ≤ 1000$
$\sum{|S|}$ and $\sum{Q}$ over all test case does not exceed $10^{4}$.
Subtask #2 (80 points):
Original constraints
----- Sample Input 1 ------
3
(?+?)
2
2 2
1 5
(?-(?-?))
2
1 9
4 8
(?-(?+?))
1
1 9
----- Sample Output 1 ------
1 2
2 1
1
----- explanation 1 ------
Test case 1:
- Query 1: Power of the string $?$ is $1$, as it can be converted to $1$.
- Query 2: Power of the string $(?+?)$ is $2$, as it can be converted to $(1+1)$.
Test case 2:
- Query 1: Power of the string $(?-(?-?))$ is $2$, as it can be converted to $(1-(0-1))$.
- Query 2: Power of the string $(?-?)$ is $1$, as it can be converted to $(1-0)$.
Test case 3:
- Query 1: Power of the string $(?-(?+?))$ is $1$, as it can be converted to $(1-(0+0))$. | import sys
sys.setrecursionlimit(10**8)
T = int(input())
for testCase in range(T):
data = input()
myList = {}
one = 1
zero = 0
flag = True
def wildcardreplace(L, data):
index, longData, shortData, sy = L + 1, zero, zero, True
while data[index] != ")":
C = data[index + zero]
if C == "?":
if sy:
longData += one - zero
else:
shortData += one
elif C == "+" and flag:
sy = True
elif C == "-":
sy = False
elif C == "(":
a = wildcardreplace(index, data)
if sy:
longData += a[zero]
shortData += a[one]
else:
longData += a[one]
shortData += a[zero]
index = a[2]
w = 2 * index - 1
index += one - zero
myList[L] = longData
return [longData, shortData, index]
wildcardreplace(zero, data)
K = int(input())
results = []
for i in range(K):
L, R = map(int, input().split())
if L == R and flag:
results.append(one)
else:
results.append(myList[L - one])
print(*results) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER WHILE VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR IF VAR STRING IF VAR VAR BIN_OP VAR VAR VAR VAR IF VAR STRING VAR ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.
Input
First line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.
Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.
Output
Print one integer — the answer to the problem.
Examples
Input
5 2
1
2
3
5
4
Output
7 | from sys import stdin
def main():
n, k = map(int, input().split())
dp = [([0] * (n + 1)) for _ in range(k + 1)]
col0, dp1 = dp[0], dp[1:]
for x in map(int, stdin.read().splitlines()):
x1 = x
while x1 <= n:
col0[x1] += 1
x1 += x1 & -x1
for col1, col2 in zip(dp, dp1):
res, x1 = 0, x - 1
while x1:
res += col1[x1]
x1 -= x1 & -x1
x1 = x
while x1 <= n:
col2[x1] += res
x1 += x1 & -x1
res = 0
while n:
res += dp[k][n]
n -= n & -n
print(res)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.
Input
First line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.
Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.
Output
Print one integer — the answer to the problem.
Examples
Input
5 2
1
2
3
5
4
Output
7 | import sys
def update(BIT, i, value):
while i <= len(BIT) - 1:
BIT[i] += value
i += i & -i
def query(BIT, start, end):
if start > 0:
return query(BIT, 0, end) - query(BIT, 0, start - 1)
res = 0
while end > 0:
res += BIT[end]
end -= end & -end
return res
def read_str():
return sys.stdin.readline().strip()
def read_ints():
return list(map(int, sys.stdin.readline().strip().split()))
n, k = list(map(int, input().split()))
a = [read_ints()[0] for _ in range(n)]
dp = [([0] * (n + 1)) for i in range(k + 1)]
res = [1] * n
for i in range(n):
update(dp[0], a[i], 1)
for j in range(1, k + 1):
res[i] = query(dp[j - 1], 0, a[i] - 1)
update(dp[j], a[i], res[i])
print(sum(res)) | IMPORT FUNC_DEF WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
A festival will be held in a town's main street. There are n sections in the main street. The sections are numbered 1 through n from left to right. The distance between each adjacent sections is 1.
In the festival m fireworks will be launched. The i-th (1 ≤ i ≤ m) launching is on time t_{i} at section a_{i}. If you are at section x (1 ≤ x ≤ n) at the time of i-th launching, you'll gain happiness value b_{i} - |a_{i} - x| (note that the happiness value might be a negative value).
You can move up to d length units in a unit time interval, but it's prohibited to go out of the main street. Also you can be in an arbitrary section at initial time moment (time equals to 1), and want to maximize the sum of happiness that can be gained from watching fireworks. Find the maximum total happiness.
Note that two or more fireworks can be launched at the same time.
-----Input-----
The first line contains three integers n, m, d (1 ≤ n ≤ 150000; 1 ≤ m ≤ 300; 1 ≤ d ≤ n).
Each of the next m lines contains integers a_{i}, b_{i}, t_{i} (1 ≤ a_{i} ≤ n; 1 ≤ b_{i} ≤ 10^9; 1 ≤ t_{i} ≤ 10^9). The i-th line contains description of the i-th launching.
It is guaranteed that the condition t_{i} ≤ t_{i} + 1 (1 ≤ i < m) will be satisfied.
-----Output-----
Print a single integer — the maximum sum of happiness that you can gain from watching all the fireworks.
Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
-----Examples-----
Input
50 3 1
49 1 1
26 1 4
6 1 10
Output
-31
Input
10 2 1
1 1000 4
9 1000 4
Output
1992 | class SortedList(list):
def add(self, other):
left = -1
right = len(self)
while right - left > 1:
mid = right + left >> 1
if other < self[mid]:
right = mid
else:
left = mid
super().insert(right, other)
INF = int(3e18)
def solve_good(n, m, d, a, b, t):
left = SortedList()
left.append(-INF)
right = SortedList()
right.append(INF)
lborder = -INF
rborder = INF
tprev = 0
ans = 0
for ai, bi, ti in zip(a, b, t):
ans += bi
dt = ti - tprev
interval = dt * d
tprev = ti
lborder += interval
rborder -= interval
lefta = lborder + ai
righta = rborder - (n - ai)
if lefta < left[-1]:
top = left.pop()
ans -= abs(top - lefta)
left.add(lefta)
left.add(lefta)
right.add(rborder - (n - abs(top - lborder)))
elif righta > right[0]:
top = right.pop(0)
ans -= abs(top - righta)
right.add(righta)
right.add(righta)
left.add(lborder + n - abs(top - rborder))
else:
left.add(lefta)
right.add(righta)
return ans
n, m, d = [int(elem) for elem in input().split()]
a, b, t = [], [], []
for i in range(m):
ai, bi, ti = [int(elem) for elem in input().split()]
a.append(ai)
b.append(bi)
t.append(ti)
print(solve_good(n, m, d, a, b, t)) | CLASS_DEF VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR |
Given an array nums of integers, you can perform operations on the array.
In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
You start with 0 points. Return the maximum number of points you can earn by applying such operations.
Example 1:
Input: nums = [3, 4, 2]
Output: 6
Explanation:
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.
Example 2:
Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation:
Delete 3 to earn 3 points, deleting both 2's and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned.
Note:
The length of nums is at most 20000.
Each element nums[i] is an integer in the range [1, 10000]. | class Solution:
def deleteAndEarn(self, nums):
count = collections.Counter(nums)
prev = None
avoid = using = 0
for k in sorted(count):
temp = max(avoid, using)
if k - 1 != prev:
using = k * count[k] + temp
avoid = temp
else:
using = k * count[k] + avoid
avoid = temp
prev = k
return max(avoid, using) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given an array nums of integers, you can perform operations on the array.
In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
You start with 0 points. Return the maximum number of points you can earn by applying such operations.
Example 1:
Input: nums = [3, 4, 2]
Output: 6
Explanation:
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.
Example 2:
Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation:
Delete 3 to earn 3 points, deleting both 2's and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned.
Note:
The length of nums is at most 20000.
Each element nums[i] is an integer in the range [1, 10000]. | class Solution:
def deleteAndEarn(self, nums):
if len(nums) == 0:
return 0
nums.sort()
count = collections.Counter(nums)
take = [0] * (1 + nums[-1])
delete = [0] * (1 + nums[-1])
try:
take[1] = count[1]
except KeyError:
take[1] = 0
for i in range(2, nums[-1] + 1):
print(i)
try:
delete[i] = max(take[i - 1], delete[i - 1])
take[i] = i * count[i] + max(take[i - 2], delete[i - 1])
except KeyError:
continue
print(take)
print(delete)
res = max(take[-1], delete[-1])
return res | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an array nums of integers, you can perform operations on the array.
In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
You start with 0 points. Return the maximum number of points you can earn by applying such operations.
Example 1:
Input: nums = [3, 4, 2]
Output: 6
Explanation:
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.
Example 2:
Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation:
Delete 3 to earn 3 points, deleting both 2's and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned.
Note:
The length of nums is at most 20000.
Each element nums[i] is an integer in the range [1, 10000]. | class Solution:
def deleteAndEarn(self, nums):
if len(nums) == 0:
return 0
nums.sort()
a = [0] * len(nums)
b = [0] * len(nums)
k = -1
a[-1] = nums[-1]
b[-1] = 0
for i in range(len(nums) - 2, -1, -1):
print(nums[i], nums[i + 1])
if nums[i] == nums[i + 1] - 1:
a[i] = max(a[k], b[k]) + nums[i] if k != -1 else nums[i]
b[i] = max(a[i + 1], b[i + 1])
elif nums[i] == nums[i + 1]:
a[i] = a[i + 1] + nums[i]
b[i] = b[i + 1]
else:
a[i] = max(a[i + 1], b[i + 1]) + nums[i]
b[i] = max(a[i + 1], b[i + 1])
if nums[i] < nums[i + 1]:
k = i + 1
return max(a[0], b[0]) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Given an array nums of integers, you can perform operations on the array.
In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
You start with 0 points. Return the maximum number of points you can earn by applying such operations.
Example 1:
Input: nums = [3, 4, 2]
Output: 6
Explanation:
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.
Example 2:
Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation:
Delete 3 to earn 3 points, deleting both 2's and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned.
Note:
The length of nums is at most 20000.
Each element nums[i] is an integer in the range [1, 10000]. | class Solution:
def deleteAndEarn(self, nums):
nums.sort()
nums.append(-1)
prev_max, cur_max = 0, 0
prev = -1
j, i = 0, 0
while i < len(nums):
if nums[i] == nums[j]:
i += 1
continue
dif = i - j
temp = dif * nums[j]
if nums[j] == prev + 1:
prev_max, cur_max = cur_max, max(temp + prev_max, cur_max)
else:
prev_max, cur_max = cur_max, temp + cur_max
prev = nums[j]
j = i
i += 1
return cur_max | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
Given an array nums of integers, you can perform operations on the array.
In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
You start with 0 points. Return the maximum number of points you can earn by applying such operations.
Example 1:
Input: nums = [3, 4, 2]
Output: 6
Explanation:
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.
Example 2:
Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation:
Delete 3 to earn 3 points, deleting both 2's and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned.
Note:
The length of nums is at most 20000.
Each element nums[i] is an integer in the range [1, 10000]. | class Solution:
def deleteAndEarn(self, nums):
if len(nums) == 0:
return 0
upper = max(nums)
ordered = [0] * (upper + 1)
for i in nums:
ordered[i] += i
ans = [0] * (upper + 1)
ans[1] = ordered[1]
for i in range(2, upper + 1):
ans[i] = max(ans[i - 1], ans[i - 2] + ordered[i])
return ans[upper] | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR |
Given an array nums of integers, you can perform operations on the array.
In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
You start with 0 points. Return the maximum number of points you can earn by applying such operations.
Example 1:
Input: nums = [3, 4, 2]
Output: 6
Explanation:
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.
Example 2:
Input: nums = [2, 2, 3, 3, 3, 4]
Output: 9
Explanation:
Delete 3 to earn 3 points, deleting both 2's and the 4.
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
9 total points are earned.
Note:
The length of nums is at most 20000.
Each element nums[i] is an integer in the range [1, 10000]. | class Solution:
def deleteAndEarn(self, nums):
if nums == []:
return 0
if len(nums) == 1:
return nums[0]
nums.sort()
numsp = nums[0]
choose = [0, nums[0]]
for i in range(1, len(nums)):
numsc = nums[i]
if numsc == numsp:
choose[1] += numsc
continue
elif numsc == numsp + 1:
temp = choose[0]
choose[0] = max(choose)
choose[1] = temp + numsc
numsp = numsc
else:
choose[0] = max(choose)
choose[1] = choose[0] + numsc
numsp = numsc
return max(choose) | CLASS_DEF FUNC_DEF IF VAR LIST RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
l = [int(amount + 1)] * int(amount + 1)
if amount == 0:
return 0
else:
l[0] = 0
coins.sort()
for i in range(1, amount + 1):
for coin in coins:
remainder = i - coin
if remainder < 0:
break
elif l[i] > l[remainder] + 1:
l[i] = l[remainder] + 1
if l[amount] == amount + 1:
return -1
else:
return l[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def back_tracking_solution(self):
def _is_solution(amount_left):
if amount_left == 0:
return True
return False
def _process_solution(num_coins_used, fewest_coins):
if num_coins_used < fewest_coins[0]:
fewest_coins[0] = num_coins_used
def _candidate_coins_start_idx(amount_left, coins, current_coin_start_idx):
for i in range(current_coin_start_idx, len(coins)):
if amount_left >= coins[i]:
return i
return None
def _backtrack(
amount_left,
num_coins_used,
current_fewest_coins,
coins,
current_coin_start_idx,
):
if _is_solution(amount_left):
_process_solution(num_coins_used, current_fewest_coins)
else:
real_coin_start_idx = _candidate_coins_start_idx(
amount_left, coins, current_coin_start_idx
)
if real_coin_start_idx is not None:
for i in range(real_coin_start_idx, len(coins)):
_backtrack(
amount_left - coins[i],
num_coins_used + 1,
current_fewest_coins,
coins,
i,
)
return _backtrack
def dynamic_programming_solution(self):
def _dp_bad(amount, coins):
sorted_coins = sorted(coins)
cache = [([float("inf")] * len(coins)) for i in range(amount + 1)]
for denom_idx in range(len(sorted_coins)):
cache[0][denom_idx] = 0
for amt in range(amount + 1):
cache[amt][0] = (
amt // sorted_coins[0]
if amt % sorted_coins[0] == 0
else float("inf")
)
for i in range(1, len(cache)):
for j in range(1, len(sorted_coins)):
max_num_of_denom = i // sorted_coins[j] + 1
for k in range(max_num_of_denom):
cache[i][j] = min(
cache[i][j], k + cache[i - k * sorted_coins[j]][j - 1]
)
return cache[amount][-1]
def _dp_cache_recursive(amount, coins, cache):
if amount in cache:
return cache[amount]
for c in coins:
if amount >= c:
intermediate = 1 + _dp_cache_recursive(amount - c, coins, cache)
if amount in cache:
cache[amount] = min(cache[amount], intermediate)
else:
cache[amount] = intermediate
return cache[amount] if amount in cache else float("inf")
def _dp_bottom_up_1(amount, coins):
sorted_coins = sorted(coins)
cache = [([float("inf")] * len(coins)) for i in range(amount + 1)]
for denom_idx in range(len(sorted_coins)):
cache[0][denom_idx] = 0
for amt in range(1, len(cache)):
for denom_idx in range(len(sorted_coins)):
amt_left = amt - sorted_coins[denom_idx]
if amt_left >= 0:
cache[amt][denom_idx] = 1 + min(
cache[amt_left][0 : denom_idx + 1]
)
return min(cache[amount])
return _dp_bottom_up_1
def coinChange(self, coins: List[int], amount: int) -> int:
result = self.dynamic_programming_solution()(amount, coins)
return -1 if result == float("inf") else result | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR RETURN NONE FUNC_DEF IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [float("inf") for _ in range(amount + 1)]
if amount == 0:
return 0
for i in range(0, len(dp)):
for c in coins:
if i >= c:
if i % c == 0:
dp[i] = min(int(i / c), dp[i])
elif i - c >= 0 and dp[i - c] != float("inf"):
dp[i] = min(int(1 + dp[i - c]), dp[i])
ret_val = dp[amount]
if ret_val == float("inf"):
return -1
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort(reverse=True)
n, res = len(coins), amount + 1
def dfs(index, target, cnt):
nonlocal res
if cnt + (target + coins[index] - 1) // coins[index] >= res:
return
if target % coins[index] == 0:
res = cnt + target // coins[index]
return
if index == n - 1:
return
for i in range(target // coins[index], -1, -1):
dfs(index + 1, target - coins[index] * i, cnt + i)
dfs(0, amount, 0)
return -1 if res > amount else res | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR RETURN IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN IF VAR BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR VAR NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
if len(coins) == 0:
return 0
coins = sorted(coins, reverse=True)
memo = {}
def coinChangeRec(index, amount):
if amount == 0:
return 0
if amount < 0 or index == len(coins):
return math.inf
if (index, amount) in memo:
return memo[index, amount]
withCoin = coinChangeRec(index, amount - coins[index]) + 1
withoutCoin = coinChangeRec(index + 1, amount)
memo[index, amount] = min(withCoin, withoutCoin)
return min(withCoin, withoutCoin)
minCoins = coinChangeRec(0, amount)
if minCoins == math.inf:
return -1
return minCoins | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange2(self, coins: List[int], amount: int) -> int:
impossible = amount + 1
cnts = [impossible] * impossible
cnts[0] = 0
for coin in coins:
for x in range(coin, impossible):
cnts[x] = min(cnts[x], cnts[x - coin] + 1)
if cnts[amount] >= impossible:
return -1
return cnts[amount]
def coinChange(self, coins: List[int], amount: int) -> int:
impossible = amount + 1
self.cnts = [0] * impossible
def createCoins(total: int):
if total == 0:
return 0
if self.cnts[total] != 0:
return self.cnts[total]
minCnt = impossible
for coin in coins:
if total - coin < 0:
continue
cnt = createCoins(total - coin) + 1
minCnt = min(cnt, minCnt)
self.cnts[total] = minCnt
return minCnt
retV = createCoins(amount)
if retV >= impossible:
return -1
return retV | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
dp = [amount] * (amount + 1)
dp[0] = 0
pos = 1
contain = False
while pos < len(dp):
for coin in coins:
if coin == 1:
contain = True
if pos - coin >= 0:
dp[pos] = min(dp[pos], dp[pos - coin] + 1)
pos += 1
print(dp)
if dp[-1] == amount and not contain:
return -1
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR RETURN NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [None for _ in range(amount + 2)]
dp[amount + 1] = float("inf")
dp[amount] = 0
for i in range(amount - 1, -1, -1):
s = float("inf")
for c in coins:
s = min(s, 1 + dp[min(c + i, amount + 1)])
dp[i] = s
if dp[0] == float("inf"):
return -1
return dp[0] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [(amount + 1) for _ in range(amount + 1)]
dp[0] = 0
for coin in coins:
for a in range(coin, amount + 1):
dp[a] = min(dp[a], dp[a - coin] + 1)
res = dp[amount] if dp[amount] < amount + 1 else -1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort()
dp = [0] * (amount + 1)
for item in range(amount + 1):
if item < coins[0] and item != 0:
dp[item] = -1
elif item == 0:
dp[item] = 0
if amount < coins[0] and amount == 0:
return 0
elif amount < coins[0]:
return -1
for i in range(coins[0], amount + 1):
count = 0
for j in coins:
if i - j < 0:
break
elif dp[i - j] != -1:
count += 1
if count > 0:
dp[i] = math.ceil(i / coins[0])
for j in coins:
if i - j < 0:
break
elif dp[i - j] == -1:
continue
else:
dp[i] = min(dp[i], 1 + dp[i - j])
else:
dp[i] = -1
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def changeCoin(self, coins, amount, change):
if amount < 0:
return -1
if amount == 0:
return 0
if amount in change:
return change[amount]
costs = []
for coin in coins:
cost = self.changeCoin(coins, amount - coin, change)
if amount - coin in change:
change[amount - coin] = min(cost, change[amount - coin])
else:
change[amount - coin] = cost
if cost != -1:
costs.append(cost)
if len(costs) == 0:
return -1
return 1 + min(costs)
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort()
coins = [coin for coin in coins if coin <= amount]
change = {}
return self.changeCoin(coins, amount, change) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = dict()
def recurse(amount, index):
if amount == 0:
return 0
if amount in memo:
return memo[amount]
minCoins = float("inf")
for coin in coins:
if amount - coin >= 0:
response = recurse(amount - coin, 0)
if response != -1:
minCoins = min(minCoins, response + 1)
memo[amount] = minCoins if minCoins != float("inf") else -1
return memo[amount]
return recurse(amount, 0) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
rec = [0] * amount
fr = [0]
new = []
l = 1
while fr or new:
if not fr:
fr = new
new = []
l += 1
cur = fr.pop(0)
for c in coins:
if cur + c == amount:
return l
if cur + c < amount and not rec[cur + c]:
new.append(cur + c)
rec[cur + c] = 1
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR RETURN VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange2(self, coins: List[int], amount: int, count) -> int:
if amount < 0:
return -1
if amount == 0:
return 0
if count[amount - 1] != 0:
return count[amount - 1]
min = 100000000000
for i, c in enumerate(sorted(coins)[::-1]):
ret = self.coinChange2(coins, amount - c, count)
if ret >= 0 and ret < min:
min = 1 + ret
if min == 100000000000:
count[amount - 1] = -1
else:
count[amount - 1] = min
return count[amount - 1]
def coinChange(self, coins: List[int], amount: int) -> int:
if amount < 1:
return 0
return self.coinChange2(coins, amount, [0] * amount) | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP LIST NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0]
length = len(coins)
for i in range(1, amount + 1):
dp += [9999]
for j in range(length):
if i >= coins[j] and dp[int(i - coins[j])] != 9999:
dp[i] = min(dp[i], dp[int(i - coins[j])] + 1)
if dp[amount] == 9999:
return -1
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return amount
dp = [(1000000000.0) for _ in range(amount + 1)]
dp[0] = 0
for i in range(1, amount + 1):
k = 1000000000.0
for c in coins:
if i - c >= 0:
k = min(k, dp[i - c] + 1)
if k != 1000000000.0:
dp[i] = k
return -1 if dp[amount] == 1000000000.0 else dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR NUMBER NUMBER VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
coins.sort(reverse=True)
res = amount // coins[-1] + 1
def comb(cursum, num, index):
nonlocal res
if (amount - cursum) / coins[index] >= res - num:
return
for i in range(index, len(coins)):
newsum = cursum + coins[i]
if newsum == amount:
res = min(num + 1, res)
return
elif newsum < amount:
comb(newsum, num + 1, i)
comb(0, 0, 0)
if res == amount // coins[-1] + 1:
return -1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not coins or amount <= 0:
return 0
f = [float("inf")] * (amount + 1)
f[0] = 0
for c in coins:
for a in range(c, amount + 1):
f[a] = min(f[a], f[a - c] + 1)
return f[amount] if f[amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
fewest_coins = [(0) for j in range(amount + 1)]
for j in range(1, amount + 1):
fewest_coins[j] = float("inf")
for coin_i in range(len(coins)):
if coins[coin_i] == j:
fewest_coins[j] = 1
break
elif coins[coin_i] < j:
if fewest_coins[j - coins[coin_i]] > 0:
fewest_coins[j] = min(
fewest_coins[j], fewest_coins[j - coins[coin_i]] + 1
)
if fewest_coins[j] == float("inf"):
fewest_coins[j] = -1
return fewest_coins[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: [int], amount: int) -> int:
summary = 0
result = [float("inf")] * (amount + 1)
result[0] = 0
while summary <= amount:
if result[summary] != -1:
for i in coins:
if (
summary + i <= amount
and result[summary] + 1 < result[summary + i]
):
result[summary + i] = result[summary] + 1
summary += 1
return -1 if result[amount] == float("inf") else result[amount] | CLASS_DEF FUNC_DEF LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_CALL VAR STRING NUMBER VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
numpath = [0] + [float("inf")] * amount
for coin in coins:
for i in range(coin, amount + 1):
if numpath[i] > 1 + numpath[i - coin]:
numpath[i] = 1 + numpath[i - coin]
return numpath[amount] if numpath[amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
count = {}
for coin in coins:
count[coin] = 1
count[0] = 0
for i in range(1, amount + 1):
ans = amount + 1
if i in list(count.keys()):
continue
for coin in coins:
if i - coin > 0 and count[i - coin] != -1:
ans = min(count[i - coin], ans)
if ans == amount + 1:
count[i] = -1
else:
count[i] = ans + 1
return count[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort()
coins.reverse()
lookup = {}
def find_combos(total_remain, total_coins):
if total_remain in lookup:
if lookup[total_remain] > total_coins:
lookup[total_remain] = total_coins
else:
return
else:
lookup[total_remain] = total_coins
for coin in coins:
if total_remain - coin < 0:
continue
find_combos(total_remain - coin, total_coins + 1)
find_combos(amount, 0)
if 0 in lookup:
return lookup[0]
else:
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF NUMBER VAR RETURN VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
dp = {i: [] for i in range(1, amount + 1)}
coins.sort()
for intermediate_amount in range(1, amount + 1):
for denom in reversed(coins):
if intermediate_amount == denom:
dp[intermediate_amount] = [denom]
break
elif (
denom < intermediate_amount
and dp[intermediate_amount - denom] != []
):
if (
dp[intermediate_amount] == []
or len(dp[intermediate_amount])
> len(dp[intermediate_amount - denom]) + 1
):
dp[intermediate_amount] = dp[intermediate_amount - denom] + [
denom
]
return len(dp[amount]) if len(dp[amount]) > 0 else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR IF VAR VAR VAR BIN_OP VAR VAR LIST IF VAR VAR LIST FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR LIST VAR RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [float("inf")] * (amount + 1)
dp[0] = 0
for i in range(min(coins), amount + 1):
tmp = list()
for coin in coins:
if i - coin < 0:
tmp.append(float("inf"))
else:
tmp.append(dp[i - coin])
dp[i] = min(tmp) + 1
return dp[-1] if dp[-1] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
x = coinCount(coins, amount, {})
if x == float("inf"):
return -1
return x
def coinCount(coins, amount, found):
if amount < 0:
return float("inf")
if amount == 0:
return 0
if found.get(amount):
return found[amount]
count = 0
minCount = float("inf")
for coin in coins:
if coin == amount:
return 1
count = 1 + coinCount(coins, amount - coin, found)
if count < minCount:
minCount = count
found[amount] = minCount
return minCount | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR DICT IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if not amount:
return 0
dp: List[int] = [(0) for _ in range(amount)]
for i in range(amount):
for coin in coins:
if coin > i + 1:
continue
if coin == i + 1:
dp[i] = 1
elif dp[i - coin] != 0:
if dp[i] == 0:
dp[i] = dp[i - coin] + 1
else:
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[amount - 1] if dp[amount - 1] != 0 else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort()
dp = [None for _ in range(amount + 1)]
ret = self.helper(coins, amount, dp)
return ret if ret != float("inf") else -1
def helper(self, coins: List[int], amount: int, dp) -> int:
if amount == 0:
return 0
if amount < coins[0]:
return float("inf")
ans = float("inf")
for coin in coins:
if amount >= coin:
if amount == coin:
ans = 0
else:
tmp = (
self.helper(coins, amount - coin, dp)
if dp[amount - coin] == None
else dp[amount - coin]
)
if dp[amount - coin] == None:
dp[amount - coin] = tmp
ans = min(ans, tmp)
return ans + 1 if ans != -1 else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NONE FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
@lru_cache(None)
def dfs(amt, idx):
if idx < 0:
if amt == 0:
return 0
else:
return float("inf")
if amt < 0:
return float("inf")
if amt == coins[idx]:
return 1
return min(dfs(amt - coins[idx], idx) + 1, dfs(amt, idx - 1))
res = dfs(amount, len(coins) - 1)
return res if res < float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR STRING IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
mem = [-1] * (amount + 1)
mem[0] = 0
for i in range(1, amount + 1):
m = math.inf
for c in coins:
if i - c >= 0:
if mem[i - c] != -1 and mem[i - c] + 1 < m:
m = mem[i - c] + 1
if m != math.inf:
mem[i] = m
print(mem)
return mem[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
cache = [0] * (amount + 1)
for i in range(1, amount + 1):
min_count = float("inf")
for coin in coins:
if i - coin >= 0:
current_min_coin = cache[i - coin] + 1
if min_count > current_min_coin:
min_count = current_min_coin
cache[i] = min_count
if cache[amount] in [0, float("inf")]:
return -1
return cache[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR LIST NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], total: int) -> int:
dic = {(0): 0}
def helper(amount):
if amount in coins:
dic[amount] = 1
elif amount < 0:
return float("inf")
elif amount not in dic:
dic[amount] = min([(helper(amount - c) + 1) for c in coins])
return dic[amount]
return helper(total) if helper(total) != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
MAX = float("inf")
dp = [0] + [MAX] * amount
for i in range(1, amount + 1):
for coin in coins:
if i - coin >= 0:
dp[i] = min(dp[i], dp[i - coin] + 1)
if dp[-1] == MAX:
return -1
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort()
n = len(coins)
d = [([-1] * (amount + 1)) for i in range(n + 1)]
for i in range(amount + 1):
if i % coins[0] == 0:
d[1][i] = i // coins[0]
for i in range(n + 1):
d[i][0] = 0
for i in range(2, n + 1):
for j in range(1, amount + 1):
if j - coins[i - 1] >= 0:
if d[i - 1][j] != -1 and d[i][j - coins[i - 1]] != -1:
d[i][j] = min(d[i - 1][j], d[i][j - coins[i - 1]] + 1)
elif d[i - 1][j] == -1 and d[i][j - coins[i - 1]] != -1:
d[i][j] = d[i][j - coins[i - 1]] + 1
elif d[i - 1][j] != -1 and d[i][j - coins[i - 1]] == -1:
d[i][j] = d[i - 1][j]
else:
d[i][j] = d[i - 1][j]
else:
d[i][j] = d[i - 1][j]
return d[n][amount] | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [None for _ in range(amount + 1)]
def fewest_num(amount, dp):
if dp[amount] is not None:
return dp[amount]
if amount == 0:
min_num = 0
elif amount < min(coins):
min_num = float("inf")
elif amount in set(coins):
min_num = 1
else:
min_num = float("inf")
for coin in coins:
if amount - coin >= 0:
num = fewest_num(amount - coin, dp)
min_num = min(min_num, num)
min_num += 1
dp[amount] = min_num
return min_num
min_num = fewest_num(amount, dp)
if min_num == float("inf"):
return -1
return min_num | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR NONE RETURN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
level = seen = {0}
number = 0
while level:
if amount in seen:
return number
level = {
(pre_amount + coin)
for pre_amount in level
for coin in coins
if pre_amount + coin <= amount
}
seen.update(level)
number += 1
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def helper(amount, minAmounts):
if amount == 0:
return 0
elif amount < 0:
return math.inf
if amount in minAmounts:
return minAmounts[amount]
minAmount = math.inf
for coin in coins:
minAmount = min(minAmount, helper(amount - coin, minAmounts) + 1)
minAmounts[amount] = minAmount
return minAmount
minAmounts = dict()
x = helper(amount, minAmounts)
if x == math.inf:
return -1
return x | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
result = []
coins.sort(reverse=True)
max_sum = 2**31 - 1
def find_combinations(combination, remain, start):
nonlocal max_sum
if remain == 0:
max_sum = min(max_sum, len(combination))
return
elif remain < 0:
return
for i in range(start, len(coins)):
allowed_coins = max_sum - len(combination)
max_value_can_be_achieved = coins[i] * allowed_coins
if coins[i] <= remain < max_value_can_be_achieved:
combination.append(coins[i])
find_combinations(combination, remain - coins[i], i)
combination.pop()
find_combinations([], amount, 0)
if max_sum == 2**31 - 1:
return -1
return max_sum | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN IF VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
cal = [float("inf") for i in range(amount + 1)]
for i in range(0, amount + 1):
for j in range(len(coins)):
if i == 0:
cal[i] = 0
elif coins[j] <= i:
cal[i] = min(1 + cal[i - coins[j]], cal[i])
print(cal)
if cal[amount] == float("inf"):
return -1
else:
return cal[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
table = [float("inf") for _ in range(amount + 1)]
i = 0
table[0] = 0
for coin in coins:
if coin <= amount:
table[coin] = 1
while i <= amount:
for coin in coins:
if i - coin >= 0:
table[i] = min(table[i - coin] + 1, table[i])
i += 1
return table[amount] if table[amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] + [-1] * amount
for x in range(amount + 1):
if dp[x] < 0:
continue
for c in coins:
if x + c > amount:
continue
if dp[x + c] < 0 or dp[x + c] > dp[x] + 1:
dp[x + c] = dp[x] + 1
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def dfs(remaining):
nonlocal memo
if remaining == 0:
return 0
if remaining < 0:
return -1
if memo[remaining]:
return memo[remaining]
res = amount + 1
for coin in coins:
count = dfs(remaining - coin)
if count == -1:
continue
res = min(res, 1 + count)
memo[remaining] = res if res != amount + 1 else -1
return memo[remaining]
memo = [None for _ in range(amount + 1)]
return dfs(amount) | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
queue = [[amount, 0]]
visited = {amount}
for q in queue:
for c in coins:
if q[0] - c in visited:
continue
if q[0] == c:
return q[1] + 1
if q[0] > c:
visited.add(q[0] - c)
queue.append([q[0] - c, q[1] + 1])
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR RETURN BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [([float("inf")] * (amount + 1)) for _ in range(len(coins))]
for r in range(len(coins)):
dp[r][0] = 0
for r in range(len(coins)):
for c in range(1, amount + 1):
take = leave = float("inf")
if c - coins[r] >= 0:
take = dp[r][c - coins[r]] + 1
if r > 0:
leave = dp[r - 1][c]
dp[r][c] = min(leave, take)
return dp[-1][-1] if dp[-1][-1] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
dp = {}
for c in coins:
dp[c] = 1
for i in range(amount + 1):
for c in coins:
if dp.get(i - c):
if not dp.get(i):
dp[i] = dp[i - c] + 1
else:
dp[i] = min(dp[i], dp[i - c] + 1)
return dp.get(amount, -1) | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
n = len(coins)
t = [([float("inf") - 1] * (amount + 1)) for _ in range(n + 1)]
for i in range(n + 1):
t[i][0] = 0
for j in range(amount + 1):
t[0][j] = float("inf") - 1
if j % coins[0] == 0:
t[1][j] = j // coins[0]
else:
t[1][j] = float("inf") - 1
for i in range(1, n + 1):
for j in range(1, amount + 1):
if coins[i - 1] <= j:
t[i][j] = min(1 + t[i][j - coins[i - 1]], t[i - 1][j])
else:
t[i][j] = t[i - 1][j]
return t[n][amount] if t[n][amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP FUNC_CALL VAR STRING NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR STRING NUMBER IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_CALL VAR STRING VAR VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [[math.inf for _ in range(amount + 1)] for _ in coins]
for i in range(len(coins)):
dp[i][0] = 0
for i in range(1, amount + 1):
if coins[0] <= i:
dp[0][i] = 1 + dp[0][i - coins[0]]
for i in range(1, len(coins)):
for j in range(1, amount + 1):
take, leave = math.inf, math.inf
if coins[i] <= j:
take = 1 + dp[i][j - coins[i]]
leave = dp[i - 1][j]
dp[i][j] = min(take, leave)
return dp[-1][-1] if dp[-1][-1] != math.inf else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [(-1) for i in range(amount + 1)]
dp[0] = 0
for coin in coins:
for i in range(coin, amount + 1):
if i == coin:
dp[i] = 1
elif dp[i] == -1 and dp[i - coin] != -1:
dp[i] = dp[i - coin] + 1
elif dp[i] != -1 and dp[i - coin] != -1:
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
dp = [float("inf")] * (amount + 1)
calculated = [False] * (amount + 1)
dp[0] = 0
for coin in coins:
if coin <= amount:
dp[coin] = 1
calculated[coin] = True
for i in range(1, amount + 1):
if not calculated[i]:
candidates = [dp[i - coin] for coin in coins if i >= coin]
if candidates:
dp[i] = min(candidates) + 1
calculated[i] = True
if dp[-1] == float("inf"):
return -1
else:
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
N = len(coins)
mem = [[None for i in range(amount + 1)] for j in range(N + 1)]
def helper(target, i):
if mem[i][target] != None:
return mem[i][target]
if target == 0:
return 0
if i == N and target > 0:
return float("inf")
if coins[i] > target:
mem[i][target] = helper(target, i + 1)
else:
include = 1 + helper(target - coins[i], i)
exclude = helper(target, i + 1)
mem[i][target] = min(include, exclude)
return mem[i][target]
ans = helper(amount, 0)
return ans if ans != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins, amount):
coins.sort()
stack = [(0, 0, len(coins))]
min_steps = 2**31
while len(stack) != 0:
steps, accumulated, sequence = stack.pop()
if accumulated == amount:
min_steps = min(min_steps, steps)
if accumulated > amount or amount - accumulated > coins[sequence - 1] * (
min_steps - steps
):
continue
for seq, coin in enumerate(coins[:sequence]):
stack.append((steps + 1, accumulated + coin, seq + 1))
return min_steps if min_steps != 2**31 else -1 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR BIN_OP NUMBER NUMBER VAR NUMBER |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = [float("inf") for _ in range(amount + 1)]
memo[0] = 0
for c in coins:
for i in range(c, len(memo)):
memo[i] = min(1 + memo[i - c], memo[i])
return memo[amount] if memo[amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] * (amount + 1)
return self.painful(coins, amount, dp)
def painful(self, coins: List[int], amount: int, dp: List[int]) -> int:
if amount == 0:
return 0
if amount < 0:
return -1
if dp[amount]:
return dp[amount]
minCost = math.inf
for coin in coins:
res = self.painful(coins, amount - coin, dp)
if res != -1:
minCost = min(minCost, 1 + res)
dp[amount] = minCost if minCost < math.inf else -1
return minCost if minCost < math.inf else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
cache = {}
coins = sorted(coins, reverse=True)
def helper(coins, amount, cache):
if amount == 0:
return 0
elif amount in cache:
return cache[amount]
cache[amount] = float("inf")
for c in coins:
if amount - c >= 0:
cache[amount] = min(
cache[amount], helper(coins, amount - c, cache) + 1
)
return cache[amount]
if amount == 0:
return 0
if min(coins) > amount:
return -1
ans = helper(coins, amount, {})
return ans if ans != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR DICT RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def __init__(self):
self.min_coins = float("inf")
def coinChange(self, coins: List[int], amount: int) -> int:
def getChange(num_coins, need, start):
divided_coin = need // coins[start]
if num_coins + divided_coin >= self.min_coins:
return
if need % coins[start] == 0:
self.min_coins = min(self.min_coins, divided_coin + num_coins)
return
if start == len(coins) - 1:
return
for num_used in range(divided_coin, -1, -1):
new_need = need - coins[start] * num_used
getChange(num_coins + num_used, new_need, start + 1)
coins = sorted(coins, reverse=True)
getChange(0, amount, 0)
return self.min_coins if self.min_coins < float("inf") else -1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
n = len(coins)
dp = [0] + [(amount + 1) for i in range(amount)]
for i in range(1, amount + 1):
for c in coins:
if i >= c:
dp[i] = min(dp[i - c] + 1, dp[i])
return dp[amount] if dp[amount] < amount + 1 else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins, amount):
dp = [amount + 1] * (amount + 1)
dp[0] = 0
for i in range(1, amount + 1):
for j in range(0, len(coins)):
if coins[j] <= i:
dp[i] = min(dp[i], dp[i - coins[j]] + 1)
return -1 if dp[amount] > amount else dp[amount] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR VAR NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
self.smallest = float("inf")
coins.sort(reverse=True)
self.dfs(coins, amount, 0)
return -1 if type(self.smallest) is float else self.smallest
def dfs(self, coins, amount, prev_count):
if len(coins) == 0:
return
if amount % coins[0] == 0:
self.smallest = min(self.smallest, prev_count + amount // coins[0])
else:
for k in range(amount // coins[0], -1, -1):
if prev_count + k >= self.smallest:
break
self.dfs(coins[1:], amount - k * coins[0], prev_count + k) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] + [-1] * amount
for coin in coins:
for idx in range(coin, len(dp)):
if dp[idx - coin] != -1:
if dp[idx] != -1:
dp[idx] = min(dp[idx - coin] + 1, dp[idx])
else:
dp[idx] = dp[idx - coin] + 1
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def __init__(self):
self.total_min = 0
self.change_map = {(0): 0}
def changer(self, coins_obj, amount_obj):
if amount_obj == 0:
self.change_map[amount_obj] = 0
return 0
if min(coins_obj) > amount_obj:
self.change_map[amount_obj] = -1
return -1
rev_coins_obj = list(reversed(coins_obj))
for el in rev_coins_obj:
if el <= amount_obj:
if amount_obj - el in self.change_map.keys():
tmp = self.change_map[amount_obj - el]
else:
tmp = Solution.changer(self, coins_obj, amount_obj - el)
if tmp != -1:
key = tmp + 1
if amount_obj not in self.change_map.keys():
self.change_map[amount_obj] = key
elif amount_obj in self.change_map.keys():
if key < self.change_map[amount_obj]:
self.change_map[amount_obj] = key
if amount_obj not in self.change_map.keys():
self.change_map[amount_obj] = -1
return -1
elif amount_obj in self.change_map.keys():
return self.change_map[amount_obj]
def coinChange(self, coins: List[int], amount: int) -> int:
Solution.changer(self, coins, amount)
if amount not in self.change_map.keys():
return -1
return self.change_map[amount] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
t = [[(0) for x in range(amount + 1)] for x in range(len(coins) + 1)]
for j in range(amount + 1):
t[0][j] = 1000
for i in range(1, len(coins) + 1):
t[i][0] = 0
for i in range(1, len(coins) + 1):
for j in range(1, amount + 1):
if j % coins[i - 1] == 0:
t[i][j] = j / coins[i - 1]
else:
t[i][j] = 1000
for i in range(1, len(coins) + 1):
for j in range(1, amount + 1):
if coins[i - 1] <= j:
t[i][j] = min(t[i][j - coins[i - 1]] + 1, t[i - 1][j])
else:
t[i][j] = t[i - 1][j]
if t[len(coins)][amount] != 1000:
return t[len(coins)][amount]
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [[(0) for i in range(amount + 1)] for i in range(len(coins))]
col = amount + 1
for i in range(len(coins)):
for j in range(1, col):
if i == 0:
r = i
c = j - coins[i]
if c < 0 or dp[r][c] == -1:
dp[i][j] = -1
else:
dp[i][j] = dp[r][c] + 1
else:
incIndex = j - coins[i]
if incIndex < 0 or dp[i][incIndex] == -1:
dp[i][j] = dp[i - 1][j]
else:
dp[i][j] = dp[i][incIndex] + 1
if dp[i - 1][j] != -1:
dp[i][j] = min(dp[i - 1][j], dp[i][incIndex] + 1)
return dp[len(coins) - 1][col - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | import sys
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
s = list()
import sys
for i in range(0, amount + 1):
s.append(sys.maxsize)
s[0] = 0
for i in range(1, amount + 1):
m = sys.maxsize
for j in range(0, len(coins)):
if i - coins[j] >= 0:
m = min(m, s[i - coins[j]])
if m == sys.maxsize:
s[i] = m
else:
s[i] = m + 1
if s[amount] == sys.maxsize:
s[amount] = -1
return s[amount] | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR IMPORT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = {}
def backtrack(tot):
if tot == 0:
return 0
if tot not in memo:
min_coins = float("inf")
for coin in coins:
if coin <= tot:
cur_count = backtrack(tot - coin) + 1
min_coins = min(min_coins, cur_count)
memo[tot] = min_coins
return memo[tot]
out = backtrack(amount)
return -1 if out == float("inf") else out | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [float("inf")] * (amount + 1)
dp[0] = 0
for i in range(1, amount + 1):
tmp = float("inf")
for c in coins:
if i - c >= 0:
if tmp > dp[i - c] + 1:
tmp = dp[i - c] + 1
dp[i] = tmp
return dp[amount] if dp[amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [None for i in range(amount + 1)]
dp[0] = 0
for i in range(1, amount + 1):
result = None
for coin in coins:
if i - coin >= 0:
tmp = dp[i - coin]
if tmp is not None and (result is None or tmp + 1 < result):
result = tmp + 1
dp[i] = result
if dp[-1] is None:
return -1
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NONE VAR NONE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER NONE RETURN NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort(reverse=True)
self.ret = float("inf")
def recurrent(index, coins, amount, current):
first = coins[index]
n = amount // first
remainder = amount % first
if remainder == 0:
self.ret = min(self.ret, current + n)
else:
l = len(coins)
while remainder <= amount and index < l - 1 and current + n < self.ret:
recurrent(index + 1, coins, remainder, current + n)
n -= 1
remainder += first
return
recurrent(0, coins, amount, 0)
if self.ret == float("inf"):
return -1
else:
return self.ret | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR RETURN EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] * (amount + 1)
for i in range(1, amount + 1):
minn = float("inf")
for j in coins:
if i == j:
minn = min(minn, 1)
continue
if i - j > 0:
minn = min(minn, 1 + dp[i - j])
dp[i] = minn
return dp[amount] if dp[amount] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins = sorted(coins)
dp = [(amount + 1) for _ in range(amount + 1)]
dp[0] = 0
for i in range(1, amount + 1):
for coin in coins:
if coin > i:
break
dp[i] = min(dp[i], dp[i - coin] + 1)
res = dp[amount] if dp[amount] < amount + 1 else -1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
if not coins:
return -1
memo = {}
stack = [amount]
l = 0
while stack:
tmp = []
for remain in stack:
if remain < 0:
continue
for coin in coins:
nxt = remain - coin
if nxt == 0:
return l + 1
if nxt in memo:
continue
else:
memo[nxt] = 1
tmp += [nxt]
stack = tmp
l += 1
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR LIST VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
IMPOSSIBLE = -1
def computeMinCoins(self, n: int) -> int:
if (result := self.memoization.get(n)) is not None:
return result
if n == 0:
result = 0
else:
result = Solution.IMPOSSIBLE
for coin in self.coins:
if coin > n:
continue
if (
needed_extra := self.computeMinCoins(n - coin)
) != Solution.IMPOSSIBLE:
possible_result = 1 + needed_extra
if result == Solution.IMPOSSIBLE or result > possible_result:
result = possible_result
self.memoization[n] = result
return result
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort(reverse=True)
if amount % coins[0] == 0:
return amount // coins[0]
self.coins = coins
self.memoization = {}
return self.computeMinCoins(amount) | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF VAR IF VAR FUNC_CALL VAR VAR NONE RETURN VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], a: int) -> int:
l = len(coins)
mem = []
for i in range(l + 1):
mem.append([0] * (a + 1))
for j in range(a + 1):
if i == 0:
mem[i][j] = float("inf")
if j == 0:
mem[i][j] = 0
if i > 0 and j > 0:
if coins[i - 1] <= j:
mem[i][j] = min(1 + mem[i][j - coins[i - 1]], mem[i - 1][j])
else:
mem[i][j] = mem[i - 1][j]
if mem[l][a] == float("inf"):
return -1
return mem[l][a] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
arr = [float("inf")] * (amount + 1)
arr[0] = 0
for i in range(1, len(arr)):
min_choices = []
for x in coins:
if i - x >= 0:
min_choices.append(1 + arr[i - x])
if min_choices:
arr[i] = min(min_choices)
if arr[amount] == float("inf"):
return -1
else:
return arr[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def helper(coins, amount, dp):
if amount < 0:
return float("inf")
if amount == 0:
return 0
if amount in dp:
return dp[amount]
ans = []
for i in range(len(coins)):
use_ci = 1 + helper(coins, amount - coins[i], dp)
ans.append(use_ci)
dp[amount] = min(ans)
return dp[amount]
if amount <= 0:
return 0
dp = {}
result = helper(coins, amount, dp)
return -1 if result == float("inf") else result | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR STRING IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount < 0 or not coins:
return -1
if amount == 0:
return 0
min_com = [(0) for i in range(amount + 1)]
for i in range(1, amount + 1):
for c in coins:
if i >= c and (min_com[i - c] > 0 or i - c == 0):
if min_com[i] == 0:
min_com[i] = min_com[i - c] + 1
else:
min_com[i] = min(min_com[i], min_com[i - c] + 1)
if min_com[amount] == 0:
return -1
return min_com[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
T = len(coins)
dp = [(0) for i in range(amount + 1)]
for n in range(1, amount + 1):
dp[n] = math.inf
for i in range(T):
if n - coins[i] >= 0:
subProbSol = dp[n - coins[i]]
dp[n] = min(dp[n], subProbSol + 1)
if dp[amount] == math.inf:
return -1
else:
return dp[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
coins.sort(reverse=True)
result = -1
def helper(coins: List[int], pos: int, left: int, current: int):
if left % coins[pos] == 0:
nonlocal result
if result == -1 or result > current + left // coins[pos]:
result = current + left // coins[pos]
return
if pos == len(coins) - 1:
return
for k in range(left // coins[pos], -1, -1):
new_amount = left - coins[pos] * k
new_count = current + k
if (
result != -1
and result
< new_count + (new_amount + coins[pos + 1] - 1) / coins[pos + 1]
):
break
helper(coins, pos + 1, left - coins[pos] * k, current + k)
helper(coins, 0, amount, 0)
return result | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
mem = {}
max_amount = 10**4 + 1
def cc(amount):
mem[amount] = max_amount
for coin in coins:
dif = amount - coin
if dif == 0:
mem[amount] = 1
elif dif > 0:
if dif not in mem:
cc(dif)
mem[amount] = min(mem[amount], mem[dif] + 1)
cc(amount)
if mem[amount] == max_amount:
return -1
else:
return mem[amount] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
memo = {}
def solve(cur):
if cur in memo:
return memo[cur]
if cur == 0:
return 0
arr = [float("inf")] * len(coins)
for i, coin in enumerate(coins):
if cur - coin < 0:
arr[i] = float("inf")
else:
arr[i] = solve(cur - coin) + 1
ret = min(arr)
memo[cur] = ret
return ret
ans = solve(amount)
if ans == float("inf"):
return -1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
que = deque()
seen = set()
res = -1
for coin in coins:
if coin <= amount:
que.append(coin)
seen.add(coin)
count = 1
while que:
prev = len(que)
while prev > 0:
cur = que.popleft()
if cur == amount:
return count
for coin in coins:
newamount = cur + coin
if newamount not in seen and newamount <= amount:
que.append(newamount)
seen.add(newamount)
prev -= 1
count += 1
return res | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR RETURN VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coin: List[int], amount: int) -> int:
dp = [[(9999999) for _ in range(amount + 1)] for _ in range(len(coin) + 1)]
for i in range(1, len(coin) + 1):
dp[i][0] = 0
for i in range(1, len(coin) + 1):
for j in range(1, amount + 1):
if j >= coin[i - 1]:
dp[i][j] = min(dp[i][j - coin[i - 1]] + 1, dp[i - 1][j])
else:
dp[i][j] = dp[i - 1][j]
if dp[-1][-1] == 9999999:
return -1
return dp[-1][-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER NUMBER RETURN NUMBER RETURN VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def helper(remAmt, coins):
nonlocal mem
if remAmt in mem:
return mem[remAmt]
minCoins = float("inf")
for coin in coins:
if coin <= remAmt:
result = 1 + helper(remAmt - coin, coins)
minCoins = min(minCoins, result)
else:
break
mem[remAmt] = minCoins
return minCoins
if amount < 1:
return 0
mem = {(0): 0}
coins.sort()
minCoins = helper(amount, coins)
if minCoins == float("inf"):
return -1
return minCoins | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins, amount):
coins.sort(reverse=True)
queue = [(amount, 0)]
visited = [(False) for i in range(amount + 1)]
for val, cnt in queue:
if val == 0:
return cnt
for coin in coins:
if val - coin >= 0 and not visited[val - coin]:
visited[val - coin] = True
queue.append((val - coin, cnt + 1))
return -1 | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER RETURN VAR FOR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN NUMBER |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [math.inf] * (amount + 1)
dp[0] = 0
for coin in coins:
for idx in range(coin, amount + 1):
prev_coins_ct = dp[idx - coin]
dp[idx] = min(prev_coins_ct + 1, dp[idx])
if dp[-1] == math.inf:
return -1
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR RETURN NUMBER RETURN VAR NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
start = [0]
visited = [False] * (amount + 1)
visited[0] = True
numCoins = 1
nextStart = []
while start:
for v in start:
for coin in coins:
nextVal = v + coin
if nextVal > amount or visited[nextVal]:
continue
elif nextVal == amount:
return numCoins
else:
visited[nextVal] = True
nextStart.append(nextVal)
start, nextStart = nextStart, []
numCoins += 1
return -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR NUMBER RETURN NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
if amount == 0:
return 0
coins.sort()
cnt = 0
toCheck = {amount}
while toCheck:
cnt += 1
tmp = set()
for x in toCheck:
for y in coins:
if y == x:
return cnt
if y > x:
break
tmp.add(x - y)
if not tmp:
return -1
toCheck = tmp
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0] + [float("inf")] * amount
for i in range(1, amount + 1):
dp[i] = min(dp[i - c] if i - c >= 0 else float("inf") for c in coins) + 1
return dp[-1] if dp[-1] != float("inf") else -1 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER RETURN VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
N = len(coins)
M = amount
table = [-1] * (M + 1)
for j in range(N + 1):
table[0] = 0
for m in range(1, M + 1):
for i in range(N):
c = coins[i]
if c > m:
pass
elif table[m - c] != -1:
if table[m] != -1:
table[m] = min(table[m - c] + 1, table[m])
else:
table[m] = table[m - c] + 1
return table[M] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR |
You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Example 3:
Input: coins = [1], amount = 0
Output: 0
Example 4:
Input: coins = [1], amount = 1
Output: 1
Example 5:
Input: coins = [1], amount = 2
Output: 2
Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 231 - 1
0 <= amount <= 104 | class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
mem = {}
mem[0] = 0
def rec(amount):
if amount < 0:
return -1
if amount in mem:
return mem[amount]
m = math.inf
for c in coins:
n = rec(amount - c)
if n != -1 and n + 1 < m:
m = n + 1
if m == math.inf:
m = -1
mem[amount] = m
return m
if amount == 0:
return 0
return rec(amount) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.