File tree Expand file tree Collapse file tree 5 files changed +45
-12
lines changed
elements-of-programming-interviews Expand file tree Collapse file tree 5 files changed +45
-12
lines changed Original file line number Diff line number Diff line change 22
33#include " test_framework/generic_test.h"
44using 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
Original file line number Diff line number Diff line change 55using std::vector;
66// Returns the number of valid entries after deletion.
77int 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}
1122vector<int > DeleteDuplicatesWrapper (TimedExecutor& executor, vector<int > A) {
1223 int end = executor.Run ([&] { return DeleteDuplicates (&A); });
Original file line number Diff line number Diff 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 } ,
Original file line number Diff line number Diff line change 44
55
66def 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
1118if __name__ == '__main__' :
Original file line number Diff line number Diff line change 77
88# Returns the number of valid entries after deletion.
99def 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
You can’t perform that action at this time.
0 commit comments