Skip to main content

Elevator Requests IV - Video Solutions

Easy

Leetcode : 4030. Check ASCII Palindromic ( Weekly Contest 516 )

3 video solutions available

Elevator Requests IV - Video Solution

Watch 3 video solutions for Elevator Requests IV, a easy level problem. This walkthrough by Web Fusion with Bharadwaj has 44 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

You are given a string s consisting of lowercase English letters.

Construct a binary string by replacing each character in s with the 8-bit binary representation of its ASCII value, including leading zeros, while preserving the original order of the characters.

Return true if the resulting binary string is a palindrome. Otherwise, return false.

 

Example 1:

Input: s = "ff"

Output: true

Explanation:

  • The ASCII value of f is 102, whose 8-bit binary representation is 01100110.
  • Thus, the binary string is 0110011001100110.
  • Since this binary string is a palindrome, the output is true.

Example 2:

Input: s = "leet"

Output: false

Explanation:

  • The ASCII values of l, e, e, and t are 108, 101, 101, and 116, respectively.
  • Their 8-bit binary representations are 01101100, 01100101, 01100101, and 01110100.
  • Thus, the binary string is 01101100011001010110010101110100.
  • Since this binary string is not a palindrome, the output is false.

 

Constraints:

  • 1 <= s.length <= 100
  • s consists of lowercase English letters.
Read full problem with examples

Approach Overview

Problem Overview: You're given a set of elevator requests, each with a start floor, destination floor, and time, and you need to schedule a set of elevators to minimize the total waiting time or completion time. The optimal solution must efficiently assign requests to elevators while respecting their movement constraints.

Approach 1: Brute Force (O(n^2) time, O(1) space)

Simulate every possible assignment of requests to elevators. For each request, try all elevators and check which one finishes earliest based on current position and direction. This works for tiny inputs but explodes as n grows because you iterate through all elevators for every request. It's a good starting point to verify correctness but never passes large test cases.

Approach 2: Greedy with Sorting (O(n log n) time, O(n) space)

Sort requests by arrival time and process them in order. Maintain each elevator's current floor and next available time. For each request, pick the elevator that can reach the start floor earliest (considering travel time from its current position). This is a classic greedy strategy for interval scheduling and often passes most test cases. The key insight is that you only need to compare the earliest available elevator—no need to try all combinations. However, it fails when future requests could be better served by waiting for a closer elevator, so it's not optimal in all cases.

Approach 3: Optimal with Priority Queue (O(n log n) time, O(n) space)

Use a min-heap to track each elevator's ready time and the request they're handling. Instead of scanning all elevators, you pop the elevator with the earliest available time and assign the current request to it, then push it back with its new availability. This gives a globally optimal schedule when the cost function is additive and each request is independent. The trick is to treat the elevator as a resource that becomes free after serving a request, and the heap keeps the next free elevator at the top. This approach is what interviewers expect—it shows you understand priority queues and greedy scheduling.

Recommended for interviews: The optimal priority queue solution is the one to present. Brute force shows you can reason about the problem structure, but the heap-based method demonstrates you can apply classic algorithms (greedy + priority queue) to real-world constraints. Start with brute force to confirm understanding, then jump to the optimal solution. Mention that the greedy solution fails for certain corner cases where a longer wait for a closer elevator beats picking the first free one, but the heap approach handles that by always choosing the earliest available elevator.

Relevant topics to strengthen your understanding: greedy algorithms, priority queues, and scheduling.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute ForceO(n²)O(1)Small inputs (n ≤ 100), edge case testing
Greedy (Sort + Earliest)O(n log n)O(n)When requests are independent and elevators identical
Priority Queue (Optimal)O(n log n)O(n)General case, handles overlapping requests, best for interviews