Week3‐Week4 - Joanna-0427/leetcode GitHub Wiki
Problem 1: Post Format Validator
You are managing a social media platform and need to ensure that posts are properly formatted. Each post must have balanced and correctly nested tags, such as () for mentions, [] for hashtags, and {} for links. You are given a string representing a post's content, and your task is to determine if the tags in the post are correctly formatted.
A post is considered valid if:
Every opening tag has a corresponding closing tag of the same type. Tags are closed in the correct order. def is_valid_post_format(posts): pass Example Usage:
print(is_valid_post_format("()")) print(is_valid_post_format("()[]{}")) print(is_valid_post_format("(]"))
Problem 2: Reverse User Comments Queue
On your platform, comments on posts are displayed in the order they are received. However, for a special feature, you need to reverse the order of comments before displaying them. Given a queue of comments represented as a list of strings, reverse the order using a stack.
def reverse_comments_queue(comments): pass Example Usage:
print(reverse_comments_queue(["Great post!", "Love it!", "Thanks for sharing."]))
print(reverse_comments_queue(["First!", "Interesting read.", "Well written."]))
Problem 3: Check Symmetry in Post Titles
As part of a new feature on your social media platform, you want to highlight post titles that are symmetrical, meaning they read the same forwards and backwards when ignoring spaces, punctuation, and case. Given a post title as a string, use a new algorithmic technique the two-pointer method to determine if the title is symmetrical.
def is_symmetrical_title(title): pass Example Usage:
print(is_symmetrical_title("A Santa at NASA")) print(is_symmetrical_title("Social Media"))
Problem 4: Engagement Boost
You track your daily engagement rates as a list of integers, sorted in non-decreasing order. To analyze the impact of certain strategies, you decide to square each engagement rate and then sort the results in non-decreasing order.
Given an integer array engagements sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Modify the solution below to use the two-pointer technique.
Example Usage:
print(engagement_boost([-4, -1, 0, 3, 10])) print(engagement_boost([-7, -3, 2, 3, 11]))
Problem 1: Time Needed to Stream Movies There are n users in a queue waiting to stream their favorite movies, where the 0th user is at the front of the queue and the (n - 1)th user is at the back of the queue.
You are given a 0-indexed integer array movies of length n where the number of movies that the ith user would like to stream is movies[i].
Each user takes exactly 1 second to stream a movie. A user can only stream 1 movie at a time and has to go back to the end of the queue (which happens instantaneously) in order to stream more movies. If a user does not have any movies left to stream, they will leave the queue.
Return the time taken for the user at position k (0-indexed) to finish streaming all their movies.
def time_required_to_stream(movies, k): pass Example Usage:
print(time_required_to_stream([2, 3, 2], 2)) print(time_required_to_stream([5, 1, 1, 1], 0))
Problem 8: Find Closest NFT Values
Buyers often look for NFTs that are closest in value to their budget. Given a sorted list of NFT values and a budget, you need to find the two NFT values that are closest to the given budget: one that is just below or equal to the budget and one that is just above or equal to the budget. If an exact match exists, it should be included as one of the values.
Write the find_closest_nft_values() function, which takes a sorted list of NFT values and a budget, and returns the pair of the two closest NFT values.
Evaluate the time and space complexity of your solution. Define your variables and provide a rationale for why you believe your solution has the stated time and space complexity.