-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #623 from vaskoz/day305
Day305
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package day305 | ||
|
||
// LL is a linked list of integers. | ||
type LL struct { | ||
Value int | ||
Next *LL | ||
} | ||
|
||
// RemoveConsecutiveSumZero removes all sublists that sum to zero. | ||
// Runs in O(N^2) time and O(1) space. | ||
func RemoveConsecutiveSumZero(head *LL) *LL { | ||
fakeHead := &LL{0, head} | ||
for start := fakeHead; start != nil; start = start.Next { | ||
var sum int | ||
var farthest *LL | ||
var found bool | ||
for next := start.Next; next != nil; next = next.Next { | ||
sum += next.Value | ||
if sum == 0 { | ||
farthest = next.Next | ||
found = true | ||
} | ||
} | ||
if found { | ||
start.Next = farthest | ||
} | ||
} | ||
return fakeHead.Next | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package day305 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
input, expected *LL | ||
}{ | ||
{ | ||
&LL{3, &LL{4, &LL{-7, &LL{5, &LL{-6, &LL{6, nil}}}}}}, | ||
&LL{5, nil}, | ||
}, | ||
{ | ||
&LL{3, &LL{4, &LL{5, &LL{-12, nil}}}}, | ||
nil, | ||
}, | ||
{ | ||
&LL{3, &LL{4, &LL{-7, &LL{5, &LL{-5, &LL{6, nil}}}}}}, | ||
&LL{6, nil}, | ||
}, | ||
{ | ||
&LL{3, &LL{4, &LL{-7, &LL{3, &LL{5, &LL{-6, &LL{6, &LL{-5, nil}}}}}}}}, | ||
&LL{3, nil}, | ||
}, | ||
{ | ||
&LL{1, &LL{1, &LL{2, &LL{3, &LL{4, &LL{5, &LL{6, &LL{-5, nil}}}}}}}}, | ||
&LL{1, &LL{1, &LL{2, &LL{3, &LL{4, &LL{5, &LL{6, &LL{-5, nil}}}}}}}}, | ||
}, | ||
} | ||
|
||
func equal(a, b *LL) bool { | ||
for a != nil && b != nil { | ||
if a.Value != b.Value { | ||
return false | ||
} | ||
a = a.Next | ||
b = b.Next | ||
} | ||
return a == nil && b == nil | ||
} | ||
|
||
func TestRemoveConsecutiveSumZero(t *testing.T) { | ||
t.Parallel() | ||
for tcid, tc := range testcases { | ||
if result := RemoveConsecutiveSumZero(tc.input); !equal(result, tc.expected) { | ||
t.Errorf("TCID%d doesn't match", tcid) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkRemoveConsecutiveSumZero(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
RemoveConsecutiveSumZero(tc.input) | ||
} | ||
} | ||
} |