CC Worksheet - 1.1 - 20BCS8284
CC Worksheet - 1.1 - 20BCS8284
CC Worksheet - 1.1 - 20BCS8284
Experiment - 1.1
Aim:
Understand the problem and find out better approach to solve
particular problem based on stack queue and arrays.
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();
}
};
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:
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;
}
};
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.
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];
}
};
Notes:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Solution:
Algorithm:
1- Start with an empty String
2- loop over the input string and add characters of it into the string
3- If you encounter any / then push string into the stack if it is not . or ..
4 - If string is equal to the .. then pop up stack one time in this case
6. add all the element of stack in a new string with an forword slash
that’s the answer
Code:
class Solution {
public:
string simplifyPath(string A) {
stack<string> stk;
string str;
for(int i=0;i<A.size();i++){
if(A[i]=='/'){
if(str.empty()) continue;
if(str==".."){
if(!stk.empty()) stk.pop();
}
if(str!="." && str!="..") stk.push(str);
str="";
}else str+=A[i];
}
if(!stk.empty() && str=="..") stk.pop();
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of
lists
Solution:
Algorithm:
1- Start from head of both the list and make a new head of an list
2- Compare both of them list element adds smaller value list to the
newlist
3- And forward the pointer of small element
list
Code:
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2){
if(!list1 && !list2) return list1;
if(!list1) return list2;
if(!list2) return list1;
vector<int> vec;
for(ListNode* it=list1;it;it=it->next){
vec.push_back(it->val);
}for(ListNode* it=list2;it;it=it->next){
vec.push_back(it->val);
}
sort(vec.begin(),vec.end());
ListNode* head=new ListNode(vec[0]);
ListNode* it=head;
for(int i=1;i<vec.size();i++){
it->next=new ListNode(vec[i]);
it=it->next;
}
return head;
}
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Solution:
Algorithm:
1- Iterate over the list.
2- If any element in the list is same as its next element than increment
the pointer until it reaches the value where it’s not equal to its previous.
3- join the pointer with the previous pointer where we started.
Code:
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* node=new ListNode(-101);
node->next=head;
ListNode* it=node;
while(it->next){
int val=it->next->val;
if(it->next->next && it->next->next->val==val){
while(it->next && it->next->val==val) it->next=it->next->next;
}else it=it->next;
}
return node->next;
}
};