Segment Tree LeetCode Problems: All 41, With Python Solutions

Every problem in this library that LeetCode tags Segment Tree 41 in total, 33 of them with a complete Python solution, a worked example and the time and space complexity of the approach.

  • 41 problems
  • 0 Easy
  • 11 Medium
  • 30 Hard

Segment Tree problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Medium (11)

#ProblemDifficultyTopics
307Range Sum Query - MutableMediumDesign, Binary Indexed Tree, Segment Tree +2
406Queue Reconstruction by HeightMediumBinary Indexed Tree, Segment Tree, Array +1
673Number of Longest Increasing SubsequenceMediumBinary Indexed Tree, Segment Tree, Array +1
729My Calendar IMediumDesign, Segment Tree, Array +2
731My Calendar IIMediumDesign, Segment Tree, Array +3
1395Count Number of TeamsMediumBinary Indexed Tree, Segment Tree, Array +1
2080Range Frequency QueriesMediumDesign, Segment Tree, Array +2
2424Longest Uploaded PrefixMediumUnion Find, Design, Binary Indexed Tree +5
308Range Sum Query 2D - MutablePremiumMediumDesign, Binary Indexed Tree, Segment Tree +2
2031Count Subarrays With More Ones Than ZerosPremiumMediumBinary Indexed Tree, Segment Tree, Array +5
2907Maximum Profitable Triplets With Increasing Prices IPremiumMediumBinary Indexed Tree, Segment Tree, Array

Hard (30)

#ProblemDifficultyTopics
218The Skyline ProblemHardBinary Indexed Tree, Segment Tree, Array +5
315Count of Smaller Numbers After SelfHardBinary Indexed Tree, Segment Tree, Array +4
327Count of Range SumHardBinary Indexed Tree, Segment Tree, Array +4
493Reverse PairsHardBinary Indexed Tree, Segment Tree, Array +4
699Falling SquaresHardSegment Tree, Array, Ordered Set
715Range ModuleHardDesign, Segment Tree, Ordered Set
732My Calendar IIIHardDesign, Segment Tree, Binary Search +2
850Rectangle Area IIHardSegment Tree, Array, Ordered Set +1
1157Online Majority Element In SubarrayHardDesign, Binary Indexed Tree, Segment Tree +2
1505Minimum Possible Integer After at Most K Adjacent Swaps On DigitsHardGreedy, Binary Indexed Tree, Segment Tree +1
1521Find a Value of a Mysterious Function Closest to TargetHardBit Manipulation, Segment Tree, Array +1
1622Fancy SequenceHardDesign, Segment Tree, Math
1649Create Sorted Array through InstructionsHardBinary Indexed Tree, Segment Tree, Array +4
1687Delivering Boxes from Storage to PortsHardSegment Tree, Queue, Array +4
2179Count Good Triplets in an ArrayHardBinary Indexed Tree, Segment Tree, Array +4
2213Longest Substring of One Repeating CharacterHardSegment Tree, Array, String +1
2276Count Integers in IntervalsHardDesign, Segment Tree, Ordered Set
2286Booking Concert Tickets in GroupsHardDesign, Binary Indexed Tree, Segment Tree +1
2407Longest Increasing Subsequence IIHardBinary Indexed Tree, Segment Tree, Queue +4
2426Number of Pairs Satisfying InequalityHardBinary Indexed Tree, Segment Tree, Array +4
2569Handling Sum Queries After UpdateHardSegment Tree, Array
2659Make Array EmptyHardGreedy, Binary Indexed Tree, Segment Tree +4
2736Maximum Sum QueriesHardStack, Binary Indexed Tree, Segment Tree +4
2926Maximum Balanced Subsequence SumHardBinary Indexed Tree, Segment Tree, Array +2
2940Find Building Where Alice and Bob Can MeetHardStack, Binary Indexed Tree, Segment Tree +4
683K Empty SlotsPremiumHardBinary Indexed Tree, Segment Tree, Queue +5
2158Amount of New Area Painted Each DayPremiumHardSegment Tree, Array, Ordered Set
2519Count the Number of K-Big IndicesPremiumHardBinary Indexed Tree, Segment Tree, Array +4
2916Subarrays Distinct Element Sum of Squares IIHardBinary Indexed Tree, Segment Tree, Array +1
2921Maximum Profitable Triplets With Increasing Prices IIPremiumHardBinary Indexed Tree, Segment Tree, Array

When the Segment Tree problem arrives live

Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.