Блог пользователя The_Elephant

Автор The_Elephant, 22 месяца назад, По-английски

This is one of the new added problem for dp(dynamic programming) on cses.

Task : Link

It is similar to 0/1 knapsack or subset sum equal to k. I have solved this problem using a single array.

TC : O(N*M)
SC : O(M)

Solution
Code
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
13 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by The_Elephant (previous revision, new revision, compare).

»
13 месяцев назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Your way of skipping the last element (n) is also good ... If we include it then just find no. of ways to create sum/2 i.e., int ans = dp[sum/2], then output ans / 2 (take care of modulo)
Why ? Since for every possible set that set and its compliment will both contribute to dp[sum/2] but we need it only once (choosing one set automatically creates the other set)!

Code