Stacks, Queues and Monotonicity.pdf
1.5 MB
โ
A2SV Community Education Resources.๐
๐ข Very good resources for A2SV interview prep.
๐๐ @AceCoding Presents! ๐๐
๐ข Very good resources for A2SV interview prep.
๐๐ @AceCoding Presents! ๐๐
๐1
Here are the resources for the topics covered so far. We've also linked them in the Resources tab of the Community Sheet
โข Python Learning Path
โข Time and Space Complexity Learning Path
โข Sorting Learning Path
โข Stack and Queue Learning Path
โข Recursion Learning Path
โข LinkedList Learning Path
โข Sliding Window and Two Pointers Learning Path
โข Prefix Sum Learning Path
โข Heaps Learning Path
๐๐ @AceCoding Presents! ๐๐
โข Python Learning Path
โข Time and Space Complexity Learning Path
โข Sorting Learning Path
โข Stack and Queue Learning Path
โข Recursion Learning Path
โข LinkedList Learning Path
โข Sliding Window and Two Pointers Learning Path
โข Prefix Sum Learning Path
โข Heaps Learning Path
๐๐ @AceCoding Presents! ๐๐
Google Docs
A2SV Community - Python Learning Path
Make a copy of this doc and go on at your own pace Python Learning Path Introduction
W3Schools GeeksforGeeks YouTube Python Doc Work on these questions from the sheet Basic Data Types
W3Schools GeeksforGeeks YouTube Python Doc Work on these questionsโฆ
W3Schools GeeksforGeeks YouTube Python Doc Work on these questions from the sheet Basic Data Types
W3Schools GeeksforGeeks YouTube Python Doc Work on these questionsโฆ
๐ข Important Update for A2SV G6 In person Education Cohort!
If you've received an email from A2SV, congratulations! ๐ Youโve been shortlisted for an interview for the A2SV G6 Cohort!
๐ Interviews Begin: on Tuesday
๐ Focus Areas: Make sure to prepare thoroughly on the topics mentioned in the email.
If you havenโt checked your email yet, go check it now and start your preparation! ๐จ
๐ @AceCoding Presents! ๐
If you've received an email from A2SV, congratulations! ๐ Youโve been shortlisted for an interview for the A2SV G6 Cohort!
๐ Interviews Begin: on Tuesday
๐ Focus Areas: Make sure to prepare thoroughly on the topics mentioned in the email.
If you havenโt checked your email yet, go check it now and start your preparation! ๐จ
๐ @AceCoding Presents! ๐
๐ง Python Quiz: Predict the Output
What will the output be? You can choose your answer below.
@AceCoding Presents!
arr = [[0]] * 5
print(arr)
arr[0][0] = 1
print(arr)
What will the output be? You can choose your answer below.
@AceCoding Presents!
For the above question.
Anonymous Quiz
53%
1๏ธโฃ Option A: [[0], [0], [0], [0], [0]] and [[1], [0], [0], [0], [0]]
25%
2๏ธโฃ Option B: [[0], [0], [0], [0], [0]] and [[1], [1], [1], [1], [1]]
9%
3๏ธโฃ Option C: [[0]] [[1]]
13%
4๏ธโฃ Option D: [[0], [0], [0], [0], [0]] Error: List index out of range
๐5
โ
Sliding window, Two pointers, and Hash Table problem
๐ก 567. Permutation in String
https://leetcode.com/problems/permutation-in-string/submissions/1482104785/
๐ @AceCoding Presents! ๐
๐ก 567. Permutation in String
https://leetcode.com/problems/permutation-in-string/submissions/1482104785/
๐ @AceCoding Presents! ๐
โ
Dynamic Sliding window and hashmap / hashset problem.
This problem has a pattern that repeats a lot : worth revising
๐ก 3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
๐ @AceCoding Presents! ๐
This problem has a pattern that repeats a lot : worth revising
๐ก 3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
๐ @AceCoding Presents! ๐
๐1
Substrings that begin and end with the same letter
You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "abcba"
Output: 7
Explanation:
The substrings of length 1 that start and end with the same letter are: "a", "b", "c", "b", and "a".
The substring of length 3 that starts and ends with the same letter is: "bcb".
The substring of length 5 that starts and ends with the same letter is: "abcba" .
Example 2:
Input: s = "abacad"
Output: 9 112131
Explanation:
The substrings of length 1 that start and end with the same letter are: "a", "b", "a", "c", "a", and "d" = 6.
The substrings of length 3 that start and end with the same letter are: "aba" and "aca" = 2.
The substring of length 5 that starts and ends with the same letter is: "abaca" = 1.
Example 3:
Input: s = "a"
Output: 1
Explanation:
The substring of length 1 that starts and ends with the same letter is: "a".
Constraints:
1 <= s.length <= 105
s consists only of lowercase English letters.
You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character.
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "abcba"
Output: 7
Explanation:
The substrings of length 1 that start and end with the same letter are: "a", "b", "c", "b", and "a".
The substring of length 3 that starts and ends with the same letter is: "bcb".
The substring of length 5 that starts and ends with the same letter is: "abcba" .
Example 2:
Input: s = "abacad"
Output: 9 112131
Explanation:
The substrings of length 1 that start and end with the same letter are: "a", "b", "a", "c", "a", and "d" = 6.
The substrings of length 3 that start and end with the same letter are: "aba" and "aca" = 2.
The substring of length 5 that starts and ends with the same letter is: "abaca" = 1.
Example 3:
Input: s = "a"
Output: 1
Explanation:
The substring of length 1 that starts and ends with the same letter is: "a".
Constraints:
1 <= s.length <= 105
s consists only of lowercase English letters.
< Ace Coding /> ๐
Substrings that begin and end with the same letter You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character. A substring is a contiguous non-empty sequenceโฆ
question from today interview question
Question: Maximum Sum Score of an Array
You are given a 0-indexed integer array
โข The sum of the first
โข The sum of the last
Your task is to return the maximum sum score of
โExamples:
1. Input:
Output:
Explanation:
โข At index 0: max(4, 4 + 3 - 2 + 5) = max(4, 10) = 10.
โข At index 1: max(4 + 3, 3 - 2 + 5) = max(7, 6) = 7.
โข At index 2: max(4 + 3 - 2, -2 + 5) = max(5, 3) = 5.
โข At index 3: max(4 + 3 - 2 + 5, 5) = max(10, 5) = 10.
โข The maximum sum score of
2. Input:
Output:
Explanation:
โข At index 0: max(-3, -3 - 5) = max(-3, -8) = -3.
โข At index 1: max(-3 - 5, -5) = max(-8, -5) = -5.
โข The maximum sum score of
โChallenge:
Write a function
You are given a 0-indexed integer array
nums of length n. The sum score of nums at an index i (where 0 <= i < n) is defined as the maximum of:โข The sum of the first
i + 1 elements of nums.โข The sum of the last
n - i elements of nums.Your task is to return the maximum sum score of
nums at any index.โExamples:
1. Input:
nums = [4, 3, -2, 5] Output:
10 Explanation:
โข At index 0: max(4, 4 + 3 - 2 + 5) = max(4, 10) = 10.
โข At index 1: max(4 + 3, 3 - 2 + 5) = max(7, 6) = 7.
โข At index 2: max(4 + 3 - 2, -2 + 5) = max(5, 3) = 5.
โข At index 3: max(4 + 3 - 2 + 5, 5) = max(10, 5) = 10.
โข The maximum sum score of
nums is 10.2. Input:
nums = [-3, -5] Output:
-3 Explanation:
โข At index 0: max(-3, -3 - 5) = max(-3, -8) = -3.
โข At index 1: max(-3 - 5, -5) = max(-8, -5) = -5.
โข The maximum sum score of
nums is -3.โChallenge:
Write a function
maximumSumScore(nums) that takes an integer array as input and returns the maximum sum score.
< Ace Coding /> ๐
Question: Maximum Sum Score of an Array You are given a 0-indexed integer array nums of length n. The sum score of nums at an index i (where 0 <= i < n) is defined as the maximum of: โข The sum of the first i + 1 elements of nums. โข The sum of the last nโฆ
Leetcode
2219 - Maximum Sum Score of Array
Welcome to Subscribe On Youtube Formatted question description: https://leetcode.ca/all/2219.html 2219. Maximum Sum Score of Array Description You are given a 0-indexed integer array nums of length n. The sum score of nums at an index i where 0 <= i < n isโฆ
< Ace Coding /> ๐
Substrings that begin and end with the same letter You are given a 0-indexed string s consisting of only lowercase English letters. Return the number of substrings in s that begin and end with the same character. A substring is a contiguous non-empty sequenceโฆ
โ
Free leetcode questions which is 99% similar with the first question :
https://leetcode.com/problems/count-substrings-starting-and-ending-with-given-character/description/
class Solution:
def countSubstrings(self, s: str, c: str) -> int:
count = s.count(c)
return count * (count + 1) // 2
https://leetcode.com/problems/count-substrings-starting-and-ending-with-given-character/description/
LeetCode
Count Substrings Starting and Ending with Given Character - LeetCode
Can you solve this real interview question? Count Substrings Starting and Ending with Given Character - You are given a string s and a character c. Return the total number of substrings of s that start and end with c.
Example 1:
Input: s = "abada", cโฆ
Example 1:
Input: s = "abada", cโฆ
/*
You are given a string s consisting only of lowercase English letters.
We call a substring special if it contains no character which has occurred at least twice (in other words, it does not contain a repeating character).
Your task is to count the number of special substrings.
For example, in the string "pop", the substring "po" is a special substring, however, "pop" is not special (since 'p' has occurred twice).
Return the number of special substrings.
A substring is a contiguous sequence of characters within a string. For example, "abc" is a substring of "abcd", but "acd" is not.
Example 1:
Input: s = "abcd"
Output: 10
Explanation: Since each character occurs once, every substring is a special substring.
We have 4 substrings of length one, 3 of length two, 2 of length three, and 1 substring of length four. So overall there are 4 + 3 + 2 + 1 = 10 special substrings.
Example 2:
Input: s = "ooo"
Output: 3
Explanation: Any substring with a length of at least two contains a repeating character. So we have to count the number of substrings of length one, which is 3.
Example 3:
Input: s = "abab"
Output: 7
Explanation: Special substrings are as follows (sorted by their start positions):
Special substrings of length 1: "a", "b", "a", "b"
Special substrings of length 2: "ab", "ba", "ab"
And it can be shown that there are no special substrings with a length of at least three. So the answer would be 4 + 3 = 7.
l r
a b c d a
Constraints:
1 <= s.length <= 10^5
s consists of lowercase English letters
You are given a string s consisting only of lowercase English letters.
We call a substring special if it contains no character which has occurred at least twice (in other words, it does not contain a repeating character).
Your task is to count the number of special substrings.
For example, in the string "pop", the substring "po" is a special substring, however, "pop" is not special (since 'p' has occurred twice).
Return the number of special substrings.
A substring is a contiguous sequence of characters within a string. For example, "abc" is a substring of "abcd", but "acd" is not.
Example 1:
Input: s = "abcd"
Output: 10
Explanation: Since each character occurs once, every substring is a special substring.
We have 4 substrings of length one, 3 of length two, 2 of length three, and 1 substring of length four. So overall there are 4 + 3 + 2 + 1 = 10 special substrings.
Example 2:
Input: s = "ooo"
Output: 3
Explanation: Any substring with a length of at least two contains a repeating character. So we have to count the number of substrings of length one, which is 3.
Example 3:
Input: s = "abab"
Output: 7
Explanation: Special substrings are as follows (sorted by their start positions):
Special substrings of length 1: "a", "b", "a", "b"
Special substrings of length 2: "ab", "ba", "ab"
And it can be shown that there are no special substrings with a length of at least three. So the answer would be 4 + 3 = 7.
l r
a b c d a
Constraints:
1 <= s.length <= 10^5
s consists of lowercase English letters
< Ace Coding /> ๐
/* You are given a string s consisting only of lowercase English letters. We call a substring special if it contains no character which has occurred at least twice (in other words, it does not contain a repeating character). Your task is to count the numberโฆ
This one took me much longer time not gonna lie.๐ฎโ๐จ
โ Solution : this is as efficient as it can get
โ Solution : this is as efficient as it can get
python
def countSpaceialSubString(s):
seen = set()
l = 0
count = 0
for r in range(len(s)):
if s[r] in seen:
while s[l] != s[r]:
seen.remove(s[l])
l += 1
l += 1
seen.add(s[r])
count += r - l + 1
return count
print(countSpaceialSubString("abcd"))
print(countSpaceialSubString("ooo"))
print(countSpaceialSubString("abab"))
print(countSpaceialSubString("abcabc"))
Question: Can You Make This String a Palindrome?
A palindrome is a string that reads the same forwards and backwards. Given a string, determine if it's possible to rearrange the characters to form a palindrome.
Examples:
1. Input:
โข Output:
โข Explanation: The string is already a palindrome.
2. Input:
โข Output:
โข Explanation: Rearranging the characters can form the palindrome
3. Input:
โข Output:
โข Explanation: No rearrangement can form a palindrome.
4. Input:
โข Output:
โข Explanation: Rearranging the characters can form the palindrome
5. Input:
โข Output:
โข Explanation: The string is already a palindrome.
Challenge:
Write a function that takes a string as input and returns
A palindrome is a string that reads the same forwards and backwards. Given a string, determine if it's possible to rearrange the characters to form a palindrome.
Examples:
1. Input:
"civic"โข Output:
Trueโข Explanation: The string is already a palindrome.
2. Input:
"ivicc"โข Output:
Trueโข Explanation: Rearranging the characters can form the palindrome
"civic".3. Input:
"hello"โข Output:
Falseโข Explanation: No rearrangement can form a palindrome.
4. Input:
"aabbcc"โข Output:
Trueโข Explanation: Rearranging the characters can form the palindrome
"abcba".5. Input:
"racecar"โข Output:
Trueโข Explanation: The string is already a palindrome.
Challenge:
Write a function that takes a string as input and returns
True if the string can be rearranged to form a palindrome, and False otherwise.Question Description
Given a string s and an integer k, return the number of substrings in s of length k with no repeated characters.
Example 1:
Input: s = "unonleetcode", k = 5
Output: 2
Explanation: There are 6 substrings they are: 'havef','avefu','vefun','efuno','etcod','tcode'.
Example 2:
Input: s = "home", k = 5
Output: 0
Explanation: Notice k can be larger than the length of s. In this case, it is not possible to find any substring.
Example 3:
Input: s = "havefunonleetcode", k = 5
Output: 6
Explanation: There are 6 substrings they are: 'havef','avefu','vefun','efuno','etcod','tcode'.
โ Solution: as I have told you this pattern repeats a lot so you got this
def subStringK(s, k):
seen = set()
count = 0
l = 0
for r in range(len(s)):
if s[r] in seen:
while s[l] != s[r]:
seen.remove(s[l])
l += 1
l += 1
if r - l + 1 == k:
count += 1
seen.remove(s[l])
l += 1
seen.add(s[r])
return count
print(subStringK("unonleetcode", 5))
print(subStringK("havefunonleetcode", 5))
print(subStringK("aaabbaaa", 2))
print(subStringK("aaabbaaa", 100))
โ
Hello everyone, today was my interview date, and I was asked the following question: At first, I thought I could use the two pointers technique to solve it, but then I realized that that would make the algorithm inefficient. Then I noticed that the number of 1s will be the length of the subarray with grouped 1s. This changed my approach to a fixed sliding window, and then the rest was easy. My interviewer was very nice and guided me the whole way.
'''
Given a binary array data, return the minimum number of swaps required to group all 1โs
present in the array together in any place in the array.
Example 1:
Input: data = [1,0,1,0,1]
Output: 1
Explanation: There are 3 ways to group all 1's together:
[1,1,1,0,0] using 1 swap.
[0,1,1,1,0] using 2 swaps.
[0,0,1,1,1] using 1 swap.
The minimum is 1.
Example 2:
Input: data = [0,0,0,1,0]
Output: 0
Explanation: Since there is only one 1 in the array, no swaps are needed.
Example 3:
Input: data = [1,0,1,0,1,0,0,1,1,0,1] count_ones = 6 count_zeros = 3 curr_zeros = 3 min of count_zeros and curr_zeros
l
r
time comp = O(n)
space comp = O(1)
Output: 3
Explanation: One possible solution that uses 3 swaps is [0,0,0,0,0,1,1,1,1,1,1].
Constraints:
1 <= data.length <= 10**5
data[i] is either 0 or 1.
'''
"""
1. count 1's store one count_ones
2. assign count_zeros = inf curr_zeros = 0
3. l, r = 0
4. check for a valid window
5. update curr_zeros
6. take the min of the count_zeros and curr_zeros
7. check if the values at the indexes are zeros if so decrement curr_zeros
8. update pointers
9. return count_zeros
"""
# my code
"""
1= 6
curr_zeros = 3
count_zeros = 3
1,0,1,0,1,0,0,1,1,0,1
l
r
"""
#A2SV #a2sv #a2sv2024
A2SV a2sv 2024 In person
๐ @AceCoding Presents! ๐
'''
Given a binary array data, return the minimum number of swaps required to group all 1โs
present in the array together in any place in the array.
Example 1:
Input: data = [1,0,1,0,1]
Output: 1
Explanation: There are 3 ways to group all 1's together:
[1,1,1,0,0] using 1 swap.
[0,1,1,1,0] using 2 swaps.
[0,0,1,1,1] using 1 swap.
The minimum is 1.
Example 2:
Input: data = [0,0,0,1,0]
Output: 0
Explanation: Since there is only one 1 in the array, no swaps are needed.
Example 3:
Input: data = [1,0,1,0,1,0,0,1,1,0,1] count_ones = 6 count_zeros = 3 curr_zeros = 3 min of count_zeros and curr_zeros
l
r
time comp = O(n)
space comp = O(1)
Output: 3
Explanation: One possible solution that uses 3 swaps is [0,0,0,0,0,1,1,1,1,1,1].
Constraints:
1 <= data.length <= 10**5
data[i] is either 0 or 1.
'''
"""
1. count 1's store one count_ones
2. assign count_zeros = inf curr_zeros = 0
3. l, r = 0
4. check for a valid window
5. update curr_zeros
6. take the min of the count_zeros and curr_zeros
7. check if the values at the indexes are zeros if so decrement curr_zeros
8. update pointers
9. return count_zeros
"""
# my code
def minNumberOfSwaps(arr):
count_ones = arr.count(1)
count_zeros, curr_zeros = float('inf'), 0
l = 0
for r in range(len(arr)):
if arr[r] == 0:
curr_zeros += 1
# check for a valid window
if r - l + 1 == count_ones:
count_zeros = min(count_zeros, curr_zeros)
if arr[l] == 0:
curr_zeros -= 1
l += 1
return count_zeros if count_zeros != float('inf') else 0
"""
1= 6
curr_zeros = 3
count_zeros = 3
1,0,1,0,1,0,0,1,1,0,1
l
r
"""
#A2SV #a2sv #a2sv2024
A2SV a2sv 2024 In person
๐ @AceCoding Presents! ๐
๐9
"""
You are given a string s consisting only of the letters 'a' and 'b', and an integer k.
What is the minimum number of characters you need to change to obtain a substring of length โฅ k where all characters are the same?
Example 1:
s = โaabaabaaโ, k = 3
Output: 1
Explanation: s can be transformed to โaaaaabaaโ
Example 2:
s = โbbabbabaโ, k = 8
Output: 3
Explanation: s can be transformed to โbbbbbbbbโ
Constraints:
1 <= s.length <= 10^5
1 <= k <= s.length
aaab, k = 4
aaaa
abs(3 - 1) = 2
"""
You are given a string s consisting only of the letters 'a' and 'b', and an integer k.
What is the minimum number of characters you need to change to obtain a substring of length โฅ k where all characters are the same?
Example 1:
s = โaabaabaaโ, k = 3
Output: 1
Explanation: s can be transformed to โaaaaabaaโ
Example 2:
s = โbbabbabaโ, k = 8
Output: 3
Explanation: s can be transformed to โbbbbbbbbโ
Constraints:
1 <= s.length <= 10^5
1 <= k <= s.length
aaab, k = 4
aaaa
abs(3 - 1) = 2
"""
๐3