-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLesson 5 - StoneWall.m
80 lines (61 loc) · 1.28 KB
/
Lesson 5 - StoneWall.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
@interface NSStack: NSObject
@property (strong, nonatomic) NSMutableArray * array;
- (void)push:(id)elem;
- (id)peek;
- (id)pop;
@end
@implementation NSStack
- (id)init {
if (self = [super init]) {
_array = [[NSMutableArray alloc] init];
}
return self;
}
- (void)push:(id)elem {
[_array addObject:elem];
}
- (id)peek
{
NSUInteger len = [_array count];
if (len > 0) {
id elem = [_array objectAtIndex:len - 1];
return elem;
} else {
return nil;
}
}
- (id)pop
{
NSUInteger len = [_array count];
if (len > 0) {
id elem = [_array objectAtIndex:len - 1];
[_array removeLastObject];
return elem;
} else {
return nil;
}
}
- (NSUInteger)size
{
return [_array count];
}
- (NSString *)description
{
return [_array description];
}
@end
int solution(NSMutableArray *H) {
NSStack * stack = [[NSStack alloc] init];
NSUInteger blocks = 0;
for (int i = 0; i < [H count]; i++) {
NSUInteger height = [H[i] intValue];
while ([stack size] > 0 && [[stack peek] intValue] > height) {
[stack pop];
}
if ([[stack peek] intValue] != height) {
[stack push:H[i]];
blocks ++;
}
}
return blocks;
}