Pattern
Sorting
A compact view of archive entries grouped by recurring technique.
153 entries
153 entries tagged as Sorting.
21.04.2026
medium1722. Minimize Hamming Distance After Swap Operations
Diff src vs target after rearrange by rules
01.04.2026
hard2751. Robot Collisions
Collide LR robots
17.03.2026
medium1727. Largest Submatrix With Rearrangements
Max area of binary matrix, swap columns
14.03.2026
medium1415. The k-th Lexicographical String of All Happy Strings of Length n
K-th generated abc non-repeating string
25.02.2026
easy1356. Sort Integers by The Number of 1 Bits
Sort
01.02.2026
easy3010. Divide an Array Into Subarrays With Minimum Cost I
Split on 2 min values
26.01.2026
easy1200. Minimum Absolute Difference
Min diff pairs
25.01.2026
easy1984. Minimum Difference Between Highest and Lowest of K Scores
Min k-diff window
24.01.2026
medium1877. Minimize Maximum Pair Sum in Array
Min max pair
23.01.2026
hard3510. Minimum Pair Removal to Sort Array II
Sort by removing min-sum pairs
22.01.2026
easy3507. Minimum Pair Removal to Sort Array I
Sort by removing lowest sum pairs
15.01.2026
medium2943. Maximize Area of Square Hole in Grid
Max square area after removing bars in a grid
25.12.2025
medium3075. Maximize Happiness of Selected Children
K max values, decreas each pick
24.12.2025
easy3074. Apple Redistribution into Boxes
Min containers for all apples sort
23.12.2025
medium2054. Two Best Non-Overlapping Events
Max at most two non-overlapping events max
22.12.2025
hard960. Delete Columns to Make Sorted III
Remove columns to sort strings
21.12.2025
medium955. Delete Columns to Make Sorted II
Remove columns to sort strings
12.12.2025
medium3433. Count Mentions Per User
Count messages and track online users
11.12.2025
medium3531. Count Covered Buildings
Count surrounded dots on XY plane
20.11.2025
hard757. Set Intersection Size At Least Two
Min set each interval have two values in it
04.11.2025
easy3318. Find X-Sum of All K-Long Subarrays I
Sums of x most frequent from k-windows
18.10.2025
medium3397. Maximum Number of Distinct Elements After Operations
Distincts count after adding -k..k to each
11.10.2025
medium3186. Maximum Total Damage With Spell Casting
Max sum of choosen values, skip v[i]+1+2-1-2
08.10.2025
medium2300. Successful Pairs of Spells and Potions
Number of potions spells[i] bigger than success
03.10.2025
hard407. Trapping Rain Water II
Fill water in 3D
28.09.2025
easy976. Largest Perimeter Triangle
Max triangle perimeter from array of lengths window
26.09.2025
medium611. Valid Triangle Number
Count triangles can be made from numbers search sum
11.09.2025
medium2785. Sort Vowels in a String
Sort vowels
03.09.2025
hard3027. Find the Number of Ways to Place People II
Left-top, bottom-right pairs with empty rectangles
02.09.2025
medium3025. Find the Number of Ways to Place People I
Left-top, bottom-right pairs with empty rectangles
28.08.2025
medium3446. Sort Matrix by Diagonals
Sort bottom left and top right diagonals
19.07.2025
hard1233. Remove Sub-Folders from the Filesystem
Fold folders
13.07.2025
medium2410. Maximum Matching of Players With Trainers
Count trainers for players
10.07.2025
medium3440. Reschedule Meetings for Maximum Free Time II
Max free time after moving 1 event
30.06.2025
easy594. Longest Harmonious Subsequence
Longest subsequence with max-min=1 pointers
29.06.2025
medium1498. Number of Subsequences That Satisfy the Given Sum Condition
Subsequencies target in 0..min+max search pointers
28.06.2025
easy2099. Find Subsequence of Length K With the Largest Sum
Subsequence of k largest
23.06.2025
hard2081. Sum of k-Mirror Numbers
n palindromes in both 10 and k-base
19.06.2025
medium2294. Partition Array Such That Maximum Difference Is K
k-narrow subsequences
18.06.2025
medium2966. Divide Array Into Arrays With Max Difference
List of k-narrow tripplets
22.05.2025
medium3362. Zero Array Transformation III
Max removed queries still zero-fy an array
17.05.2025
medium75. Sort Colors
Sort 0,1,2 array pointers
31.03.2025
hard2551. Put Marbles in Bags
Max split sum - min split sum of borders
28.03.2025
hard2503. Maximum Number of Points From Grid Queries
Running queries of increasing BFS
25.03.2025
medium3394. Check if Grid can be Cut into Sections
non-intersecting ranges on x or y axis sweep
24.03.2025
medium3169. Count Days Without Meetings
Days of non-intersecting intervals sweep
17.03.2025
easy2206. Divide Array Into Equal Pairs
All numbers in pairs
13.03.2025
medium3356. Zero Array Transformation II
Min queries (l..r, v) to decrease nums by v to 0 sweep
02.03.2025
easy2570. Merge Two 2D Arrays by Summing Values
Merge two ascending [key, value] lists
20.02.2025
medium1980. Find Unique Binary String
Binary number not in a set
09.02.2025
medium2364. Count Number of Bad Pairs
Count pairs a[i] - a[j] != j - i
06.02.2025
medium1726. Tuple with Same Product
Count uniq 4-tupples a b=c d
27.01.2025
medium1462. Course Schedule IV
All innodes for each query in graph warshall
25.01.2025
medium2948. Make Lexicographically Smallest Array by Swapping Elements
Sort by swapping ab, where abs(a - b) < limit
24.01.2025
medium802. Find Eventual Safe States
Nodes without cycles
05.01.2025
medium2381. Shifting Letters II
Apply from..to, direction shifts to string chars sweep
27.12.2024
medium1014. Best Sightseeing Pair
Max (a[i] + a[j] + i - j), i > j
24.12.2024
hard3203. Find Minimum Diameter After Merging Two Trees
Diameter of 2 connected trees
23.12.2024
medium2471. Minimum Number of Operations to Sort a Binary Tree by Level
Min swaps to sort tree layers
19.12.2024
medium769. Max Chunks To Make Sorted
Maximum independent chunks to merge-sort array
17.12.2024
medium2182. Construct String With Repeat Limit
Max lexical ordered with repeat limit string sort
13.12.2024
medium2593. Find Score of an Array After Marking All Elements
Sum of minimums in order excluding siblings stack
11.12.2024
medium2779. Maximum Beauty of an Array After Applying Operation
Max equal nums after adjusting to [-k..+k] search sweep
08.12.2024
medium2054. Two Best Non-Overlapping Events
Max two non-overlapping intervals search
06.12.2024
medium2554. Maximum Number of Integers to Choose From a Range I
Sum 1..n excluding banned until maxSum
01.12.2024
easy1346. Check If N and Its Double Exist
Any i != j && a[i] = 2 a[j]
28.11.2024
hard2290. Minimum Obstacle Removal to Reach Corner
Min removals to travel first-last in 2D grid
12.11.2024
medium2070. Most Beautiful Item for Each Query
Queries of max beauty for q[i] price search
31.10.2024
hard2463. Minimum Total Distance Traveled
Min robots[x] travel to factories[x,capacity] programming
25.10.2024
medium1233. Remove Sub-Folders from the Filesystem
Remove empty subfolders
15.10.2024
medium2938. Separate Black and White Balls
Min moves to sort 01 string
12.10.2024
medium2406. Divide Intervals Into Minimum Number of Groups
Count non-intersecting groups of intervals
11.10.2024
medium1942. The Number of the Smallest Unoccupied Chair
target's chair number when chairs reused by multiple [arrival, leave] times
10.10.2024
medium962. Maximum Width Ramp
Max j-i between a[i] <= a[j] in an array stack
04.10.2024
medium2491. Divide Players Into Teams of Equal Skill
Sum of products of pairs with equal sums
27.09.2024
medium731. My Calendar II
Add intervals intersecting less than two times sweep
18.09.2024
medium179. Largest Number
Concatenate nums to largest number
03.08.2024
easy1460. Make Two Arrays Equal by Reversing Subarrays
Can arr transform to target by rotating subarrays?
30.07.2024
medium1653. Minimum Deletions to Make String Balanced
Min removals to sort 'ab' string
25.07.2024
medium912. Sort an Array
Sort array using minimum memory
24.07.2024
medium2191. Sort the Jumbled Numbers
Sort array by digits mapping
23.07.2024
easy1636. Sort Array by Increasing Frequency
Sort by frequency or descending
22.07.2024
easy2418. Sort the People
Sort one array by another
13.07.2024
hard2751. Robot Collisions
D dimensional robots fight
2.07.2024
easy350. Intersection of Two Arrays II
Array intersection with duplicates
29.06.2024
medium2192. All Ancestors of a Node in a Directed Acyclic Graph
List of ancestors in a DAG
28.06.2024
medium2285. Maximum Total Importance of Roads
Sort graph by siblings and compute sum(i s)
18.06.2024
medium826. Most Profit Assigning Work
Max profit by assigning [profit, difficulty] to workers any times
15.06.2024
hard502. IPO
Max capital by choosing k jobs with profits[] & capital[] given w on start
14.06.2024
medium945. Minimum Increment to Make Array Unique
Min increments to make items unique
12.06.2024
medium75. Sort Colors
Sort 012 array
11.06.2024
easy1122. Relative Sort Array
Sort an array by the given order
10.06.2024
easy1051. Height Checker
Count unsorted elements in array
06.06.2024
medium846. Hand of Straights
Can array be split into consecutive groups
27.05.2024
easy1608. Special Array With X Elements Greater Than or Equal X
Count of more or equal nums[i] equal itself
11.05.2024
hard857. Minimum Cost to Hire K Workers
Min cost of k workers each doing quality[i] work for fair rate
09.05.2024
medium3075. Maximize Happiness of Selected Children
Sum of k maximums decreasing each step
02.05.2024
easy2441. Largest Positive Integer That Exists With Its Negative
Max number that has its negative in array pointers
23.04.2024
medium310. Minimum Height Trees
Center of an acyclic graph
10.04.2024
medium950. Reveal Cards In Increasing Order
Sort cards by rules: take top, next goes bottom
19.03.2024
medium621. Task Scheduler
Count CPU cycles if task can't run twice in n cycles
18.03.2024
medium452. Minimum Number of Arrows to Burst Balloons
Count non-intersecting intervals
11.03.2024
medium791. Custom Sort String
Construct string from s using order
10.03.2024
easy349. Intersection of Two Arrays
Intersection of two nums arrays
04.03.2024
medium948. Bag of Tokens
Max score converting power to token[i] and token[i] to score.
17.02.2024
medium1642. Furthest Building You Can Reach
Max index to climb diff = a[i +1] - a[i] > 0 using bricks -= diff and ladders-- for each.
16.02.2024
medium1481. Least Number of Unique Integers after K Removals
Min uniq count after removing k numbers.
09.02.2024
medium368. Largest Divisible Subset
Longest subset of divisible by s[i] % s[j] == 0 || s[j] % s[i] == 0.
07.02.2024
medium451. Sort Characters By Frequency
Sort string by char's frequencies.
01.02.2024
medium2966. Divide Array Into Arrays With Max Difference
Split array into tripples with at most k difference.
6.01.2024
hard1235. Maximum Profit in Job Scheduling
Max profit in non-intersecting jobs given startTime[], endTime[] and profit[].
1.01.2024
easy455. Assign Cookies
Max count of greedy children g[Int] to assign cookies with sizes s[Int].
21.12.2023
easy1637. Widest Vertical Area Between Two Points Containing No Points
Max x window between xy points
18.12.2023
easy1913. Maximum Product Difference Between Two Pairs
max second max - min second min
12.12.2023
easy1464. Maximum Product of Two Elements in an Array
#### Join me on Telegram
26.11.2023
medium1727. Largest Submatrix With Rearrangements
Max area of 1 submatrix after sorting columns optimally
23.11.2023
medium1630. Arithmetic Subarrays
Query array ranges can form arithmetic sequence
22.11.2023
medium1424. Diagonal Traverse II
Diagonal 2D matrix order with prunes
18.11.2023
medium1838. Frequency of the Most Frequent Element
Max count of equal numbers if increment arr[i] k times
16.11.2023
medium1980. Find Unique Binary String
First absent number in a binary string array
13.11.2023
medium2785. Sort Vowels in a String
Sort vowels in a string
30.10.2023
easy1356. Sort Integers by The Number of 1 Bits
Sort an array comparing by bit count and value
26.10.2023
medium823. Binary Trees With Factors
Number of trees from arr where each k node has i and j leafs arr[k]=arr[j] arr[i]
11.10.2023
hard2251. Number of Flowers in Full Bloom
Array of counts of segments in intersection
10.10.2023
hard2009. Minimum Number of Operations to Make Array Continuous
Min replacements to make array continuous a[i] = a[i - 1] + 1
28.09.2023
easy905. Sort Array By Parity
Sort an array by even-odd
18.09.2023
easy1337. The K Weakest Rows in a Matrix
k indices with smallest row sum in a binary matrix
12.09.2023
medium1647. Minimum Deletions to Make Character Frequencies Unique
Minimum removes duplicate frequency chars from string
31.08.2023
hard1326. Minimum Number of Taps to Open to Water a Garden
Fill all space between 0..n using minimum intervals
30.08.2023
hard2366. Minimum Replacements to Sort the Array
Minimum number of number splits to make an array non-decreasing
23.08.2023
medium767. Reorganize String
Create non repeated subsequent chars string from string
20.08.2023
hard1203. Sort Items by Groups Respecting Dependencies
Sort items by groups and in groups given dependencies.
19.08.2023
hard1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
List of list of must-have edges and list of optional edges for Minimum Weight Minimum Spanning Tree
19.07.2023
medium435. Non-overlapping Intervals
Minimum intervals to erase overlap
13.07.2023
medium207. Course Schedule
If none edges in a cycle
17.06.2023
hard1187. Make Array Strictly Increasing
Minimum replacements to make arr1 increasing using any numbers arr2
06.06.2023
easy1502. Can Make Arithmetic Progression From Sequence
Is IntArray can be arithmetic progression?
24.05.2023
medium2542. Maximum Subsequence Score
Max score of k sum(subsequence(a)) min(subsequence(b))
22.05.2023
medium347. Top K Frequent Elements
First k unique elements sorted by frequency.
6.05.2023
medium1498. Number of Subsequences That Satisfy the Given Sum Condition
Precompute the 2-powers
29.04.2023
hard1697. Checking Existence of Edge Length Limited Paths
for better time complexity, compress the Union-Find path uf[x] = n
03.04.2023
medium881. Boats to Save People
Sort an array and move two pointers lo and hi.
02.04.2023
medium2300. Successful Pairs of Spells and Potions
sort potions
29.03.2023
hard1402. Reducing Dishes
The naive $$O(n^2)$$ solution will work. However, there is an optimal one if we simply go from the end.
01.03.2023
medium912. Sort an Array
Let's just implement naive quicksort.
2.02.2023
easy953. Verifying an Alien Dictionary
Just translate and then sort and compare. (But we can also just scan linearly and compare).
31.01.2023
medium1626. Best Team With No Conflicts
We can use DFS to search all the possible teams and memorize the result in dp cache.
6.01.2023
medium1833. Maximum Ice Cream Bars
fun maxIceCream(costs: IntArray, coins: Int): Int {
5.01.2023
medium452. Minimum Number of Arrows to Burst Balloons
fun findMinArrowShots(points: Array
25.12.2022
easy2389. Longest Subsequence With Limited Sum
fun answerQueries(nums: IntArray, queries: IntArray): IntArray {
3.12.2022
medium451. Sort Characters By Frequency
fun frequencySort(s: String): String =
17.11.2022
mediumRectangle Area
class Solution {
6.11.2022
hardOrderly Queue
One idea that come to my mind is: if k >= 2 then you basically can swap any adjacent elements. That means you can actually sort all the characters.