-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit-sum.m
93 lines (81 loc) · 3.43 KB
/
split-sum.m
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#import <Foundation/Foundation.h>
NSArray<NSArray<NSNumber *> *> *splitSum(const NSArray<NSNumber *> *nums) {
NSInteger totalLeft = 0;
NSInteger totalRight = 0;
NSInteger left = 0;
NSInteger right = [nums count] - 1;
// Empty array or single element array.
if (right < 1) {
return @[@[], @[]];
}
// Sum until the indexes meet in the middle.
while (right != left) {
if (totalLeft <= totalRight) {
totalLeft += [nums[left] integerValue];
left++;
} else {
totalRight += [nums[right] integerValue];
right--;
}
}
// Check middle in the left group.
if ((totalLeft + [nums[left] integerValue]) == totalRight) {
return @[[nums subarrayWithRange:NSMakeRange(0, left + 1)], [nums subarrayWithRange:NSMakeRange(right + 1, [nums count] - right - 1)]];
}
// Check middle in the right group.
if (totalLeft == (totalRight + [nums[right] integerValue])) {
return @[[nums subarrayWithRange:NSMakeRange(0, left)], [nums subarrayWithRange:NSMakeRange(right, [nums count] - right)]];
}
return @[@[], @[]];
}
// Global so they aren't reallcoated on the stack each invocation.
NSArray<NSArray<NSNumber *> *> *cases = @[ @[],
@[@100],
@[@99, @99],
@[@98, @1, @99],
@[@99, @1, @98],
@[@1, @2, @3, @0],
@[@1, @2, @3, @5],
@[@1, @2, @2, @1, @0],
@[@10, @11, @12, @16, @17],
@[@1, @1, @1, @1, @1, @1, @6],
@[@6, @1, @1, @1, @1, @1, @1],
];
// Test cases
void testCases(bool toScreen) {
@autoreleasepool {
for (NSArray<NSNumber *> *c in cases) {
if (toScreen) {
NSMutableString *outStr = [NSMutableString stringWithString:@"objective-c: {"];
[c enumerateObjectsUsingBlock:^(NSNumber *number, NSUInteger i, BOOL *stop) {
[outStr appendFormat:@"%s%@", i ? ", " : "", number];
}];
[outStr appendString:@"} -> {"];
[splitSum(c) enumerateObjectsUsingBlock:^(NSArray *result, NSUInteger i, BOOL *stop) {
[outStr appendFormat:@"%s{", i ? ", " : ""];
[result enumerateObjectsUsingBlock:^(NSNumber *number, NSUInteger j, BOOL *stop) {
[outStr appendFormat:@"%s%@", j ? ", " : "", number];
}];
[outStr appendString:@"}"];
}];
[outStr appendString:@"}"];
puts([outStr UTF8String]);
} else {
splitSum(c);
}
}
}
}
int main(int argc, const char * argv[]) {
testCases(true);
@autoreleasepool {
NSDate *startTime = [NSDate date];
for (int i = 0; i < 1000000; i++) {
testCases(false);
}
NSDate *endTime = [NSDate date];
NSTimeInterval elapsedTime = [endTime timeIntervalSinceDate:startTime];
puts([ [NSString stringWithFormat:@"objective-c: %.3f seconds", elapsedTime ] UTF8String]);
}
return 0;
}