Fast Learner Assignment
Fast Learner Assignment
Fast Learner Assignment
int main() {
vector<int> height = {7, 0, 4, 2, 5, 0, 6, 4, 0, 5};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
cout << "Maximum water that can be trapped: " << trap(height) << endl;
return 0;
}
Input:
vector<int> height = {7, 0, 4, 2, 5, 0, 6, 4, 0, 5};
Output:
Maximum water that can be trapped: 25
Time Complexity:
• O(n), where n is the number of bars (heights).
The algorithm makes a single pass from both sides of the array, which
gives linear complexity.
Space Complexity:
• O(1), as we are using only a few variables (left, right, maxLeft,
maxRight, water) apart from the input array.
return true;
}
int main() {
vector<int> stations = {1, 2, 4, 5, 0};
int r = 1, k = 2;
cout << "Maximum possible minimum power: " << maxPowerCity(stations,
r, k) << endl;
return 0;
}
Input:
vector<int> stations = {1, 2, 4, 5, 0};
int r = 1, k = 2;
Output:
Maximum possible minimum power: 5
Time Complexity:
• O(n * log(maxPower)), where n is the number of cities.
For each binary search step (log(maxPower)), we iterate over the n cities
to check if placing power stations is feasible.
Space Complexity:
• O(n), as we store additional arrays for tracking power and any
modifications during the feasibility check.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int main() {
string s = "abacaba", t = "bzaa";
cout << "Minimum possible score: " << minScoreToSubsequence(s, t) <<
endl;
return 0;
}
Input:
string s = "abacaba";
string t = "bzaa";
Output:
Minimum possible score: 1
Time Complexity:
• O(n + m), where n is the length of string s and m is the length of string t.
We are scanning through both strings, making it linear with respect to
their combined lengths.
Space Complexity:
• O(1), since only a few variables are used (left, right), and no extra arrays
or data structures are needed.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
int main() {
vector<int> nums1 = {1, 0, 1};
vector<int> nums2 = {0, 0, 0};
vector<vector<int>> queries = {{1, 1, 1}, {2, 1, 0}, {3, 0, 0}};
processQueries(nums1, nums2, queries);
return 0;
}
Input:
vector<int> nums1 = {1, 0, 1};
vector<int> nums2 = {0, 0, 0};
vector<vector<int>> queries = {{1, 1, 1}, {2, 1, 0}, {3, 0, 0}};
Output:
3
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Time Complexity:
• For type 1 queries (flip operation): O(r - l), where l and r are indices for
flipping the values.
• For type 2 queries (modify array): O(n), where n is the length of the
arrays nums1 and nums2.
• For type 3 queries (sum query): O(n), since it involves summing up all
elements of nums2.
• Overall complexity depends on the number of queries, worst case would
be O(q * n), where q is the number of queries and n is the length of the
arrays.
Space Complexity:
• O(n), where n is the size of the nums1 and nums2 arrays.
int main() {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Input:
vector<int> nums = {1, 3, 5, 2, 7, 5};
int minK = 1, maxK = 5;
Output:
Number of fixed-bound subarrays: 2
Time Complexity:
• O(n), where n is the number of elements in the nums array.
We scan through the array once, keeping track of the positions of minK
and maxK.
Space Complexity:
• O(1), since we use only a few variables to store positions and counts
} else {
int h = heights[s.top()];
s.pop();
int width = s.empty() ? i : i - s.top() - 1;
maxArea = max(maxArea, h * width);
}
}
while (!s.empty()) {
int h = heights[s.top()];
s.pop();
int width = s.empty() ? i : i - s.top() - 1;
maxArea = max(maxArea, h * width);
}
return maxArea;
}
int main() {
vector<int> heights = {2, 1, 5, 6, 2, 3};
cout << "Largest rectangle area: " << largestRectangleArea(heights) << endl;
return 0;
}
Input:
vector<int> heights = {2, 1, 5, 6, 2, 3};
Output:
Largest rectangle area: 10
Time Complexity:
• O(n), where n is the number of bars (heights).
Each bar is pushed and popped from the stack at most once, so the overall
complexity is linear.
Space Complexity:
• O(n), where n is the number of bars. The stack can grow up to the size of
the input array.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int main() {
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
cout << "Maximum path sum: " << getMaxPathSum(root) << endl;
return 0;
}
Input:
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Maximum path sum: 6
Time Complexity:
• O(n), where n is the number of nodes in the tree.
Each node is visited once to compute the maximum path sum.
Space Complexity:
• O(h), where h is the height of the tree.
This is due to the recursion stack, and in the worst case (a skewed tree), h
can be equal to n.
int main() {
vector<int> prices = {3, 2, 6, 5, 0, 3};
int k = 2;
cout << "Maximum profit: " << maxProfit(k, prices) << endl;
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Input:
vector<int> prices = {3, 2, 6, 5, 0, 3};
int k = 2;
Output:
Maximum profit: 7
Time Complexity:
• O(k * n), where k is the number of transactions and n is the number of
days.
We update the buy and sell arrays for each day and each transaction.
Space Complexity:
• O(k), as we are using two arrays of size k (one for buy and one for sell).