Posts with tag 'array'

Remove Duplicate Numbers

Given a list of integers nums, remove numbers that appear multiple times in the list, while maintaining order of the appearance in the original list.

Longest Sublist with K Distinct Numbers

Given an integer k and a list of integers nums, return the length of the longest sublist that contains at most k distinct integers.

Profitable Job Matching

You are given lists of integers people, jobs, profits. Each person i in people have people[i] amount of strength, and performing job j requires jobs[j] amoun...

Sum of Three Numbers Less than Target

Given a list of integers nums and an integer target, return the number of triples i < j < k that exist such that nums[i] + nums[j] + nums[k] < target.

Maximum Sum Removing K Numbers From Ends

You are given a list of integers nums and integer k. Return the maximum sum of elements that you can remove given that you must pop exactly k times, where ea...

Longest Sublist of 1s After K Sets

You are given a list of integers nums containing 1s and 0s and an integer k. Given that you can set at most k 0s to 1s, return the length of the longest subl...

Longest Consecutive Sequence

Given an unsorted array of integers nums, find the length of the longest sequence of consecutive elements.

Longest Strictly Increasing Then Decreasing Sublist

You are given a list of integers nums. Return the length of the longest sublist such that its length is at least 3 and its values are strictly increasing and...

Sort by Permutation

Given a list of strings lst and a list of integers p, reorder lst so that every lst[i] gets placed to p[i].

Dividing Station

Given a list of distinct positive integers nums, find the largest subset such that every pair of elements in the subset (i, j) satisfies either i % j = 0 or ...

Diagonal Sort

Given a two-dimensional integer matrix, sort each of the diagonals descending from left to right in ascending order.

Rotate a Box Under Gravity

You are given a two-dimensional list of strings matrix. Each element in matrix contains one of the following:

Permute List to Make Largest Range Sum

You are given a list of integers nums and a two-dimensional list of integers ranges. Each element in ranges contains [i, j], meaning to sum the numbers in nu...

List Partitioning with Inequality Relation

Given a list of integers nums, we want to split the list into two non-empty sublists a and b such that every element in a is less than or equal to every elem...

Maximum Unique Sublist Sum

You are given a list of non-negative integers nums. Return the maximum sum of a sublist in nums containing only unique values.

Next Integer Permutation

Given an integer n, return the next bigger permutation of its digits.

Range Query on Two Dimensional List

Implement a data structure with the following methods:

Sum Pairs to Target

You are given a list of integers nums and an integer target. In one operation we can remove any two numbers in nums if the pair sums to target. Return the ma...

Eat Bananas in K Hours

You are given a list of integers piles and an integer k. piles[i] represents the number of bananas on pile i. On each hour, you can choose any pile and eat r...

Submajority Vote

You are given a list of integers nums where each number represents a vote to a candidate.

XOR Range Queries

You are given a list of integers nums and a two-dimensional list of integers queries. Each element in queries contains [i, j] and asks what is the value of n...

K Distinct Window

Given a list of integers nums and an integer k, return a list of count of distinct numbers in each window of size k.

Longest Bitonic Subsequence

A sequence is called bitonic if it’s strictly increasing and then strictly decreasing. A sequence that is only strictly increasing is bitonic. Also, a sequen...

Matrix Search

Given a two-dimensional integer matrix, where every row and column is sorted in ascending order, find the kth (0-indexed) smallest number.

Matrix Search Sequel

Given a two-dimensional integer matrix, where every row and column is sorted in ascending order, return whether an integer target exists in the matrix.

You are given a list of integers positions representing the position of a car at equally spaced intervals of time. Return the length of the longest sublist w...

Spiral Matrix

Given a 2-d array matrix, return elements in spiral order starting from matrix[0][0].

Smallest Intersecting Element

You are given a two-dimensional list of integers matrix where each row is sorted in ascending order. Return the smallest number that exists in every row. If ...

Minimum Dropping Path Sum With Column Distance Constraint

You are given a two-dimensional list of integers matrix. Return the minimum sum you can get by taking a number in each row with the constraint that any row-a...

Find the Largest Number in a Rotated List

You are given a list of unique integers nums that is sorted in ascending order and is rotated at some pivot point. Find the maximum number in the rotated list.

Remove Duplicates Occurring More Than Twice

Given a list of integers nums sorted in ascending order, remove in-place duplicates that appear more than twice.

Largest and Smallest Difference

Given a list of integers nums and an integer k, choose elements from nums to create a list of length k such that the difference between the largest integer i...

Rotate by 90 Degrees Counter-Clockwise

Given a two-dimensional square matrix, rotate in-place it 90 degrees counter-clockwise.

Packing Boxes

Given a list of integers nums, pack consecutive elements of the same value into sublists.

Merging K Sorted Lists

Given a list of sorted lists of integers, merge them into one large sorted list.

Binary Matrix Leftmost One

You are given a two-dimensional list of integers matrix which contains 1s and 0s. Given that each row is sorted in ascending order with 0s coming before 1s, ...

Valid N Queens

The n queens puzzle asks to place n queens on an n×n chessboard so that no two queens are attacking each other.

Three Player Coin Game

You are given a list of non-negative integers piles where each piles[i] represents the number of coins on pile i. In each move, you can do the following unti...

Bubble Swap

Given two lists of integers lst0 and lst1, each of length n and where each value is unique to its array and between 0 to n - 1, return the minimum number of ...

Wolves of Wall Street

Given a list of integers prices representing the stock prices of a company in chronological order, return the maximum profit you could have made from buying ...

Wolf of Wall Street

Given a list of integers prices representing the stock prices of a company in chronological order, return the maximum profit you could have made from buying ...

Unidirectional Word Search

Given a two-dimensional matrix of characters board and a string target, return whether the target can be found in the matrix by going left-to-right, or up-to...

Transpose of a Matrix

Given an integer square (n by n) matrix, return its transpose. A transpose of a matrix switches the row and column indices. That is, for every r and c, matri...

Toeplitz Matrix

Given a two-dimensional matrix of integers matrix, determine whether it’s a Toeplitz matrix. A Toeplitz is one where every diagonal descending from left to r...

You are given a list of integers tasks and another list of integers people. The integer tasks[i] represents the amount of strength required to perform the it...

Swap Consecutive Index Pairs

Given a list of integers nums, swap each consecutive even indexes with each other, and swap each consecutive odd indexes with each other.

Sum of Two Numbers

Given a list of numbers nums and a number k, return whether any two elements from the list add up to k. You may not use the same element twice.

Sum of Two Numbers with Sorted List

Given a list of integers nums sorted in ascending order and an integer k, return whether any two elements from the list add up to k. You may not use the same...

Sum of Two Numbers Less Than Target

Given a list of integers nums and an integer target, return the sum of the largest pair of numbers in nums whose sum is less than target. You can assume that...

Sum of Three Numbers

Given a list of integers nums and an integer k, determine if there are three distinct elements in the list that add up to k.

Strictly Increasing or Strictly Decreasing

Given a list of integers nums, return whether the list is strictly increasing or strictly decreasing.

Square of a List

Given a list of integers sorted in ascending order nums, square the elements and give the output in sorted order.

Shortest Sublist With Max Frequency

You are given a list of integers nums. Let k be the frequency of a most frequent number in nums. Return the length of a shortest sublist such that the freque...

Shortest Sublist to Sort

Given a list of integers nums, return the length of the shortest sublist in nums which if sorted would make nums sorted in ascending order.

Sentence Reversal

Given a list of strings sentence where each sentence[i] is a string with single character, reverse the order of the words in the sentence.

Rotate List Left by K

Write a function that rotates a list of numbers to the left by k elements. Numbers should wrap around.

Remove Last Duplicate Entries

Given a list of integers nums, find all duplicate numbers and delete their last occurrences.

Range Update

You are given a list of integers nums and a two-dimensional list of integers operations. Each operation is of the following form: [L, R, X], which means that...

Range Query on a List

Implement a data structure with the following methods:

Rain Catcher

You are given a list of non-negative integers nums where each element represents the height of a hill. Suppose it will rain and all the spaces between two si...

You are given a list of integers nums sorted in ascending order, and integers a, b, and c. Apply the following function for each number x in nums: \(ax^2 + b...

Odd Number of Digits

Given a list of positive integers nums, return the number of integers that have odd number of digits.

Number of Sublists With Sum of Target

Given a list of integers nums and an integer target, return the number of sublists whose sum is equal to target.

Mixed Sorting

Given a list of integers nums, sort the array such that:

Minimum Initial Value for Positive Prefix Sums

You are given a list of integers nums. Return the minimum positive value we can append to the beginning of nums such that prefix sums of the resulting list c...

Minimum Cost Sort

Given a list of integers nums, return the minimum cost of sorting the list in ascending or descending order. The cost is defined as the sum of absolute diffe...

Minimize Amplitude After Deleting K-Length Sublist 🎄

You are given a list of integers nums and an integer k. Given that you must first remove a sublist of length k, return the minimum resulting max(nums) - min(...

Merging Two Sorted Lists

Given two lists of integers a and b sorted in ascending order, merge them into one large sorted list.

Median Minimization

Given a list of integers nums, split it into two lists of equal size where the absolute difference between each list’s median is as small as possible and ret...

Maximum Number by Inserting Five

Given an integer n, return the maximum number you can make by inserting 5 anywhere in the number.

Maximize Social Distancing

You are given a list of integers seats containing 1s and 0s. Each element seats[i] represents a seat and is either occupied if seats[i] = 1 or empty if seats...

Max Product of Three Numbers

Given a list of integers nums, find the largest product of three distinct elements.

Matrix Prefix Sum

Given a two-dimensional integer matrix, return a new matrix A of the same dimensions where each element is set to A[i][j] = sum(matrix[r][c]) for all r ≤ i, ...

Longest Sign Alternating Subsequence

Given a list of non-zero integers nums, return the length of longest subsequence that flips signs on each consecutive number.

Longest Alliteration

Given a list of lowercase alphabet strings words, return the length of the longest contiguous sublist where all words share the same first letter.

List Partitioning

Given a list of strings strs, containing the strings "red", "green" and "blue", partition the list so that the red come before green, which come before blue.

List Min Replacement

Given a list of integers nums, replace every nums[i] with the smallest integer left of i. Replace nums[0] with 0.

Lexicographically Largest Mountain List

You are given three positive integers n, lower, and upper. You want to create a list of length n that is strictly increasing and then strictly decreasing and...

You are given a list of integers nums, representing the number of chess matches each person has won. Return a relative ranking (0-indexed) of each person. If...

Largest Sum After K Negations

You are given a list of integers nums and an integer k. Consider an operation where you pick an element in nums and negate it. Given that you must make exact...

Kth Smallest Element

Given a list of unsorted integers nums, and an integer k, return the kth (0-indexed) smallest element in the list.

K Prefix

Given a list of integers nums and an integer k, return the maximum possible i where nums[0] + nums[1] + ... + nums[i] ≤ k. Return -1 if no valid i exists.

K Numbers Greater Than or Equal to K

You are given a list of non-negative integers nums. If there are exactly k numbers in nums that are greater than or equal to k, return k. Otherwise, return -1.

Insertion Index in Sorted List

Given a list of integers nums, sorted in ascending order, and a number target, return the index where target should be inserted to keep nums sorted. If targe...

Index with Equal Left and Right Sums

Given a list of integer nums, return the earliest index i such that the sum of the numbers left of i is equal to the sum of numbers right of i. If there’s no...

In-Place Move Zeros to End of List

Given a list of integers nums, put all the zeros to the back of the list by modifying the list in-place. The relative ordering of other elements should stay ...

Hoppable

Given an integer list nums where each number represents the maximum number of hops you can make, determine whether you can reach to the last index starting a...

Fixed Point

Given a list of unique integers nums sorted in ascending order, return the minimum i such that nums[i] == i. If there’s no solution, return -1.

First Fit Room

You are given a list of integers rooms and an integer target. Return the first integer in rooms that’s target or larger. If there is no solution, return -1.

Equivalent Pairs

You are given a list of integers nums. Return the number of pairs i < j such that nums[i] = nums[j].

Detect the Only Duplicate in a List

You are given a list nums of length n + 1 picked from the range 1, 2, ..., n. By the pigeonhole principle, there must be a duplicate. Find and return it. The...

Consecutive Ones

You are given a list of integers nums which contains at least one 1. Return whether all the 1s appear consecutively.

Complete an Arithmetic Sequence

You are given a list of integers nums that used to be an arithmetic sequence. Given that a number was removed, and that the number was not the first or the l...

Column Sort

Given a two-dimensional integer matrix, sort each of the columns in ascending order.

Given a list of integers prices representing prices of cars for sale, and a budget k, return the maximum number of cars you can buy.

Arithmetic Sequences

Given a list of integers nums, return the number of contiguous arithmetic sequences of length ≥ 3.

Arithmetic Sequence Permutation

Given a list of integers nums, return whether you can rearrange the order of nums such that the difference between every consecutive two numbers is the same.