PLACEMENT PRACTICE QUESTIONS
10/12/2024
HIMANSHU BHADANI
RA2211026010368
1. REGULAR EXPRESSION MATCHING
class Solution {
public:
bool isMatch(string s, string p) {
int n = s.length(), m = p.length();
bool dp[n+1][m+1];
memset(dp, false, sizeof(dp));
dp[0][0] = true;
for(int i=0; i<=n; i++){
for(int j=1; j<=m; j++){
if(p[j-1] == '*'){
dp[i][j] = dp[i][j-2] || (i > 0 && (s[i-1] == p[j-2] || p[j-2] == '.') && dp[i-1][j]);
}
else{
dp[i][j] = i > 0 && dp[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '.');
}
}
}
return dp[n][m];
}
};
2. Merge k Sorted Lists
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
vector<int> v1;
for (auto list : lists) {
while (list) {
v1.push_back(list->val);
list = list->next;
}
}
sort(v1.begin(), v1.end());
ListNode* dummy = new ListNode(0);
ListNode* temp = dummy;
for (auto i : v1) {
temp->next = new ListNode(i);
temp = temp->next;
}
return dummy->next;
}
};
3. Trapping Rain Water
class Solution {
public:
int trap(vector<int>& height) {
int n=height.size();
int l=0;
int r=n-1;
int lMax=0;
int rMax=0;
int total=0;
while(l<=r){
if(height[l]<=height[r]){
if(height[l]>=lMax){
lMax=height[l];
}
else{
total+=lMax-height[l];
}
l++;
}
else{
if(height[r]>=rMax){
rMax=height[r];
}
else{
total+=rMax-height[r];
}
r--;
}
}
return total;
}
};
4. Minimum Window Substring
class Solution {
public:
string minWindow(string s, string t) {
if (s.length() < t.length()) {
return "";
}
unordered_map<char, int> charCount;
for (char ch : t) {
charCount[ch]++;
}
int targetCharsRemaining = t.length();
int minWindow[2] = {0, INT_MAX};
int startIndex = 0;
for (int endIndex = 0; endIndex < s.length(); endIndex++) {
char ch = s[endIndex];
if (charCount.find(ch) != charCount.end() && charCount[ch] > 0) {
targetCharsRemaining--;
}
charCount[ch]--;
if (targetCharsRemaining == 0) {
while (true) {
char charAtStart = s[startIndex];
if (charCount.find(charAtStart) != charCount.end() && charCount[charAtStart] ==
0) {
break;
}
charCount[charAtStart]++;
startIndex++;
}
if (endIndex - startIndex < minWindow[1] - minWindow[0]) {
minWindow[0] = startIndex;
minWindow[1] = endIndex;
}
charCount[s[startIndex]]++;
targetCharsRemaining++;
startIndex++;
}
}
return minWindow[1] >= s.length() ? "" : s.substr(minWindow[0], minWindow[1] -
minWindow[0] + 1);
}
};
5. WORD LADDER
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
// Create a set for fast lookup of words
unordered_set<string> st(wordList.begin(), wordList.end());
// If the endWord is not in the wordList, we cannot form a transformation
if (st.find(endWord) == st.end()) return 0;
// BFS queue: stores {current word, transformation steps}
queue<pair<string, int>> q;
q.push({beginWord, 1});
st.erase(beginWord); // Remove the starting word to avoid revisiting it
while (!q.empty()) {
string word = q.front().first;
int steps = q.front().second;
q.pop();
// Try all possible transformations
for (int i = 0; i < word.size(); i++) {
char originalChar = word[i]; // Save the original character
// Replace with every possible character from 'a' to 'z'
for (char ch = 'a'; ch <= 'z'; ch++) {
word[i] = ch;
// Check if the transformed word is the endWord
if (word == endWord) return steps + 1;
// If the transformed word exists in the set, process it
if (st.find(word) != st.end()) {
st.erase(word); // Remove the word from the set
q.push({word, steps + 1});
}
}
word[i] = originalChar; // Restore the original character
}
}
return 0; // If no transformation sequence is found
}
};
LEETCODE PRACTICE QUESTIONS
1. Range Sum Query – Immutable
class NumArray {
public:
vector<int> ans;
NumArray(vector<int>& nums) {
ans.push_back(0);
for(int num:nums){
ans.push_back(ans.back()+num);
}
}
int sumRange(int left, int right) {
return ans[right+1]-ans[left];
}};
2. Range Sum Query – Mutable
class BIT {
private:
std::vector<int> stree;
public:
BIT(int N) {
stree = std::vector<int>(N + 1, 0);
}
void increase(int i, int x) {
while (i < stree.size()) {
stree[i] += x;
i |= (i + 1);
}
}
int total(int i) {
int sum = 0;
while (i >= 0) {
sum += stree[i];
i = (i & (i + 1)) - 1;
}
return sum;
}
};
class NumArray {
private:
BIT bit;
std::vector<int> nums;
public:
NumArray(std::vector<int>& nums) : bit(nums.size()), nums(nums) {
for (int i = 0; i < nums.size(); i++) {
bit.increase(i + 1, nums[i]);
}
}
void update(int index, int val) {
int delta = val - nums[index];
bit.increase(index + 1, delta);
nums[index] += delta;
}
int sumRange(int left, int right) {
return bit.total(right + 1) - bit.total(left);
}
};
3. Count of Smaller Numbers After Self
class Solution {
public:
void merge(int left, int mid, int right, vector<pair<int, int>>& arr,vector<int>& count)
{
vector<pair<int, int>> temp(right - left + 1);
int i = left;
int j = mid + 1;
int k = 0;
while(i <= mid && j <= right)
{
if(arr[i].first <= arr[j].first)
{
temp[k++] = arr[j++];
}
else
{
count[arr[i].second] += (right - j + 1);
temp[k++] = arr[i++];
}
}
while(i <= mid)
{
temp[k++] = arr[i++];
}
while(j <= right)
{
temp[k++] = arr[j++];
}
for(int l = left; l <= right; l++)
arr[l] = temp[l - left];
}
void mergeSort(int left, int right, vector<pair<int, int>>& arr, vector<int>& count)
{
if(left >= right)
{
return;
}
int mid = left + (right - left) / 2;
mergeSort(left, mid, arr, count);
mergeSort(mid + 1, right, arr, count);
merge(left, mid, right, arr, count);
}
vector<int> countSmaller(vector<int>& nums) {
int n=nums.size();
vector<pair<int, int>> arr;
for(int i = 0; i < n; i++)
{
arr.push_back({nums[i], i});
}
vector<int> count(n, 0);
mergeSort(0, n - 1, arr, count);
return count;
}};
4. Count of Range Sum
class Solution {
public:
int countWithMergeSort(vector<long> &sums, int left, int right, int lower, int upper)
{
int count = 0;
if(right - left <= 1)
{
if(right - left == 1)
{
return (lower <= sums[left] && sums[left] <= upper);
}
else
{
return 0;
}
}
int mid = (left + right)/2;
int leftSideSum = countWithMergeSort(sums, left, mid, lower, upper);
int rightSideSum = countWithMergeSort(sums, mid, right, lower, upper);
int i = left;
int j = mid;
int n = 0;
int m = 0;
vector<long> cache(right - left, 0);
int k = 0;
while(i < mid)
{
while(mid+n < right && sums[mid+n] < sums[i]+lower)
{
n++;
}
while(mid+m < right && sums[mid+m] <= sums[i] + upper)
{
m++;
}
while(j < right && sums[j] < sums[i])
{
cache[k++] = sums[j++];
}
cache[k++] = sums[i++];
count += m-n;
}
while(j < right)
{
cache[k++] = sums[j++];
}
for(int idx = 0; idx<cache.size(); idx++)
{
sums[left + idx] = cache[idx];
}
return leftSideSum + rightSideSum + count;
int countRangeSum(vector<int>& nums, int lower, int upper) {
vector<long> prefixSum(nums.size(),0);
int n = nums.size();
prefixSum[0] = nums[0];
for(int i = 1; i<nums.size(); i++)
{
prefixSum[i] = nums[i] + prefixSum[i-1];
}
return countWithMergeSort(prefixSum, 0, n, lower, upper);
}
};