- vừa được xem lúc

Subset Sum in Dynamic Programming

0 0 1

Người đăng: Augustus Nguyen

Theo Viblo Asia

Dynamic Programming (P1)

The Subset Sum is a classic problem in Dynamic Programming (DP). It is used to determine whether there exists a subset of a given set of numbers that sums up to a specific target value. This is a problem commonly encountered in areas like partitioning problems, knapsack problems, and combinatorial optimization.

Problem Description:

Given a set of integers, you need to determine whether there exists a subset whose sum is equal to a specific target value.

Example:

  • Input:nums = [2, 3, 7, 8, 10] , target = 11

  • Output: True

    • There exists a subset of nums that adds up to 11, for example: [3, 8] or[2, 3, 7].
  • Input: nums = [1, 2, 3, 9], target = 8

  • Output: False

    • No subset sums to 8.

Approach (Dynamic Programming):

1. Define the Problem:

  • We want to know if there is a subset of the numbers in the array that sums up to the given target.

2. State Representation:

  • Let dp[i] represent whether a subset sum of i is achievable using any subset of the numbers in the array.

  • Initialize dp[0]= true because a sum of 0 is always achievable (the empty subset).

3. Transition:

For each number in the array, we update the dp array in reverse order. For each i, if dp[i - num] is true (i.e., if i - num can be achieved), then set dp[i]to true.

4. Final Answer:

The answer is stored in dp[target]. If dp[target] is true, then there is a subset whose sum is equal to the target.

C++ Solution Example:

#include <iostream>
#include <vector>
using namespace std; bool subsetSum(const vector<int>& nums, int target) { vector<bool> dp(target + 1, false); // DP array initialized to false dp[0] = true; // Sum of 0 is always possible with an empty subset for (int num : nums) { for (int i = target; i >= num; --i) { dp[i] = dp[i] || dp[i - num]; // If dp[i-num] is true, set dp[i] to true } } return dp[target]; // Return if the target sum is achievable
} int main() { vector<int> nums = {2, 3, 7, 8, 10}; int target = 11; if (subsetSum(nums, target)) { cout << "There is a subset with sum " << target << endl; } else { cout << "There is no subset with sum " << target << endl; } return 0;
}

Bình luận

Bài viết tương tự

- vừa được xem lúc

React hooks: Sự khác nhau giữa useMemo và useCallback

Thư viện React cung cấp 2 hook được build sẵn giúp chúng ta tối ưu hoá hiệu suất của app: useMemo và useCallback. Ở lần load đầu tiên, thoạt nhìn có vẻ như cách hoạt động của chúng khá giống nhau, vì

0 0 32

- vừa được xem lúc

6 tip hữu ích cho frontend có thể bạn chưa biết

Hôm nay mình sẽ chia sẻ một số tip hữu ích cho CSS, Html, Javascript. .

0 0 32

- vừa được xem lúc

Tổng quan về Active Directory trên Windows Server

I. Tổng quan:. 1) Active Directory là gì:. .

0 0 122

- vừa được xem lúc

Tổng hợp bài tập ôn thi ISTQB ADVANCED

❗ Học ISTQB advanced level để giúp bạn mở rộng thêm các kỹ năng mới trong test, có kinh nghiệm và chiến thuật thông minh trong test, chủ động xử lý các vấn đề trước khi nó xảy ra, test hiệu quả hơn. ✔

0 0 25

- vừa được xem lúc

DevTestOps xu hướng Tester nên biết

Khi nói đến chúng ta đều biết tới Manual testing và Automation testing. Ở hầu hết các quy trình sản xuất phần mềm, các Tester thường tham gia vào những công đoạn sau, làm hạn chế những hiệu quả mà Te

0 0 27

- vừa được xem lúc

Một số lưu ý trong automation test khi xác định elements

Khi thực hiện automation test trên web, các trường hợp thường gặp phải khi bắt element (các phần tử trên trang web) bao gồm:. 1.

0 0 24