Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5b6f6a2

Browse files
author
Victor
authored
add some comments.
1 parent 9611004 commit 5b6f6a2

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

‎975. Odd Even Jump.c

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,43 +74,45 @@ We can reach the end from starting indexes 1, 2, and 4.
7474
*/
7575

7676
int smallest_among_greaters(int k, int *map, int sz) {
77-
for (int i = k; i < sz; i ++) {
78-
if (map[i] != 0) return map[i];
79-
}
80-
return 0;
77+
for (int i = k; i < sz; i ++) {
78+
if (map[i] != 0) return map[i];
79+
}
80+
return 0;
8181
}
8282
int greatest_among_smallers(int k, int *map, int sz) {
83-
for (int i = k; i >= 0; i --) {
84-
if (map[i] != 0) return map[i];
85-
}
86-
return 0;
87-
83+
for (int i = k; i >= 0; i --) {
84+
if (map[i] != 0) return map[i];
85+
}
86+
return 0;
87+
}
8888
int oddEvenJumps(int* A, int ASize){
89-
int map[100000] = { 0 };
90-
91-
#define sz (sizeof(map)/sizeof(map[0]))
92-
93-
int hi[20000] = { 0 }, lo[20000] = { 0 };
94-
95-
int i, h, l, ans;
96-
97-
hi[ASize - 1] = lo[ASize - 1] = 1;
98-
map[A[ASize - 1]] = ASize; // 1 - ASize
99-
ans = 1;
100-
101-
for (i = ASize - 2; i >= 0; i --) {
102-
h = smallest_among_greaters(A[i], map, sz);
103-
l = greatest_among_smallers(A[i], map, sz);
104-
if (h && lo[h - 1]) { hi[i] = 1; ans ++; }
105-
if (l && hi[l - 1]) lo[i] = 1;
106-
map[A[i]] = i + 1;
107-
}
108-
109-
return ans;
89+
// if jump can to to a high or low number on current index
90+
bool hi[20000] = { false }, lo[20000] = { false };
91+
92+
// index of a number is on
93+
int map[100000] = { 0 };
94+
#define sz (sizeof(map) / sizeof(map[0]))
95+
96+
int i, h, l, ans;
97+
98+
hi[ASize - 1] = lo[ASize - 1] = true;
99+
100+
map[A[ASize - 1]] = ASize; // index + 1
101+
102+
ans = 1;
103+
104+
for (i = ASize - 2; i >= 0; i --) {
105+
h = smallest_among_greaters(A[i], map, sz);
106+
l = greatest_among_smallers(A[i], map, sz);
107+
if (h && lo[h - 1]) { hi[i] = true; ans ++; }
108+
if (l && hi[l - 1]) lo[i] = true;
109+
map[A[i]] = i + 1;
110+
}
111+
112+
return ans;
110113
}
111114

112115

113-
114116
/*
115117
Difficulty:Hard
116118

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /