Skip to content

Commit 9c35c38

Browse files
committed
EPI: array game, array duplicate removal (py, cpp)
1 parent 9c2179f commit 9c35c38

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

elements-of-programming-interviews/cpp/advance_by_offsets.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
#include "test_framework/generic_test.h"
44
using std::vector;
5-
bool CanReachEnd(const vector<int>& max_advance_steps) {
6-
// TODO - you fill in here.
5+
bool CanReachEnd(const vector<int>& arr) {
6+
int best = arr[0];
7+
for (int curr = 0; curr < arr.size(); curr++) {
8+
if (best >= arr.size() - 1) {
9+
return true;
10+
}
11+
if (curr > best) {
12+
return false;
13+
}
14+
best = (curr + arr[curr] > best) ? curr + arr[curr] : best;
15+
}
716
return true;
817
}
918

elements-of-programming-interviews/cpp/sorted_array_remove_dups.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55
using std::vector;
66
// Returns the number of valid entries after deletion.
77
int DeleteDuplicates(vector<int>* A_ptr) {
8-
// TODO - you fill in here.
9-
return 0;
8+
vector<int>& A = *A_ptr;
9+
if (A.size() == 1 || A.size() == 0) {
10+
return A.size();
11+
}
12+
13+
int target = 1;
14+
for (int i = 1; i < A.size(); i++) {
15+
if (A[i - 1] != A[i]) {
16+
A[target] = A[i];
17+
target++;
18+
}
19+
}
20+
return target;
1021
}
1122
vector<int> DeleteDuplicatesWrapper(TimedExecutor& executor, vector<int> A) {
1223
int end = executor.Run([&] { return DeleteDuplicates(&A); });

elements-of-programming-interviews/problem_mapping.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,29 +228,29 @@ problem_mapping = {
228228
},
229229
"5.04 Advancing through an array": {
230230
"C++: advance_by_offsets.cc": {
231-
"passed": 0,
231+
"passed": 2004,
232232
"total": 2004
233233
},
234234
"Java: AdvanceByOffsets.java": {
235235
"passed": 0,
236236
"total": 2004
237237
},
238238
"Python: advance_by_offsets.py": {
239-
"passed": 0,
239+
"passed": 2004,
240240
"total": 2004
241241
}
242242
},
243243
"5.05 Delete duplicates from a sorted array": {
244244
"C++: sorted_array_remove_dups.cc": {
245-
"passed": 0,
245+
"passed": 2003,
246246
"total": 2003
247247
},
248248
"Java: SortedArrayRemoveDups.java": {
249249
"passed": 0,
250250
"total": 2003
251251
},
252252
"Python: sorted_array_remove_dups.py": {
253-
"passed": 0,
253+
"passed": 2003,
254254
"total": 2003
255255
}
256256
},

elements-of-programming-interviews/python/advance_by_offsets.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55

66
def can_reach_end(A: List[int]) -> bool:
7-
# TODO - you fill in here.
8-
return True
7+
best = A[0]
8+
curr = 0
9+
for dist in A:
10+
if (curr > best):
11+
return False
12+
if (best >= len(A)-1):
13+
return True
14+
best = max(curr + A[curr], best)
15+
curr += 1
916

1017

1118
if __name__ == '__main__':

elements-of-programming-interviews/python/sorted_array_remove_dups.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77

88
# Returns the number of valid entries after deletion.
99
def delete_duplicates(A: List[int]) -> int:
10-
# TODO - you fill in here.
11-
return 0
10+
i = 1
11+
target = 1
12+
while i < len(A):
13+
if A[i-1] != A[i]:
14+
A[target] = A[i]
15+
target += 1
16+
i += 1
17+
return target
1218

1319

1420
@enable_executor_hook

0 commit comments

Comments
 (0)