1755. Closest Subsequence Sum Hard

@problem@discussion
#Array#Two Pointers#Dynamic Programming#Bit Manipulation#Bitmask



1/**
2 * [1755] Closest Subsequence Sum
3 *
4 * You are given an integer array nums and an integer goal.
5 * You want to choose a subsequence of nums such that the sum of its elements is the closest possible to goal. That is, if the sum of the subsequence's elements is sum, then you want to minimize the absolute difference abs(sum - goal).
6 * Return the minimum possible value of abs(sum - goal).
7 * Note that a subsequence of an array is an array formed by removing some elements (possibly all or none) of the original array.
8 *  
9 * Example 1:
10 * 
11 * Input: nums = [5,-7,3,5], goal = 6
12 * Output: 0
13 * Explanation: Choose the whole array as a subsequence, with a sum of 6.
14 * This is equal to the goal, so the absolute difference is 0.
15 * 
16 * Example 2:
17 * 
18 * Input: nums = [7,-9,15,-2], goal = -5
19 * Output: 1
20 * Explanation: Choose the subsequence [7,-9,-2], with a sum of -4.
21 * The absolute difference is abs(-4 - (-5)) = abs(1) = 1, which is the minimum.
22 * 
23 * Example 3:
24 * 
25 * Input: nums = [1,2,3], goal = -7
26 * Output: 7
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	1 <= nums.length <= 40
32 * 	-10^7 <= nums[i] <= 10^7
33 * 	-10^9 <= goal <= 10^9
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/closest-subsequence-sum/
39// discuss: https://leetcode.com/problems/closest-subsequence-sum/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn min_abs_difference(nums: Vec<i32>, goal: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_1755() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.