Coding
Coding
Arrays/Strings- 1
https://youtu.be/xj5Xd_Nfm24
https://youtu.be/WOZo3IeWE98
https://youtu.be/P4rV9qW55Ck
https://youtu.be/n8y0Twfcf-Y
https://youtu.be/QBIe-iXLgAw
https://youtu.be/F6Ng0QJbWs8
https://youtu.be/upkF2hUd5dg
https://youtu.be/zI15jCfADpI
https://youtu.be/2tAyVoiKNew
https://youtu.be/nP-vkiKyUug
13 Hard Trapping Rain Water - LeetCode
14 Hard Candy -
Arrays/Strings- 2
https://youtu.be/Oq4cPtWeP2M
https://youtu.be/qBSXZrU-ncw
https://youtu.be/gzb5H7qF5wg
https://youtu.be/gvy9B1JK-co
https://youtu.be/LKs-pWrm0aQ
https://youtu.be/O5khhibfKME
Matrix
https://youtu.be/lj9cmdPvt44
https://youtu.be/Pdy5SJrv-J8
25 Medium Rotate Image - LeetCode
https://youtu.be/iqoqlHvcZJI
2 Pointers
28 Easy
https://leetcode.com/problems/valid-
https://youtu.be/C6v0dBVrvbw
29 Easy
https://leetcode.com/problems/is-
https://youtu.be/3V96ois4YSs
30 Medium
https://leetcode.com/problems/two-sum-ii-
https://youtu.be/26f-QDKLzMQ
31 Medium
https://leetcode.com/problems/container-
https://youtu.be/JEAcl9zTpzE
32 Medium
https://leetcode.com/problems/3sum
https://youtu.be/5Cz3FzCRVzY
Sliding Window
33 Medium
https://leetcode.com/problems/minimum-
https://youtu.be/5mlk4xFBoWA
34 Medium
https://leetcode.com/problems/longest-
https://youtu.be/UGX8QMSpZ_Q
35 Hard
https://leetcode.com/problems/substring-
36 Hard
https://leetcode.com/problems/minimum-
https://youtu.be/OTadHSqZmOc
Hashmap
37 Easy
https://leetcode.com/problems/ransom-
https://youtu.be/wHTLlMkq3Fc
38 Easy
https://leetcode.com/problems/isomorphi
https://youtu.be/jCz0IgK4yYA
39 Easy
https://leetcode.com/problems/word-
https://youtu.be/mOk_VAT6-oI
40 Easy
https://leetcode.com/problems/valid-
https://youtu.be/z9-TtSvoRIU
41 Easy
https://leetcode.com/problems/two-sum
https://youtu.be/Ux0Fqy0cx9M
42 Medium
https://leetcode.com/problems/group-
https://youtu.be/MxdE6ekmzgk
43 Medium
https://leetcode.com/problems/longest-
https://youtu.be/0PUpzsUqyEM
Stack 44 Easy
https://leetcode.com/problems/valid-
https://youtu.be/xnA2x0wZRlw
45 Easy
https://leetcode.com/problems/next-
https://youtu.be/aloGT1R1JUA
46 Medium
https://leetcode.com/problems/min-stack
https://youtu.be/LGkIEDu7Xc0
47 Medium
https://leetcode.com/problems/simplify-
https://youtu.be/2YmEhXMjrmI
Linked List
48 Easy
https://leetcode.com/problems/linked-list-
https://youtu.be/G56TwaqGa-I
49 Easy
https://leetcode.com/problems/merge-two-
https://youtu.be/NQczlFR3vuo
50 Medium
https://leetcode.com/problems/add-two-
https://youtu.be/Bk3EAQD6r7Q
51 Medium
https://leetcode.com/problems/reverse-
https://youtu.be/X1z7DRZPnUU
52 Medium
https://leetcode.com/problems/rotate-list
https://youtu.be/3R_rVYY9VCA
53 Medium
https://leetcode.com/problems/remove-
https://youtu.be/yLhvHpiNT-s
54 Hard
https://leetcode.com/problems/reverse-
https://youtu.be/kAVzOQeXnUg
Binary Search
55 Easy
https://leetcode.com/problems/search-
https://youtu.be/uBsSov_JRuQ
56 Medium
https://leetcode.com/problems/search-a-
https://youtu.be/68KZeWPwsdk
57 Medium
https://leetcode.com/problems/find-peak-
https://youtu.be/goWqYMiHrIo
58 Medium
https://leetcode.com/problems/search-in-
https://youtu.be/evr5OjXMcgI
59 Hard
https://leetcode.com/problems/median-of-
Kadane's
Algo
60 Medium
https://leetcode.com/problems/maximum-
61 Medium
https://leetcode.com/problems/maximum-
Bit Manipulation
62 Easy
https://leetcode.com/problems/add-binary
https://youtu.be/2JJD8PeKqCE
63 Easy
https://leetcode.com/problems/reverse-
64 Easy
https://leetcode.com/problems/number-of-
https://youtu.be/GJK1FwhljJA
65 Easy
https://leetcode.com/problems/single-
https://youtu.be/ycxJYIfPtu4
Math
66 Easy
https://leetcode.com/problems/palindrom
https://youtu.be/JnGbicu7Jzw
67 Easy
https://leetcode.com/problems/sqrtx
https://youtu.be/KBl2nBVRDI0
68 Medium
https://leetcode.com/problems/powx-n
Backtracking
69 Medium
https://leetcode.com/problems/combinati
70 Medium
https://leetcode.com/problems/permutatio
71 Medium
https://leetcode.com/problems/combinati
72 Medium
https://leetcode.com/problems/n-queens-
73 Medium
https://leetcode.com/problems/letter-
Binary Tree
74 Easy
https://leetcode.com/problems/maximum-
https://youtu.be/hFL_WM5Hl1w
75 Easy
https://leetcode.com/problems/symmetric-
https://youtu.be/WdPZ5osYUbE
76 Easy
https://leetcode.com/problems/path-sum
https://youtu.be/9lmuCOxxBbk
77 Medium
https://leetcode.com/problems/lowest-
https://youtu.be/_zAXYaswt2M
78 Medium
https://leetcode.com/problems/count-
https://youtu.be/uMJGendh0N4
79 Easy
https://leetcode.com/problems/average-
https://youtu.be/AF4hnkbRCkw
80 Medium
https://leetcode.com/problems/binary-
https://youtu.be/4kXtK1bGURU
81 Medium
https://leetcode.com/problems/binary-
https://youtu.be/rgm7lFJMxtg
82 Medium
https://leetcode.com/problems/validate-
https://youtu.be/mHelNOQz4qM
83 Medium
https://leetcode.com/problems/kth-
https://youtu.be/lKTk37knvhw
84 Medium
https://leetcode.com/problems/minimum-
https://youtu.be/HHbMPXYsUuM
Graph General
85 Medium
https://leetcode.com/problems/number-of-
86 Medium
https://leetcode.com/problems/surrounde
87 Medium
https://leetcode.com/problems/clone-
88 Medium
https://leetcode.com/problems/course-
Heap 89 Medium
https://leetcode.com/problems/kth-largest-
90 Hard
https://leetcode.com/problems/find-
1D DP
91 Easy
https://leetcode.com/problems/climbing-
92 Medium
https://leetcode.com/problems/coin-
93 Medium
https://leetcode.com/problems/house-
94 Medium
https://leetcode.com/problems/longest-
95 Medium
https://leetcode.com/problems/word-
Multi Dimensional DP
96 Medium
https://leetcode.com/problems/longest-
97 Medium
https://leetcode.com/problems/minimum-
98 Medium
https://leetcode.com/problems/triangle
99 Hard
https://leetcode.com/problems/best-time-
100 Hard
https://leetcode.com/problems/best-time-
Topic Serial No. Difficulty Level Question Link:
28 Easy
https://leetcode.com/problems/valid-palindrome
29 Easy
https://leetcode.com/problems/is-subsequence
2 Pointers 30 Medium
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted
31 Medium
https://leetcode.com/problems/container-with-most-water
32 Medium
https://leetcode.com/problems/3sum
33 Medium
https://leetcode.com/problems/minimum-size-subarray-sum
https://leetcode.com/problems/longest-substring-without-repeating-characters
35 Hard
https://leetcode.com/problems/substring-with-concatenation-of-all-words
36 Hard
https://leetcode.com/problems/minimum-window-substring
37 Easy
https://leetcode.com/problems/ransom-note
38 Easy
https://leetcode.com/problems/isomorphic-strings
39 Easy
https://leetcode.com/problems/word-pattern
Hashmap 40 Easy
https://leetcode.com/problems/valid-anagram
41 Easy
https://leetcode.com/problems/two-sum
42 Medium
https://leetcode.com/problems/group-anagrams
43 Medium
https://leetcode.com/problems/longest-consecutive-sequence
44 Easy
https://leetcode.com/problems/valid-parentheses
45 Easy
https://leetcode.com/problems/next-greater-element-i/
Stack 46 Medium
https://leetcode.com/problems/min-stack
47 Medium
https://leetcode.com/problems/simplify-path
48 Easy
https://leetcode.com/problems/linked-list-cycle
49 Easy
https://leetcode.com/problems/merge-two-sorted-lists
50 Medium
https://leetcode.com/problems/add-two-numbers
https://leetcode.com/problems/reverse-linked-list-ii
52 Medium
https://leetcode.com/problems/rotate-list
53 Medium
https://leetcode.com/problems/remove-nth-node-from-end-of-list
54 Hard
https://leetcode.com/problems/reverse-nodes-in-k-group
55 Easy
https://leetcode.com/problems/search-insert-position
56 Medium
https://leetcode.com/problems/search-a-2d-matrix
https://leetcode.com/problems/find-peak-element
58 Medium
https://leetcode.com/problems/search-in-rotated-sorted-array
59 Hard
https://leetcode.com/problems/median-of-two-sorted-arrays
https://leetcode.com/problems/maximum-subarray
61 Medium
https://leetcode.com/problems/maximum-sum-circular-subarray
62 Easy
https://leetcode.com/problems/add-binary
https://leetcode.com/problems/reverse-bits
64 Easy
https://leetcode.com/problems/number-of-1-bits
65 Easy
https://leetcode.com/problems/single-number
66 Easy
https://leetcode.com/problems/palindrome-number
Math 67 Easy
https://leetcode.com/problems/sqrtx
68 Medium
https://leetcode.com/problems/powx-n
69 Medium
https://leetcode.com/problems/combinations
70 Medium
https://leetcode.com/problems/permutations
Backtracking 71 Medium
https://leetcode.com/problems/combination-sum
72 Medium
https://leetcode.com/problems/n-queens-ii
73 Medium
https://leetcode.com/problems/letter-combinations-of-a-phone-number
74 Easy
https://leetcode.com/problems/maximum-depth-of-binary-tree
75 Easy
https://leetcode.com/problems/symmetric-tree
https://leetcode.com/problems/path-sum
77 Medium
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree
78 Medium
https://leetcode.com/problems/count-complete-tree-nodes
79 Easy
https://leetcode.com/problems/average-of-levels-in-binary-tree
https://leetcode.com/problems/binary-tree-level-order-traversal
81 Medium
https://leetcode.com/problems/binary-tree-right-side-view
82 Medium
https://leetcode.com/problems/validate-binary-search-tree
https://leetcode.com/problems/kth-smallest-element-in-a-bst
84 Medium
https://leetcode.com/problems/minimum-absolute-difference-in-bst
85 Medium
https://leetcode.com/problems/number-of-islands
https://leetcode.com/problems/surrounded-regions
87 Medium
https://leetcode.com/problems/clone-graph
88 Medium
https://leetcode.com/problems/course-schedule/
89 Medium
https://leetcode.com/problems/kth-largest-element-in-an-array
Heap 90 Hard
https://leetcode.com/problems/find-median-from-data-stream
91 Easy
https://leetcode.com/problems/climbing-stairs
92 Medium
https://leetcode.com/problems/coin-change
1D DP 93 Medium
https://leetcode.com/problems/house-robber
94 Medium
https://leetcode.com/problems/longest-increasing-subsequence
95 Medium
https://leetcode.com/problems/word-break
96 Medium
https://leetcode.com/problems/longest-palindromic-substring
https://leetcode.com/problems/minimum-path-sum
98 Medium
https://leetcode.com/problems/triangle
99 Hard
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii
100 Hard
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv
TCS Series eBook
Q1. Non-Repeating Element
Find the first non-repeating element in a given array arr of N integers.
Note: Array consists of only positive and negative integers and not zero.
Example 1:
Input : arr[] = {-1, 2, -1, 3, 2}
Output : 3
Explanation:
-1 and 2 are repeating whereas 3 is
the only number occuring once.
Hence, the output is 3.
Example 2:
Input : arr[] = {1, 1, 1}
Output : 0
Your Task:
This is a function problem. The input is already taken care of by the driver code. You
only
need to complete the function firstNonRepeating() that takes an array (arr), sizeOfArray
(n),
and returns the first non-repeating element. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 107
-1016 <= Ai <= 1016
{Ai !=0 }
Answer
Q2. Airport security officials have confiscated several items of the passenger at the
security
checkpoint. All the items have been dumped into a huge box(array). Each item
possessed a
certain amount of risk(0,1,2). Here is the risk severity of the item representing an array[]
of
N number of integer values. The risk here is to sort the item based on their level of risk
values range from 0 to 2.
Example 1:
Input:
7-----------Value of N
[1,0,2,0,1,0,2]-> Element of arr[0] to arr[N-1], while input each element is separated by
new
line
Output:
0 0 0 1 1 2 2 -> Element after sorting based on the risk severity.
Answer
Q3. Array Subset of another array
Given two arrays: a1[0..n-1] of size n and a2[0..m-1] of size m. Task is to check whether
a2[]
is a subset of a1[] or not. Both the arrays can be sorted or unsorted.
Example 1:
Input:
a1[] = {11, 1, 13, 21, 3, 7}
a2[] = {11, 3, 7, 1}
Output:
Yes
Explanation:
a2[] is a subset of a1[]
Example 2:
Input:
a1[] = {1, 2, 3, 4, 5, 6}
a2[] = {1, 2, 4}
Output:
Yes
Explanation:
a2[] is a subset of a1[]
Example 3:
Input:
a1[] = {10, 5, 2, 23, 19}
a2[] = {19, 5, 3}
Output:
No
Explanation:
a2[] is not a subset of a1[]
Your Task:
You don't need to read input or print anything. Your task is to complete the function
isSubset() which takes the array a1[], a2[], its size n and m as inputs and return "Yes" if
arr2
is subset of arr1 else return "No" if arr2 is not subset of arr1.
Output:
00122
Explanation:
0s 1s and 2s are segregated
into ascending order.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^6
0 <= A[i] <= 2
Answer
Q5. Frequencies of Limited Range Array Elements
Given an array A[] of N positive integers which can contain integers from 1 to P where
elements can be repeated or can be absent from the array. Your task is to count the
frequency of all elements from 1 to N.
Note: The elements greater than N in the array can be ignored for counting and do
modify
the array in-place.
Example 1:
Input:
N=5
arr[] = {2, 3, 2, 3, 5}
P=5
Output:
02201
Explanation:
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 2 times.
3 occurring 2 times.
4 occurring 0 times.
5 occurring 1 time.
Example 1:
Input:
N=5
arr[] = {2, 3, 2, 3, 5}
P=5
Output:
02201
Explanation:
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 2 times.
3 occurring 2 times.
4 occurring 0 times.
5 occurring 1 time.
Example 2:
Input:
N=4
arr[] = {3,3,3,3}
P=3
Output:
0040
Explanation:
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 0 times.
3 occurring 4 times.
4 occurring 0 times.
Your Task:
You don't need to read input or print anything. Complete the function frequencycount()
that
takes the array and n as input parameters and modify the array itself in place to denote
the
frequency count of each element from 1 to N. i,e element at index 0 should represent
the
frequency of 1, and so on.
Can you solve this problem without using extra space (O(1) Space)?
Constraints:
1 ≤ N ≤ 105
1 ≤ P ≤ 4*104
1 <= A[i] <= P
Answer
Note: Print the output in a single line, the next line character is printed by the Driver
Code.
Answer
Q10. Searching a number
Given an array Arr of N elements and a integer K. Your task is to return the position of
first
occurence of K in the given array.
Note: Position of first element is considered as 1.
Example 1:
Input:
N = 5, K = 16
Arr[] = {9, 7, 2, 16, 4}
Output: 4
Explanation: K = 16 is found in the
given array at position 4.
Example 2:
Input:
N = 7, K = 98
Arr[] = {1, 22, 57, 47, 34, 18, 66}
Output: -1
Explanation: K = 98 isn't found in
the given array.
Your Task:
Complete the function search() which takes an array arr, two integers n and k, as input
parameters and returns an integer denoting the answer. Return -1 if the number is not
found in array. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 106
1 <= K <= 106
1 <= Arr[i] <= 106
Answer
Q11. Binary Search
Given a sorted array of size N and an integer K, find the position(0-based indexing) at
which
K is present in the array using binary search.
Example 1:
Input:
N=5
arr[] = {1 2 3 4 5}
K=4
Output: 3
Explanation: 4 appears at index 3.
Example 2:
Input:
N=5
LeetCode
Q1. 121. Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and
choosing a
different day in the future to sell that stock
Return the maximum profit you can achieve from this transaction. If you cannot achieve
any
profit, return @
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy
before
you sell.
Answer
Q2. 26. Remove Duplicates from Sorted Array
Given an integer array nuns sorted in non-decreasing order, remove the duplicates in-
place
such that each unique element appears only once. The relative order of the elements
should
be kept the same.
Since it is impossible to change the length of the array in some languages, you must
instead
have the result be placed in the first part of the array nums. More formally, if there are k
elements after removing the duplicates, then the first k elements of nums should hold
the
final result. It does not matter what you leave beyond the first k elements.
Return k after placing the final result in the first k slots of nums.
Do not allocate extra space for another array. You must do this by modifying the input
array
in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct
Length
Answer
Q3. 1480. Running Sum of 1d Array
Given an array nums. We define a running sum of an array as runningSum[i] =
um[nums[@]...nums[1]).
Return the running sum of nums.
Example 1:
Input: nums = [1,2,3,4]
Output: 1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4] .
Example 2:
Input: nums = [1,1,1,1,1]
Output: 11,2,3,4,5]
Explanation: Running sum is obtained as follows: 11, 1+1, 1+1+1, 1+1+1+1,
1+1+1+1+1].
Answer
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n
> 1.
Given n, calculate F(n).
Example 1:
Input: n = 2
Output: 1
Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: n = 4
Output: 3
Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
Answer
You may assume that each input would have exactly one solution, and you may not use
the
same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Answer
Q9. 169. Majority Element
The majority element is the element that appears more than ⌊n / 2⌋ times. You may
Given an array nums of size n, return the majority element.
assume
that the majority element always exists in the array.
Example 1:
Input: nums = [3,2,3]
Output: 3
Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2
Answer
Q10. 118. Pascal's Triangle
Given an integer numRows, return the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as
shown:
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
Answer
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division
operation.
Example 1:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Answer
Q16. 1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
You are given a rectangular cake of size h x w and two arrays of integers horizontalCuts
and
verticalCuts where:
horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal
cut and similarly, and
verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and
vertical
position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be
a
large number, return this modulo 109 + 7.
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the
horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake
have
the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Answer
Q17. 2545. Sort the Students by Their Kth Score
There is a class with m students and n exams. You are given a 0-indexed m x n integer
matrix
score, where each row represents one student and score[i][j] denotes the score the ith
student got in the jth exam. The matrix score contains distinct integers only.
You are also given an integer k. Sort the students (i.e., the rows of the matrix) by their
scores in the kth (0-indexed) exam from the highest to the lowest.
Return the matrix after sorting it.
Example 2:
Input: score = [[3,4],[5,6]], k = 0
Output: [[5,6],[3,4]]
Explanation: In the above diagram, S denotes the student, while E denotes the exam.
- The student with index 1 scored 5 in exam 0, which is the highest score, so they got
first
place.
- The student with index 0 scored 3 in exam 0, which is the lowest score, so they got
second
place.
Answer
Q18. 1380. Lucky Numbers in a Matrix
Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any
order.
A lucky number is an element of the matrix such that it is the minimum element in its
row
and maximum in its column.
Example 1:
Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the
maximum in its column.
Example 2:
Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
Explanation: 12 is the only lucky number since it is the minimum in its row and the
maximum in its column.
Example 3:
Input: matrix = [[7,8],[1,2]]
Output: [7]
Explanation: 7 is the only lucky number since it is the minimum in its row and the
maximum
in its column.
Answer
Q19. 380. Insert Delete GetRandom O(1)
Implement the RandomizedSet class:
• RandomizedSet() Initializes the RandomizedSet object.
• bool insert(int val) Inserts an item val into the set if not present. Returns true if the
item was not present, false otherwise.
• bool remove(int val) Removes an item val from the set if present. Returns true if the
item was present, false otherwise.
• int getRandom() Returns a random element from the current set of elements (it's
guaranteed that at least one element exists when this method is called). Each
element must have the same probability of being returned.
You must implement the functions of the class such that each function works in average
O(1) time complexity.
Example 1:
Input
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert",
"getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
Output
[null, true, false, true, 2, true, false, 2]
Explanation
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted
successfully.
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains
[2].
randomizedSet.insert(2); // 2 was already in the set, so return false.
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will
always return 2.
Answer
Q20. 1876. Substrings of Size Three with Distinct Characters
A string is good if there are no repeated characters.
Given a string s, return the number of good substrings of length three in s.
Note that if there are multiple occurrences of the same substring, every occurrence
should
be counted.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "xyzzaz"
Output: 1
Explanation: There are 4 substrings of size 3: "xyz", "yzz", "zza", and "zaz".
The only good substring of length 3 is "xyz".
Example 2:
Input: s = "aababcabc"
Output: 4
Explanation: There are 7 substrings of size 3: "aab", "aba", "bab", "abc", "bca", "cab",
and
"abc".
The good substrings are "abc", "bca", "cab", and "abc".
Answer
Q21. 763. Partition Labels
You are given a string s. We want to partition the string into as many parts as possible
so
that each letter appears in at most one part.
Note that the partition is done so that after concatenating all the parts in order, the
resultant string should be s.
Return a list of integers representing the size of these parts.
Example 1:
Input: s = "ababcbacadefegdehijhklij"
Output: [9,7,8]
Explanation:
The partition is "ababcbaca", "defegde", "hijhklij".
This is a partition so that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less
parts.
Example 2:
Input: s = "eccbbbbdec"
Output: [10]
Answer
Q22. 2161. Partition Array According to Given Pivot
You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums
such
that the following conditions are satisfied:
• Every element less than pivot appears before every element greater than pivot.
• Every element equal to pivot appears in between the elements less than and greater
than pivot.
• The relative order of the elements less than pivot and the elements greater than
pivot is maintained.
• More formally, consider every pi, pj where pi is the new position of the ith element
and pj is the new position of the jth element. For elements less than pivot, if i < j and
nums[i] < pivot and nums[j] < pivot, then pi < pj. Similarly for elements greater than
pivot, if i < j and nums[i] > pivot and nums[j] > pivot, then pi < pj.
Return nums after the rearrangement.
Example 1:
Input: nums = [9,12,5,10,14,3,10], pivot = 10
Output: [9,5,3,10,10,12,14]
Explanation:
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
The elements 12 and 14 are greater than the pivot so they are on the right side of the
array.
The relative ordering of the elements less than and greater than pivot is also
maintained. [9,
5, 3] and [12, 14] are the respective orderings.
Example 2:
Input: nums = [-3,4,3,2], pivot = 2
Output: [-3,2,4,3]
Explanation:
The element -3 is less than the pivot so it is on the left side of the array.
The elements 4 and 3 are greater than the pivot so they are on the right side of the
array.
The relative ordering of the elements less than and greater than pivot is also
maintained. [-
3] and [4, 3] are the respective orderings.
Answer
Q23. 2442. Count Number of Distinct Integers After Reverse Operations
You are given an array nums consisting of positive integers.
You have to take each integer in the array, reverse its digits, and add it to the end of the
array. You should apply this operation to the original integers in nums.
3000 milliseconds (including the new request). Specifically, return the number of
requests that have happened in the inclusive range [t - 3000, t].
It is guaranteed that every call to ping uses a strictly larger value of t than the previous
call.
Example 1:
Input
["RecentCounter", "ping", "ping", "ping", "ping"]
[[], [1], [100], [3001], [3002]]
Output
[null, 1, 2, 3, 3]
Explanation
RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1
recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2
recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3
recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3
Answer
Q25. 2428. Maximum Sum of an Hourglass
You are given an m x n integer matrix grid.
We define an hourglass as a part of the matrix with the following form:
Battleships can only be placed horizontally or vertically on board. In other words, they
can
only be made of the shape 1 x k (1 row, k columns) or k x 1 (k rows, 1 column), where k
can
be of any size. At least one horizontal or vertical cell separates between two battleships
(i.e.,
there are no adjacent battleships).
Example 1:
Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
Output: 2
Example 2:
Input: board = [["."]]
Output: 0
Answer
Q27. 1381. Design a Stack With Increment Operation
Design a stack that supports increment operations on its elements.
Implement the CustomStack class:
• CustomStack(int maxSize) Initializes the object with maxSize which is the maximum
number of elements in the stack.
• void push(int x) Adds x to the top of the stack if the stack has not reached the
maxSize.
• int pop() Pops and returns the top of the stack or -1 if the stack is empty.
• void inc(int k, int val) Increments the bottom k elements of the stack by val. If there
are less than k elements in the stack, increment all the elements in the stack.
Example 1:
Input
["CustomStack","push","push","pop","push","push","push","increment","increment","pop"
,
"pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
Output
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
Explanation
CustomStack stk = new CustomStack(3); // Stack is Empty []
stk.push(1); // stack becomes [1]
stk.push(2); // stack becomes [1, 2]
stk.pop(); // return 2 --> Return top of the stack 2, stack becomes [1]
stk.push(2); // stack becomes [1, 2]
stk.push(3); // stack becomes [1, 2, 3]
stk.push(4); // stack still [1, 2, 3], Do not add another elements as size is 4
stk.increment(5, 100); // stack becomes [101, 102, 103]
stk.increment(2, 100); // stack becomes [201, 202, 103]
stk.pop(); // return 103 --> Return top of the stack 103, stack becomes [201,
202]
stk.pop(); // return 202 --> Return top of the stack 202, stack becomes [201]
stk.pop(); // return 201 --> Return top of the stack 201, stack becomes []
stk.pop(); // return -1 --> Stack is empty return -1.
Answer
Example 1:
Input: n = 12
Output: true
Explanation: 12 = 31 + 32
Example 2:
Input: n = 91
Output: true
Explanation: 91 = 30 + 32 + 34
Example 3:
Input: n = 21
Output: false
Answer
Q31. 2186. Minimum Number of Steps to Make Two Strings Anagram II
You are given two strings s and t. In one step, you can append any character to either s
or t.
Return the minimum number of steps to make s and t anagrams of each other.
An anagram of a string is a string that contains the same characters with a different (or
the
same) ordering.
Example 1:
In the 2nd query, the subarray is [5,9,3,7]. This can be rearranged as [3,5,7,9], which is
an
arithmetic sequence.
Example 2:
Input: nums = [-12,-9,-3,-12,-6,15,20,-25,-20,-15,-10], l = [0,1,6,4,8,7], r = [4,4,9,7,9,10]
Output: [false,true,false,false,true,true]
Answer
Q35. 941. Valid Mountain Array
Given an array of integers arr, return true if and only if it is a valid mountain array.
Recall that arr is a mountain array if and only if:
• arr.length >= 3
• There exists some i with 0 < i < arr.length - 1 such that:
• arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
• arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Example 1:
Input: arr = [2,1]
Output: false
Example 2:
Input: arr = [3,5,5]
Output: false
Example 3:
Input: arr = [0,3,2,1]
Output: true
Answer
Q36. 704. Binary Search
Given an array of integers nums which is sorted in ascending order, and an integer
target,
write a function to search target in nums. If target exists, then return its index.
Otherwise,
return -1.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Answer
Input: s = "cccaaa"
Output: "aaaccc"
Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid
answers.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input: s = "Aabb"
Output: "bbAa"
Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
Answer