DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment - 1.1
Name : Naitik Monga
UID : 20BCS9740
Section : 713-A
Subject : Competitive Coding
Subject Code : 20CSP-351
Aim:
Understand the problem and find out better approach to
solve particular problem based on stack queue and arrays.
Question no.1: Implement Queue
using StackDescription:
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k,
and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Solution:
Algorithm:
1- Initialize the two stacks
2- On every put request put the new element in stack one
3- On every pop and peek request transfer all the element of stack1 to
stack2 except one element in case of peek return it
4- Then again transfer all the element back into the stack1 in case of
peek and in case of pop no include last element
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
class MyQueue {
public:
stack<int>
stk1,stk2;
MyQueue() {
void push(int x) {
stk1.push(x);
}
int pop() {
while(stk1.size()>1) stk2.push(stk1.top()),stk1.pop();
int ans=stk1.top();
stk1.pop();
while(!stk2.empty()) stk1.push(stk2.top()),stk2.pop();
return ans;
}
int peek() {
while(stk1.size()>1) stk2.push(stk1.top()),stk1.pop();
int ans=stk1.top();
while(!stk2.empty()) stk1.push(stk2.top()),stk2.pop();
return ans;
}
bool empty() {
return stk1.empty();
}
};
Question no.2- 3
Sum Description:
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
Each element nums[i] represents the maximum length of a forward jump from index i. In other words,
if you are at nums[i], you can jump to any nums[i + j] where:
0 <= j <= nums[i] and
i+j<n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that
you can reach nums[n - 1].
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Solution:
Algorithm:
1- Sort the array.
2- Make a loop for fixing an element out of three.
3- Now for finding rest two element apply two pointers approach.
4- If some of all the elements are equal to 0 then add it to the answer.
Code:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& number) {
sort(number.begin(),number.end());
set<vector<int>> ans;
for(int x=0;x<number.size()-1;x++){
if(x>0 && number[x]==number[x-1]) continue;
int i=x+1,j=number.size()-1;
int tar=-number[x];
while(i<j){
if(number[i]+number[j]<tar) i++;
else if(number[i]+number[j]>tar) j--;
else if(number[i]+number[j]==tar){
ans.insert({number[x],number[i],number[j]});
i++;
j--;
}
}
}
vector<vector<int>> result(ans.begin(),ans.end());
return result;
}
};
Question 3: Jump Game
II Description:
Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-
style file system, convert it to the simplified canonical path.
In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the
directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For
this problem, any other format of periods such as '...' are treated as file/directory names.
The canonical path should have the following format:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Solution:
Algorithm:
1-Make a dp array.
2 –Start from last index how much is it cost to jump
..3-Store minimum jump on each point.
4-Use stored value to find minimum at each point
.5- Return dp[0].
Code:
class Solution {
public:
int jump(vector<int>& game) {
int m=game.size();
vector<int> dp(m,INT_MAX);
dp[game.size()-1]=0;
for(int i=m-2;i>=0;i--){
int n=min(i+game[i],m-1);
for(int j=i;j<=n;j++) if(dp[j]!=INT_MAX) dp[i]=min(dp[i],dp[j]+1);
}
return dp[0];
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING