Competitive Programming簡単

Subset Sum

Detailed guide and Python implementation for the 'Subset Sum' problem.

問題提起

簡単

Write a function is_subset_sum(arr, total) that returns True if there exists a subset of the non-negative integers in arr that sums to total, and False otherwise.

制約
  • 1 <= len(arr) <= 100
  • 0 <= arr[i] <= 1000
  • 0 <= total <= 10000

Example 1
Input
is_subset_sum([3, 34, 4, 12, 5, 2], 9)
Output
True
Explanation

The subset {4, 5} sums to 9.

Example 2
Input
is_subset_sum([3, 34, 4, 12, 5, 2], 30)
Output
False
Explanation

No subset sums to 30.

Need a Hint?
Consider using Dynamic Programming-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。