Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit dd9019a

Browse files
committed
Add solution #1755
1 parent 8058526 commit dd9019a

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,522 LeetCode solutions in JavaScript
1+
# 1,523 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1352,6 +1352,7 @@
13521352
1752|[Check if Array Is Sorted and Rotated](./solutions/1752-check-if-array-is-sorted-and-rotated.js)|Easy|
13531353
1753|[Maximum Score From Removing Stones](./solutions/1753-maximum-score-from-removing-stones.js)|Medium|
13541354
1754|[Largest Merge Of Two Strings](./solutions/1754-largest-merge-of-two-strings.js)|Medium|
1355+
1755|[Closest Subsequence Sum](./solutions/1755-closest-subsequence-sum.js)|Hard|
13551356
1764|[Form Array by Concatenating Subarrays of Another Array](./solutions/1764-form-array-by-concatenating-subarrays-of-another-array.js)|Medium|
13561357
1765|[Map of Highest Peak](./solutions/1765-map-of-highest-peak.js)|Medium|
13571358
1768|[Merge Strings Alternately](./solutions/1768-merge-strings-alternately.js)|Easy|
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* 1755. Closest Subsequence Sum
3+
* https://leetcode.com/problems/closest-subsequence-sum/
4+
* Difficulty: Hard
5+
*
6+
* You are given an integer array nums and an integer goal.
7+
*
8+
* You want to choose a subsequence of nums such that the sum of its elements is the closest
9+
* possible to goal. That is, if the sum of the subsequence's elements is sum, then you want
10+
* to minimize the absolute difference abs(sum - goal).
11+
*
12+
* Return the minimum possible value of abs(sum - goal).
13+
*
14+
* Note that a subsequence of an array is an array formed by removing some elements (possibly
15+
* all or none) of the original array.
16+
*/
17+
18+
/**
19+
* @param {number[]} nums
20+
* @param {number} goal
21+
* @return {number}
22+
*/
23+
var minAbsDifference = function(nums, goal) {
24+
const n = nums.length;
25+
const half = Math.floor(n / 2);
26+
const leftSums = new Set();
27+
const rightSums = new Set();
28+
29+
generateSums(0, half, leftSums);
30+
generateSums(half, n, rightSums);
31+
32+
const rightArray = [...rightSums].sort((a, b) => a - b);
33+
let minDiff = Infinity;
34+
35+
for (const leftSum of leftSums) {
36+
const target = goal - leftSum;
37+
let left = 0;
38+
let right = rightArray.length - 1;
39+
40+
while (left <= right) {
41+
const mid = Math.floor((left + right) / 2);
42+
const sum = leftSum + rightArray[mid];
43+
minDiff = Math.min(minDiff, Math.abs(sum - goal));
44+
45+
if (sum < goal) {
46+
left = mid + 1;
47+
} else {
48+
right = mid - 1;
49+
}
50+
}
51+
}
52+
53+
return minDiff;
54+
55+
function generateSums(start, end, sums, current = 0) {
56+
if (start === end) {
57+
sums.add(current);
58+
return;
59+
}
60+
generateSums(start + 1, end, sums, current);
61+
generateSums(start + 1, end, sums, current + nums[start]);
62+
}
63+
};

0 commit comments

Comments
 (0)