Song Duration Pairs Divisible by 60
Song Duration Pairs Divisible by 60
Arrays/Strings- 1
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
13 Hard Trapping Rain Water - LeetCode
14 Hard Candy -
Arrays/Strings- 2
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
Matrix
[Link]
[Link]
25 Medium Rotate Image - LeetCode
[Link]
2 Pointers
28 Easy
[Link]
[Link]
29 Easy
[Link]
[Link]
30 Medium
[Link]
[Link]
31 Medium
[Link]
[Link]
32 Medium
[Link]
[Link]
Sliding Window
33 Medium
[Link]
[Link]
34 Medium
[Link]
[Link]
35 Hard
[Link]
36 Hard
[Link]
[Link]
Hashmap
37 Easy
[Link]
[Link]
38 Easy
[Link]
[Link]
39 Easy
[Link]
[Link]
40 Easy
[Link]
[Link]
41 Easy
[Link]
[Link]
42 Medium
[Link]
[Link]
43 Medium
[Link]
[Link]
Stack 44 Easy
[Link]
[Link]
45 Easy
[Link]
[Link]
46 Medium
[Link]
[Link]
47 Medium
[Link]
[Link]
Linked List
48 Easy
[Link]
[Link]
49 Easy
[Link]
[Link]
50 Medium
[Link]
[Link]
51 Medium
[Link]
[Link]
52 Medium
[Link]
[Link]
53 Medium
[Link]
[Link]
54 Hard
[Link]
[Link]
Binary Search
55 Easy
[Link]
[Link]
56 Medium
[Link]
[Link]
57 Medium
[Link]
[Link]
58 Medium
[Link]
[Link]
59 Hard
[Link]
Kadane's
Algo
60 Medium
[Link]
61 Medium
[Link]
Bit Manipulation
62 Easy
[Link]
[Link]
63 Easy
[Link]
64 Easy
[Link]
[Link]
65 Easy
[Link]
[Link]
Math
66 Easy
[Link]
[Link]
67 Easy
[Link]
[Link]
68 Medium
[Link]
Backtracking
69 Medium
[Link]
70 Medium
[Link]
71 Medium
[Link]
72 Medium
[Link]
73 Medium
[Link]
Binary Tree
74 Easy
[Link]
[Link]
75 Easy
[Link]
[Link]
76 Easy
[Link]
[Link]
77 Medium
[Link]
[Link]
78 Medium
[Link]
[Link]
79 Easy
[Link]
[Link]
80 Medium
[Link]
[Link]
81 Medium
[Link]
[Link]
82 Medium
[Link]
[Link]
83 Medium
[Link]
[Link]
84 Medium
[Link]
[Link]
Graph General
85 Medium
[Link]
86 Medium
[Link]
87 Medium
[Link]
88 Medium
[Link]
Heap 89 Medium
[Link]
90 Hard
[Link]
1D DP
91 Easy
[Link]
92 Medium
[Link]
93 Medium
[Link]
94 Medium
[Link]
95 Medium
[Link]
Multi Dimensional DP
96 Medium
[Link]
97 Medium
[Link]
98 Medium
[Link]
99 Hard
[Link]
100 Hard
[Link]
Topic Serial No. Difficulty Level Question Link:
28 Easy
[Link]
29 Easy
[Link]
2 Pointers 30 Medium
[Link]
31 Medium
[Link]
32 Medium
[Link]
33 Medium
[Link]
[Link]
35 Hard
[Link]
36 Hard
[Link]
37 Easy
[Link]
38 Easy
[Link]
39 Easy
[Link]
Hashmap 40 Easy
[Link]
41 Easy
[Link]
42 Medium
[Link]
43 Medium
[Link]
44 Easy
[Link]
45 Easy
[Link]
Stack 46 Medium
[Link]
47 Medium
[Link]
48 Easy
[Link]
49 Easy
[Link]
50 Medium
[Link]
[Link]
52 Medium
[Link]
53 Medium
[Link]
54 Hard
[Link]
55 Easy
[Link]
56 Medium
[Link]
[Link]
58 Medium
[Link]
59 Hard
[Link]
[Link]
61 Medium
[Link]
62 Easy
[Link]
[Link]
64 Easy
[Link]
65 Easy
[Link]
66 Easy
[Link]
Math 67 Easy
[Link]
68 Medium
[Link]
69 Medium
[Link]
70 Medium
[Link]
Backtracking 71 Medium
[Link]
72 Medium
[Link]
73 Medium
[Link]
74 Easy
[Link]
75 Easy
[Link]
[Link]
77 Medium
[Link]
78 Medium
[Link]
79 Easy
[Link]
[Link]
81 Medium
[Link]
82 Medium
[Link]
[Link]
84 Medium
[Link]
85 Medium
[Link]
[Link]
87 Medium
[Link]
88 Medium
[Link]
89 Medium
[Link]
Heap 90 Hard
[Link]
91 Easy
[Link]
92 Medium
[Link]
1D DP 93 Medium
[Link]
94 Medium
[Link]
95 Medium
[Link]
96 Medium
[Link]
[Link]
98 Medium
[Link]
99 Hard
[Link]
100 Hard
[Link]
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();
[Link](1); // Inserts 1 to the set. Returns true as 1 was inserted
successfully.
[Link](2); // Returns false as 2 does not exist in the set.
[Link](2); // Inserts 2 to the set, returns true. Set now contains [1,2].
[Link](); // getRandom() should return either 1 or 2 randomly.
[Link](1); // Removes 1 from the set, returns true. Set now contains
[2].
[Link](2); // 2 was already in the set, so return false.
[Link](); // 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();
[Link](1); // requests = [1], range is [-2999,1], return 1
[Link](100); // requests = [1, 100], range is [-2900,100], return 2
[Link](3001); // requests = [1, 100, 3001], range is [1,3001], return 3
[Link](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 []
[Link](1); // stack becomes [1]
[Link](2); // stack becomes [1, 2]
[Link](); // return 2 --> Return top of the stack 2, stack becomes [1]
[Link](2); // stack becomes [1, 2]
[Link](3); // stack becomes [1, 2, 3]
[Link](4); // stack still [1, 2, 3], Do not add another elements as size is 4
[Link](5, 100); // stack becomes [101, 102, 103]
[Link](2, 100); // stack becomes [201, 202, 103]
[Link](); // return 103 --> Return top of the stack 103, stack becomes [201,
202]
[Link](); // return 202 --> Return top of the stack 202, stack becomes [201]
[Link](); // return 201 --> Return top of the stack 201, stack becomes []
[Link](); // 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:
• [Link] >= 3
• There exists some i with 0 < i < [Link] - 1 such that:
• arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
• arr[i] > arr[i + 1] > ... > arr[[Link] - 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