2 051
Підписники
Немає даних24 години
Немає даних7 днів
Немає даних30 днів
Архів дописів
Hi Rani , Welcome to this Community for 2022 Batch Daily Off Campus Jobs Updates join this channel ✅
👇👇
@offcampusjobs_0
@offcampusjobs_0
To solve this problem, we can use the concept of dynamic programming.
First, we create a 2D array dp of size (N+1) x (M+1), where dp[i][j] represents the minimum number of toys that can be bought using the first i toys and j rupees.
We initialize dp[0][j] = infinity for all j, except dp[0][0] = 0, since we don't have any toys and can't spend any money.
Then, for each toy i from 1 to N, we iterate through all possible amounts of money j from 0 to M, and update dp[i][j] as follows:
- If the price of the ith toy is greater than j, we can't buy this toy. So, dp[i][j] = dp[i-1][j].
- Otherwise, we have two options:
1. Buy the ith toy without applying magic: dp[i][j] = dp[i-1][j-price[i]] + 1.
2. Apply magic on the ith toy and buy it: dp[i][j] = dp[i-1][j-magical_price[i]] + 1.
Finally, if dp[N][M] is infinity, it means it is not possible to buy all the toys within the given budget even after applying magic. In this case, we return -1. Otherwise, we return dp[N][M] as the minimum number of toys on which magic should be applied.
Here is the implementation in Python:
def minimumToys(N, M, price, magical_price):
INF = float('inf')
dp = [[INF] * (M+1) for _ in range(N+1)]
dp[0][0] = 0
for i in range(1, N+1):
for j in range(M+1):
if price[i-1] > j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = min(dp[i-1][j], dp[i-1][j-price[i-1]] + 1, dp[i-1][j-magical_price[i-1]] + 1)
if dp[N][M] == INF:
return -1
else:
return dp[N][M]
# Example usage
N = 5
M = 13
price = [3, 4, 6, 2, 4]
magical_price = [1, 2, 5, 1, 3]
print(minimumToys(N, M, price, magical_price))
𝑪𝒓𝒚𝒑𝒕𝒐𝒄𝒖𝒓𝒓𝒆𝒏𝒄𝒚 𝒊𝒏𝒗𝒆𝒔𝒕𝒎𝒆𝒏𝒕 𝒊𝒔 𝒕𝒉𝒆 𝒃𝒆𝒔𝒕 𝒚𝒐𝒖 𝒄𝒐𝒖𝒍𝒅 𝒆𝒗𝒆𝒓 𝒕𝒉𝒊𝒏𝒌 𝒐𝒇 𝒊 𝒋𝒖𝒔𝒕 𝒈𝒐𝒕 𝒃𝒂𝒄𝒌 𝒂 𝒑𝒓𝒐𝒇𝒊𝒕 𝒐𝒇 €55,000 𝒕𝒉𝒓𝒐𝒖𝒈𝒉 𝒇𝒐𝒓𝒆𝒙 𝒂𝒏 𝒃𝒊𝒕𝒄𝒐𝒊𝒏 𝒊𝒏𝒗𝒆𝒔𝒕𝒎𝒆𝒏𝒕𝒔 𝒘𝒊𝒕𝒉𝒊𝒏 48𝒉𝒐𝒖𝒓𝒔 𝒄𝒐𝒏𝒕𝒂𝒄𝒕 𝒂 𝒍𝒆𝒈𝒊𝒕 𝒕𝒓𝒂𝒅𝒆𝒓 𝒕𝒐𝒅𝒂𝒚
https://t.me/joinchat/WUJOHqFmiWs3MzQ0
https://t.me/joinchat/WUJOHqFmiWs3MzQ0
𝙁𝙤𝙧 𝙛𝙧𝙚𝙚 𝙘𝙤𝙪𝙧𝙨𝙚𝙨, 𝙗𝙤𝙤𝙠𝙨, 𝙥𝙧𝙤𝙟𝙚𝙘𝙩𝙨, 𝙞𝙣𝙩𝙚𝙧𝙣𝙨𝙝𝙞𝙥𝙨, 𝙥𝙡𝙖𝙘𝙚𝙢𝙚𝙣𝙩𝙨 𝙖𝙣𝙙 𝙟𝙤𝙗𝙨 𝙧𝙚𝙡𝙖𝙩𝙚𝙙 𝙢𝙖𝙩𝙚𝙧𝙞𝙖𝙡𝙨 𝙖𝙣𝙙 𝙪𝙥𝙙𝙖𝙩𝙚𝙨 𝙟𝙤𝙞𝙣 𝙤𝙪𝙧 𝙩𝙚𝙡𝙚𝙜𝙧𝙖𝙢 𝙘𝙝𝙖𝙣𝙣𝙚𝙡:
@programmingbay
𝙁𝙤𝙧 𝙢𝙤𝙧𝙚 𝙤𝙥𝙥𝙤𝙧𝙩𝙪𝙣𝙞𝙩𝙮 𝙟𝙤𝙞𝙣 𝙤𝙪𝙧 𝙩𝙚𝙡𝙚𝙜𝙧𝙖𝙢 𝙜𝙧𝙤𝙪𝙥:
@programming_bay
𝙊𝙪𝙧 𝙩𝙚𝙡𝙚𝙜𝙧𝙖𝙢 𝙜𝙧𝙤𝙪𝙥 𝙞𝙨 𝙤𝙥𝙚𝙣 𝙛𝙤𝙧 𝙖𝙡𝙡. 𝙅𝙤𝙞𝙣 𝙩𝙝𝙚𝙧𝙚 𝙖𝙣𝙙 𝙥𝙤𝙨𝙩 𝙮𝙤𝙪𝙧 𝙙𝙤𝙪𝙗𝙩𝙨 𝙖𝙣𝙙 𝙦𝙪𝙚𝙧𝙞𝙚𝙨. 𝙒𝙚 𝙬𝙞𝙡𝙡 𝙜𝙞𝙫𝙚 𝙨𝙤𝙡𝙪𝙩𝙞𝙤𝙣.
Hi Revathy Join Our Channel for daily Off-Campus Job Updates ❤️
👇👇
@offcampusjobs_0
@offcampusjobs_0
Need assistance for tcs exam tomorrow text me…100% exam will be cleared and all proofs will be provided…no need to pay full
amount before
Hi Flying jet Welcome to this Community. Join Our Channel for daily Off-Campus Job Updates ❤️
👇👇
@offcampusjobs_0
@offcampusjobs_0
Hi amogh Welcome to this Community. Join Our Channel for daily Off-Campus Job Updates ❤️
👇👇
@placement_0
@placement_0
🔊🔊🔊Attention here 🔊🔊🔊
♦️♦️ Next IRA will be live in this channel on 3rd January for free ♦️♦️
https://t.me/Coding_solution_0
🔰🔰We will try our best to provide all MCQs and both codes 🔰🔰
✅✅JAVA / PYTHON ✅✅✅
➡️ Share the channel link to ones who need help in IRA
For any paid help @saviour_01
