Skip to content
CodeItRaw
Two Pointers & Sliding Window

Lesson 1/4

A sorted list gives you information for free: equal values sit side by side, and everything smaller than an element is to its left. A solution that does not use that is a solution written for an unsorted list.

count = 0
previous = None
for v in values:
if v != previous:
count += 1
previous = v
return count
Counting distinct values in a sorted list needs no set: each element is compared only with the one before. The moment the value changes, a new value has begun.

Looking for a value in a sorted list you do not look at it all either. Look at the middle element: if what you want is larger, throw away the whole left half; if smaller, the right half. Each step halves what is left; a million elements take twenty steps. This is binary search.

Tasks

Tasks open in order. Solve them all and the next lesson opens.

This lesson's tasks open when the lessons before it are finished. You can read the explanation now.

  1. 01

    Distinct Values

    Function

  2. 02

    Threshold Counter

    Code Duel

If you would rather not wait for the order, every problem is open without locks: Problem list