mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-24 20:16:06 +00:00
2778a6f9c7
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
55 lines
1.7 KiB
Kotlin
55 lines
1.7 KiB
Kotlin
/**
|
|
* File: subset_sum_i_native.kt
|
|
* Created Time: 2024-01-25
|
|
* Author: curtishd (1023632660@qq.com)
|
|
*/
|
|
|
|
package chapter_backtracking.subset_sum_i_naive
|
|
|
|
/* Backtracking algorithm: Subset sum I */
|
|
fun backtrack(
|
|
state: MutableList<Int>,
|
|
target: Int,
|
|
total: Int,
|
|
choices: IntArray,
|
|
res: MutableList<MutableList<Int>?>
|
|
) {
|
|
// When the subset sum equals target, record the solution
|
|
if (total == target) {
|
|
res.add(state.toMutableList())
|
|
return
|
|
}
|
|
// Traverse all choices
|
|
for (i in choices.indices) {
|
|
// Pruning: if the subset sum exceeds target, skip this choice
|
|
if (total + choices[i] > target) {
|
|
continue
|
|
}
|
|
// Attempt: make choice, update element sum total
|
|
state.add(choices[i])
|
|
// Proceed to the next round of selection
|
|
backtrack(state, target, total + choices[i], choices, res)
|
|
// Backtrack: undo choice, restore to previous state
|
|
state.removeAt(state.size - 1)
|
|
}
|
|
}
|
|
|
|
/* Solve subset sum I (including duplicate subsets) */
|
|
fun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {
|
|
val state = mutableListOf<Int>() // State (subset)
|
|
val total = 0 // Subset sum
|
|
val res = mutableListOf<MutableList<Int>?>() // Result list (subset list)
|
|
backtrack(state, target, total, nums, res)
|
|
return res
|
|
}
|
|
|
|
/* Driver Code */
|
|
fun main() {
|
|
val nums = intArrayOf(3, 4, 5)
|
|
val target = 9
|
|
val res = subsetSumINaive(nums, target)
|
|
|
|
println("Input array nums = ${nums.contentToString()}, target = $target")
|
|
println("All subsets with sum equal to $target res = $res")
|
|
println("Please note that this method outputs results containing duplicate sets")
|
|
} |