hard Sorting

Sort Colors

Write a function sortColors that takes an array of integers nums representing colors, where:

  • 0 represents red,
  • 1 represents white,
  • 2 represents blue.

The function should sort the array in place so that the colors are in the order red, white, and blue.

Function Signature

function sortColors(nums);

Example 1:

Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Explanation: The array is sorted so that all reds come first, followed by whites, and then blues.

Example 2:

Input: nums = [2,0,1]
Output: [0,1,2]
Explanation: The array is sorted so that all reds come first, followed by whites, and then blues. o solve it in a single pass (O(n) time complexity) using constant space (O(1) extra space).

Example 3:

Input: nums = [1,2,0,2,1,0]
Output: [0,0,1,1,2,2]
Explanation: The array is sorted so that all reds come first, followed by whites, and then blues.

Example 4:

Input: nums = [0]
Output: [0]
Explanation: The array contains only one element which is already sorted.

Solve this problem

Start your 7-day free trial to solve this problem in our code editor with instant feedback.

Start 7-Day Free Trial to Solve This Problem

Related Problems

Top N Frequent Words

hard

Write a function `topNFrequentWords` that takes a large text document as input and returns the top N most frequent words. The function should be optimized for performance. ## Function Signature ```function topNFrequentWords(text, N);``` ### Example 1: > **Input:** `text = "the quick brown fox jumps over the lazy dog the the the quick quick"`, `N = 2` \ > **Output:** `["the", "quick"]` \ > **Explanation:** "the" and "quick" are the most frequent words appearing 4 and 3 times respectively. ### Example 2: > **Input:** `text = "a a a b b c"`, `N = 1` \ > **Output:** `["a"]` \ > **Explanation:** "a" is the most frequent word appearing 3 times. ### Example 3: > **Input:** `text = "apple banana apple banana orange apple banana banana"`, `N = 2` \ > **Output:** `["banana", "apple"]` \ > **Explanation:** "banana" and "apple" are the most frequent words appearing 4 and 3 times respectively.

String

Longest Palindromic Substring

hard

Write a function `longestPalindrome` that takes a string and returns the longest palindromic substring. ## Function Signature ```function longestPalindrome(s);``` ### Example 1: > **Input:** `s = "babad"` \ > **Output:** `"bab"` \ > **Explanation:** The longest palindromic substring is "bab". ### Example 2: > **Input:** `s = "cbbd"` \ > **Output:** `"bb"` \ > **Explanation:** The longest palindromic substring is "bb". ### Example 3: > **Input:** `s = "a"` \ > **Output:** `"a"` \ > **Explanation:** The longest palindromic substring is "a".

String

Search in Rotated Sorted Array

hard

Write a function `search` that takes a rotated sorted array and a target value, and returns the index if the target is found. If not, returns -1. ## Function Signature ```function search(nums, target);``` ### Example 1: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 0` \ > **Output:** `4` \ > **Explanation:** Target 0 is at index 4. ### Example 2: > **Input:** `nums = [4,5,6,7,0,1,2]`, `target = 3` \ > **Output:** `-1` \ > **Explanation:** Target 3 is not in the array.

Array