Skip to content

Commit a5fdbd5

Browse files
author
rw
committed
debug
1 parent 5790120 commit a5fdbd5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

leetcode.nix

+12-6
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,20 @@ let
4141
# https://leetcode.com/problems/two-sum/
4242
twosum = { nums, target, debug ? false}:
4343
let
44-
h = { };
45-
add = (h: k: v: h // { k = v; });
4644
impl = (builtins.foldl'
4745
(acc: num:
4846
let
4947
comp = target - num;
5048
complement = builtins.toString (comp);
51-
exists = builtins.hasAttr complement h;
52-
compAddsToTarget = exists && comp + num == target;
49+
exists = builtins.hasAttr complement acc.h;
50+
compAddsToTarget = exists && sum == target;
51+
sum = comp + num;
5352
in
5453
if builtins.length acc.ans > 0 then acc
5554
else
5655
if compAddsToTarget
5756
then acc // {
58-
ans = [ h.${complement} acc.i ];
57+
ans = [ acc.h.${complement} acc.i ];
5958
detected = true;
6059
}
6160
else acc // {
@@ -65,15 +64,22 @@ let
6564
i = acc.i + 1;
6665
nums = acc.nums ++ [ num ];
6766
complements = acc.complements ++ [ complement ];
67+
sums = acc.sums ++ [ sum ];
68+
doesAddToTarget = acc.doesAddToTarget ++ [ compAddsToTarget ];
69+
everExists = acc.everExists ++ [ exists ];
70+
6871
}
6972
)
7073
{
7174
i = 0;
72-
h = h;
75+
h = {};
7376
ans = [ ];
7477
nums = [ ];
7578
complements = [ ];
7679
detected = false;
80+
sums = [ ];
81+
doesAddToTarget = [];
82+
everExists = [];
7783
}
7884
nums);
7985
in

0 commit comments

Comments
 (0)