Skip to main content
Code Review

Return to Answer

Changed array indexes from "positive" to "nonnegative."
Source Link

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positivenonnegative.

Optimization

You could optimize s before walking the array. Since array indices are positivenonnegative integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Optimized Time Complexity

~\0ドル(\lg m)\$ with m <= n and m being the number of positive integers in the array

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positive.

Optimization

You could optimize s before walking the array. Since array indices are positive integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Optimized Time Complexity

~\0ドル(\lg m)\$ with m <= n and m being the number of positive integers in the array

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always nonnegative.

Optimization

You could optimize s before walking the array. Since array indices are nonnegative integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Optimized Time Complexity

~\0ドル(\lg m)\$ with m <= n and m being the number of positive integers in the array

added 1 character in body
Source Link
vnp
  • 58.6k
  • 4
  • 55
  • 144

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positive.

Optimization

You could optimize s before walking the array. Since array indices are positive integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Optimized Time Complexity

~\0ドル(lg m)\$\0ドル(\lg m)\$ with m <= n and m being the number of positive integers in the array

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positive.

Optimization

You could optimize s before walking the array. Since array indices are positive integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Optimized Time Complexity

~\0ドル(lg m)\$ with m <= n and m being the number of positive integers in the array

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positive.

Optimization

You could optimize s before walking the array. Since array indices are positive integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Optimized Time Complexity

~\0ドル(\lg m)\$ with m <= n and m being the number of positive integers in the array

added 85 characters in body
Source Link
dfhwze
  • 14.1k
  • 3
  • 40
  • 101

Review

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positive.

Optimization

Optimization

You could optimize s before walking the array. Since array indices are positive integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm to get time complexity \0ドル(lg n)\$:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Optimized Time Complexity

~\0ドル(lg m)\$ with m <= n and m being the number of positive integers in the array

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positive.

Optimization

You could optimize s before walking the array. Since array indices are positive integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm to get time complexity \0ドル(lg n)\$:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Review

Your solution naively walks the array of ascending integers from starting position s = 0. In some situations, this means you are walking tons of negative numbers, knowing they can never match an array index, which is always positive.

Optimization

You could optimize s before walking the array. Since array indices are positive integers, you should skip walking the array where the values are strict negative.

As en example, if input = [-10000, -9999, ..., 0, 1] you just want to check 0 and 1.

The way I would optimize the algorithm:

  • determine starting point s
  • if first item is positive: s = 0
  • if last item is strict negative: return -1
  • perform binary search to find s (you want s to hold the first positive integer in the array)
  • walk i as from s to end of array
  • on match: return match
  • on array[i] > i: return -1
  • on end reached without match: return -1

Optimized Time Complexity

~\0ドル(lg m)\$ with m <= n and m being the number of positive integers in the array

Source Link
dfhwze
  • 14.1k
  • 3
  • 40
  • 101
Loading
default

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