Le migliori 150 intervisteMedio

Somma sottoinsieme uguale partizione

Guida dettagliata e implementazione Python per il problema "Partition Equal Subset Sum".

Dichiarazione del problema

Medio

Dato un array intero nums, restituisci True se puoi partizionare l'array in due sottoinsiemi in modo tale che la somma degli elementi in entrambi i sottoinsiemi sia uguale, o False altrimenti.

Scrivi una funzione canPartition(nums: List[int]) -> bool.

Vincoli
  • 1 <= len(nums) <= 200
  • 1 <= nums[i] <= 100

Esempi

Example 1
Input
nums = [1,5,11,5]
Output
True
Explanation

The array can be partitioned as [1, 5, 5] and [11].

Example 2
Input
nums = [1,2,3,5]
Output
False
Explanation

The array cannot be partitioned into equal sum subsets.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per DP 1D come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.