An LRU (Least Recently Used) Cache is a data structure that keeps track of the most recently used items and efficiently removes the least recently used ones when it reaches capacity. It’s commonly used to optimize memory usage in systems that handle large data sets by retaining only frequently accessed items.
…Search Insert Position - LeetCode
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
…A peak element is an element that is strictly greater than its neighbors.
Given a 0-indexed integer array nums
, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
You are given an m x n
integer matrix matrix
with the following two properties:
Length of Last Word - LeetCode
Given a string s
consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Remove Duplicates from Sorted Array || - LeetCode
Given an integer array nums
sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.
Best Time to Buy and Sell Stock - LeetCode
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
Given an integer array nums
, rotate the array to the right by k
steps, where k
is non-negative.
Remove Duplicates from Sorted Array - LeetCode
Given an integer array nums
sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums
.
Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The order of the elements may be changed. Then return the number of elements in nums
which are not equal to val
.
You are given two integer arrays nums1
and nums2
, sorted in non-decreasing order, and two integers m
and n
, representing the number of elements in nums1 and nums2 respectively.
Next Greater Element I - LeetCode
The next greater element of some element x
in an array is the first greater element that is to the right of x in the same array.
Longest Common Prefix - LeetCode
Write a function to find the longest common prefix string amongst an array of strings.
…Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
Bubble sort is a simple and straightforward sorting algorithm commonly used in computer science. In Swift language, Bubble sort works by repeatedly swapping adjacent elements if they are in the wrong order until the entire list is sorted. It has a time complexity of O(n^2), making it inefficient for large data sets. The algorithm iterates through the list multiple times, comparing and swapping elements. However, it is easy to understand and implement, making it suitable for small or nearly sorted arrays where simplicity is prioritized over efficiency. For larger data sets, more efficient sorting algorithms like QuickSort or MergeSort are preferred.
…A linked list is a data structure in Swift that consists of nodes linked together via pointers or references. Each node contains data and a reference to the next node. Unlike an array, a linked list does not require contiguous memory allocation. Insertions and deletions can be performed efficiently in a linked list by updating the pointers, but accessing elements requires traversing the list linearly. Linked lists are useful when frequent insertions or deletions are expected, and their time complexity for most operations is O(1) or O(n) depending on the operation.
…To remove duplicates from an array in Swift using a dictionary, we can create an empty dictionary and iterate through the array. For each element, we use it as a key in the dictionary and assign a dummy value. The dictionary automatically removes duplicate keys, so we end up with only unique elements. The time complexity of this approach is O(n), where n is the number of elements in the array, making it an efficient solution for removing duplicates..
…Binary search is an efficient search algorithm used to locate a specific element in a sorted array or list. It works by repeatedly dividing the search space in half until the target element is found. In Swift, binary search is typically implemented recursively or iteratively. The time complexity of binary search is O(log n), where n is the number of elements in the array. This makes it highly efficient for large data sets as it eliminates half of the remaining search space at each step, significantly reducing the search time.
…Merging two sorted Int arrays in Swift means combining them into a single sorted array while preserving the order. With Swift, you can efficiently implement the merging algorithm. By comparing elements in both arrays and arranging them accordingly, you’ll obtain a new sorted array. This process ensures that the elements from both arrays are in ascending order, allowing you to organize your data effectively. The merged sorted Int array can be readily used in your Swift app for various purposes like displaying sorted lists or conducting efficient searches.
…