Showing 63 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
Given a string, remove duplicate letters so that each letter appears only once. Ensure the resulting string is the smallest in lexicographical order among all possible results.
#2. Remove Duplicate Letters
Find the lexicographically smallest subsequence of distinct characters from a given string. You need to ensure each character appears only once and the relative order is maintained.
#3. Smallest Subsequence of Distinct Characters
You are moving through a series of rooms based on instructions within each room. Find the first day you've visited every room at least once.
#4. First Day Where You Have Been in All the Rooms
Given a grid representing land and water, count the number of distinct islands. An island is formed by connected land cells.
#5. Number of Islands
Determine if it is possible to finish all courses given prerequisite relationships between them. Essentially, detect if a cycle exists in the course dependencies.
#6. Course Schedule
Find all unique combinations of numbers from a given array that sum up to a specific target value. You can use the same number multiple times.
#7. Combination Sum
Find the length of the longest substring within a given string that contains no repeating characters. The substring must be contiguous.
#8. Longest Substring Without Repeating Characters
Determine if an input string matches a given regular expression that supports '.' and '*' characters. The '.' matches any single character, and '*' matches zero or more occurrences of the preceding element.
#9. Regular Expression Matching
Implement a basic calculator to evaluate a simple expression string. The expression can contain non-negative integers, '+', '-', '*', '/' operators, and spaces; integer division should truncate toward zero.
#10. Basic Calculator II
Find two numbers in a given array that add up to a specific target value, and return their indices. Essentially, you need to efficiently locate the pair that satisfies the sum requirement.
#11. Two Sum
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.
#12. Kth Largest Element in an Array
You are given a grid where some cells are buildings and others are obstacles or empty land. Find the shortest distance a person can travel from an empty land cell to reach all the buildings.
#13. Shortest Distance from All Buildings
You're given a sorted array that has been rotated, and a target value. Find the index of the target value in the rotated array, or return -1 if it's not present.
#14. Search in Rotated Sorted Array
You are a robber planning to rob houses along a street. Maximize your loot, but be careful - adjacent houses have connected security systems that will trigger an alarm if you rob them both.
#15. House Robber
You are given a collection of intervals. Merge all overlapping intervals into a single interval and return the result.
#16. Merge Intervals
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. These consecutive numbers do not need to be adjacent in the original array.
#17. Longest Consecutive Sequence
You are a robber planning to rob houses along a circular street. Determine the maximum amount of money you can rob without robbing adjacent houses, considering the first and last house are neighbors.
#18. House Robber II
Given an array representing the height of bars, calculate how much water can be trapped between them after rain. Think about finding the maximum height to the left and right of each bar.
#19. Trapping Rain Water
Two players alternate picking digits from a string. The goal is to determine if the sum of digits taken by the first player can always be made different from the sum of digits taken by the second player, assuming both play optimally.
#20. Sum Game