Assignment 4 (Stack)
Assignment 4 (Stack)
Easy:
Q1.
Given a valid parentheses string s, consider its primitive decomposition: s = P1 + P2 +
... + Pk, where Pi are primitive valid parentheses strings.
A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are
valid parentheses strings, and + represents string concatenation.
For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
A valid parentheses string s is primitive if it is nonempty, and there does not exist a
way to split it into s = A + B, with A and B nonempty valid parentheses strings.
Return s after removing the outermost parentheses of every primitive string in the
primitive decomposition of s.
Example 1:
Input: s = "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
Example 2:
Input: s = "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
Example 3:
Input: s = "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".
2.
You can apply some operations to this string where, in one operation, you can remove any
occurrence of one of the substrings "AB" or "CD" from s.
Return the minimum possible length of the resulting string that you can obtain.
Note that the string concatenates after removing the substring and could produce new "AB"
or "CD" substrings.
Example 1:
Input: s = "ABFCACDB"
Output: 2
Explanation: We can do the following operations:
- Remove the substring "ABFCACDB", so s = "FCACDB".
- Remove the substring "FCACDB", so s = "FCAB".
- Remove the substring "FCAB", so s = "FC".
So the resulting length of the string is 2.
It can be shown that it is the minimum length that we can obtain.
Example 2:
Input: s = "ACBBD"
Output: 5
Explanation: We cannot do any operations on the string so the length remains the same.
3.
A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1]
where:
To make the string good, you can choose two adjacent characters that make the string bad
and remove them. You can keep doing this until the string becomes good.
Return the string after making it good. The answer is guaranteed to be unique under the
given constraints.
Example 1:
Input: s = "leEeetcode"
Output: "leetcode"
Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to
be reduced to "leetcode".
Example 2:
Input: s = "abBAcC"
Output: ""
Explanation: We have many possible scenarios, and all lead to the same answer. For
example:
Example 3:
Input: s = "s"
Output: "s"
Medium:
Q4.
You are given an integer array arr of length n that represents a permutation of the
integers in the range [0, n - 1].
We split arr into some number of chunks (i.e., partitions), and individually sort each
chunk. After concatenating them, the result should equal the sorted array.
Return the largest number of chunks we can make to sort the array.
Example 1:
Example 2:
Q5.
Given a string containing just the characters '(' and ')', return the length of the longest
valid (well-formed) parentheses substring.
Example 1:
Input: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".
Example 2:
Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".
Example 3:
Input: s = ""
Output: 0
6.
Given string num representing a non-negative integer num, and an integer k, return the
smallest possible integer after removing k digits from num.
Example 1:
Example 2:
Example 3:
7.
Given an integer array nums and a positive integer k, return the most competitive
subsequence of nums of size k.
We define that a subsequence a is more competitive than a subsequence b (of the same
length) if in the first position where a and b differ, subsequence a has a number less than the
corresponding number in b. For example, [1,3,4] is more competitive than [1,3,5]
because the first position they differ is at the final number, and 4 is less than 5.
Example 1:
Example 2:
Hard.
Q8.
Given n non-negative integers representing an elevation map where the width of each bar
is 1, compute how much water it can trap after raining.
Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Example 2:
Output: 9
Q9.
Given an array of integers heights representing the histogram's bar height where the
width of each bar is 1, return the area of the largest rectangle in the histogram.
Example 1:
Output: 10
The largest rectangle is shown in the red area, which has an area = 10 units.
Example 2:
Input: heights = [2,4]
Output: 4
10.
There is a computer that can run an unlimited number of tasks at the same time. You are given a
2D integer array tasks where tasks[i] = [starti, endi, durationi] indicates that the
ith task should run for a total of durationi seconds (not necessarily continuous) within the
inclusive time range [starti, endi].
You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle.
Return the minimum time during which the computer should be turned on to complete all tasks.
Example 1:
Output: 2
Explanation:
- The first task can be run in the inclusive time range [2, 2].
- The second task can be run in the inclusive time range [5, 5].
- The third task can be run in the two inclusive time ranges [2, 2] and [5, 5].
Example 2:
Output: 4
Explanation:
- The first task can be run in the inclusive time range [2, 3].
- The second task can be run in the inclusive time ranges [2, 3] and [5, 5].
- The third task can be run in the two inclusive time range [5, 6].