M.Naveen VU21CSEN0100259: 638. Shopping Offers
M.Naveen VU21CSEN0100259: 638. Shopping Offers
M.NAVEEN
VU21CSEN0100259
638. Shopping Offers
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 jth 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:
Output: 14
Explanation: There are two kinds of items, A and B. Their prices are $2 and $5 respectively.
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.
Example 2:
Output: 11
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C.
You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4
for 1C.
You cannot add more items, though only $9 for 2A ,2B and 1C.
Constraints:
n == price.length == needs.length
1 <= n <= 6
special[i].length == n + 1
The input is generated that at least one of special[i][j] is non-zero for 0 <= j <= n - 1.
CODE:
class Solution {
int res;
res = Integer.MAX_VALUE;
return res;
private void backtrack(List<Integer> price, List<List<Integer>> special, List<Integer> needs, int min,
int pos) {
int totalPrice = 0;
valid = false;
BST
M.NAVEEN
VU21CSEN0100259
break;
if (valid) {
You are given the root of a binary search tree (BST), where the values of exactly two nodes of the
tree were swapped by mistake. Recover the tree without changing its structure.
Example 1:
BST
M.NAVEEN
VU21CSEN0100259
Output: [3,1,null,null,2]
Explanation: 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid.
Example 2:
Output: [2,1,4,null,null,3]
Explanation: 2 cannot be in the right subtree of 3 because 2 < 3. Swapping 2 and 3 makes the BST
valid.
BST
M.NAVEEN
VU21CSEN0100259
Constraints:
CODE:
class Solution {
TreeNode prev=null,first=null,second=null;
if(root==null)
return ;
inorder(root.left);
if(prev!=null&&root.val<prev.val){
if(first==null)
first=prev;
second=root;
prev=root;
inorder(root.right);
if(root==null)
return ;
inorder(root);
int temp=first.val;
first.val=second.val;
second.val=temp;
}
BST
M.NAVEEN
VU21CSEN0100259
Given an integer n, return all the structurally unique BST's (binary search trees), which has
exactly n nodes of unique values from 1 to n. Return the answer in any order.
Example 1:
Input: n = 3
Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
Example 2:
Input: n = 1
Output: [[1]]
Constraints:
1 <= n <= 8
CODE:
class Solution {
if (n == 0) {
if (memo.containsKey(key)) {
return memo.get(key);
trees.add(null);
return trees;
root.left = leftTree;
root.right = rightTree;
trees.add(root);
}
BST
M.NAVEEN
VU21CSEN0100259
memo.put(key, trees);
return trees;