Showing 580 questions
Design a data structure that acts as a Least Recently Used (LRU) cache. Implement methods to get and put key-value pairs, evicting the least recently used entry when the cache is full.
#1. LRU Cache
You're given daily stock prices. Find the maximum profit you can make by buying and selling the stock once. You must buy before you sell.
#2. Best Time to Buy and Sell Stock
Find the k most frequent elements in a given array of numbers. Return the top k elements based on their frequency.
#3. Top K Frequent Elements
Maximize your profit by buying and selling a stock multiple times. You're given an array representing stock prices, and can make as many transactions as you want.
#4. Best Time to Buy and Sell Stock II
Find the longest string that is a prefix of all strings in a given array. If no common prefix exists, return an empty string.
#5. Longest Common Prefix
You are given k sorted lists. Combine all of their elements into a single sorted list and return it.
#6. Merge k Sorted Lists
Find the contiguous subarray within a given array of numbers which has the largest sum. Return the sum of this subarray.
#7. Maximum Subarray
Find the maximum profit you can make by performing at most two transactions on a given stock price array. You can buy and sell a stock only once at a time.
#8. Best Time to Buy and Sell Stock III
You are given a collection of intervals. Merge all overlapping intervals into a single interval and return the result.
#9. Merge Intervals
You're given stock prices for multiple days. Determine the maximum profit you can achieve by making at most 'k' transactions (buy and sell) within the given timeframe.
#10. Best Time to Buy and Sell Stock IV
Find the kth largest element in an unsorted array. Note that it is the kth largest element in sorted order, not the kth distinct element.
#11. Kth Largest Element in an Array
You are given a square matrix representing an image. Rotate the image by 90 degrees clockwise in-place.
#12. Rotate Image
Determine if a string containing parentheses is valid. A valid string has matching and properly nested opening and closing parentheses.
#13. Valid Parentheses
Given a grid representing land and water, count the number of distinct islands. An island is formed by connected land cells.
#14. Number of Islands
Find the number of contiguous subarrays within a given array whose product is strictly less than a target value k. The goal is to efficiently count these subarrays by considering the product of their elements.
#15. Subarray Product Less Than K
You are given two arrays: numbers to insert and their corresponding indices. Construct a target array by inserting each number at its specified index.
#16. Create Target Array in the Given Order
Rearrange the characters of a given string according to a custom order defined by another string. If a character isn't in the custom order, place it at the end in any order.
#17. Custom Sort String
Search for a target value in a rotated sorted array that may contain duplicate elements. Return true if the target is found, and false otherwise.
#18. Search in Rotated Sorted Array II
Determine which kids will have the greatest number of candies after distributing extra candies to each of them. Return a boolean array indicating if each kid has the greatest number of candies.
#19. Kids With the Greatest Number of Candies
Find the two largest numbers in an array. Return the maximum product you can get by subtracting 1 from each of them.
#20. Maximum Product of Two Elements in an Array