Ass 9 Advcod
Ass 9 Advcod
ASSIGNMENT - 9
Q. LeetCode Store
In LeetCode Store, there are n items to sell. Each item has a price.
However, there are some special offers, and a special offer consists of
one or more different kinds of items with a sale price.
You are given an integer array price where price[i] is the price of
the ith item, and an integer array needs where needs[i] is the number
of pieces of the ith item you want to buy.
You are also given an array special where special[i] is of size n +
1 where special[i][j] is the number of pieces of the j th item in
the ith offer and special[i][n] (i.e., the last integer in the array) is the
price of the ith offer.
Return the lowest price you have to pay for exactly certain items as
given, where you could make optimal use of the special offers. You
are not allowed to buy more items than you want, even if that would
lower the overall price. You could use any of the special offers as
many times as you want.
Example 1:
Input: price = [2,5], special = [[3,0,5],[1,2,10]], needs = [3,2]
Output: 14
Explanation: There are two kinds of items, A and B. Their prices are
$2 and $5 respectively.
In special offer 1, you can pay $5 for 3A and 0B
In special offer 2, you can pay $10 for 1A and 2B.
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B
(special offer #2), and $4 for 2A.
Constraints:
n == price.length == needs.length
1 <= n <= 6
0 <= price[i], needs[i] <= 10
1 <= special.length <= 100
special[i].length == n + 1
0 <= special[i][j] <= 50
The input is generated that at least one of special[i][j] is non-zero
for 0 <= j <= n - 1.
Code:
import java.util.*;
if (validOffer) {
minCost = Math.min(minCost, offer.get(needs.size()) +
dfs(price, special, newNeeds, memo));
}
}
memo.put(needs, minCost);
return minCost;
}
Example 1:
import java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) {
this.val = val;
}
}
inOrder(root.left);
inOrder(root.right);
}
// Recover BST
bst.recoverTree(root);
output
Q. Ambiguous Dominoes
Polycarp and Monocarp are both solving the same puzzle with
dominoes. They are given the same set of nn dominoes, the ii-th of
which contains two numbers xixi and yiyi. They are also both given
the same mm by kk grid of values aijaij such that m⋅k=2nm⋅k=2n.
The puzzle asks them to place the nn dominoes on the grid in such a
way that none of them overlap, and the values on each domino match
the aijaij values that domino covers. Dominoes can be rotated
arbitrarily before being placed on the grid, so the
domino (xi,yi)(xi,yi) is equivalent to the domino (yi,xi)(yi,xi).
They have both solved the puzzle, and compared their answers, but
noticed that not only did their solutions not match, but none of
the nn dominoes were in the same location in both solutions!
Formally, if two squares were covered by the same domino in
Polycarp's solution, they were covered by different dominoes in
Monocarp's solution. The diagram below shows one potential aa grid,
along with the two players' solutions.
Code:
import java.util.*;