-
Notifications
You must be signed in to change notification settings - Fork 111
/
closest-subsequence-sum.nim
58 lines (52 loc) · 1.6 KB
/
closest-subsequence-sum.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Closest Subsequence Sum
import std/[algorithm, sequtils, sets]
proc minAbsDifference(nums: ptr UncheckedArray[cint], n: int, goal: int): int {.exportc.} =
proc fn(a: openarray[cint]): seq[int] =
var
b = newSeq[int]()
i, j: int
result.add 0
for x in a:
b.setLen 0
i = 0
j = 0
while i < result.len or j < result.len:
if i < result.len and (j == result.len or result[i] > result[j]+x):
b.add result[i]
inc i
else:
b.add result[j]+x
if i < result.len and result[i] == result[j]+x: inc i
inc j
result.swap b
result.reverse
let
nleft = n div 2
left = fn(nums.toOpenArray(0, nleft-1))
var i = left.len
result = int.high
for x in fn(nums.toOpenArray(nleft, n-1)):
while i > 0 and left[i-1] >= goal-x: dec i
if i < left.len: result = result.min(left[i]+x-goal)
if i > 0: result = result.min(goal-left[i-1]-x)
###
import std/[algorithm, sequtils, sets]
proc minAbsDifference(nums: ptr UncheckedArray[cint], n: int, goal: int): int {.exportc.} =
proc fn(a: openarray[cint]): seq[int] =
var s = initHashSet[int]()
s.incl 0
for x in a.toOpenArray(0, a.high):
var s1 = s
for y in s: s1.incl x+y
s = s1.move
result = s.items.toSeq
result.sort
let
nleft = n div 2
left = fn(nums.toOpenArray(0, nleft-1))
var i = left.len
result = int.high
for x in fn(nums.toOpenArray(nleft, n-1)):
while i > 0 and left[i-1] >= goal-x: dec i
if i < left.len: result = result.min(left[i]+x-goal)
if i > 0: result = result.min(goal-left[i-1]-x)