Skip to the code
+25 XP

#143 Longest Session With Two Features

medium

A product analytics tool logs which feature each user action used, as feature ids in actions. A "focused" stretch is a run of neighboring actions that uses at most two different features.

Write longestFocused(actions) that returns the length of the longest focused stretch.

Example 1

Input: actions = [1,2,1]
Output: 3

All three actions use only features 1 and 2.

Example 2

Input: actions = [0,1,2,2]
Output: 3

[1, 2, 2] uses two features.

Constraints

  • 0 <= actions.length <= 100000
  • 0 <= actions[i] <= 100000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).