----------------------INDEX---------------------
1> Scaning string with spaces
2> Ways of adding cusotm comparator in c++ STL sort
3> Way of declearing and initializing STL vector using constructor
4> Deque in STL
5> List in STL
6> unordered_set in STL
7> set in STL
8> unordered_map in STL
9> Rounding a number without ANY library function
-------------------------------------------------
1> Scaning string with spaces
=>
stirng temp;
getline(cin,temp);
https://www.geeksforgeeks.org/getline-string-c/
Now there is one problem the getline consumes the \n or ENTER key also
so if the sequence is
int n;
cin>>n;
getline(cin,temp);
then the ENTER pressed after cin>>n will be consuemd by the getline
so remember to flush the \n or ENTER .
i.e
*****
int test;
cin>>test;
string test,flush;
getline(cin,flush);
getline(cin,test);
----------------------------------------------------------------------------
2> Ways of adding cusotm comparator in c++ STL sort
=>
QUESTION = > https://leetcode.com/problems/largest-number/
METHOD 1:
class Solution {
public:
static bool comp(int n1,int n2){
string s1 = to_string(n1);
string s2 = to_string(n2);
return (s1+s2> s2+s1);
}
string largestNumber(vector<int>& nums) {
int n =nums.size();
sort(nums.begin(),nums.end(),comp);
string result="";
for(int i=0;i<n;i++){
result+=to_string(nums[i]);
}
if(result[0]=='0'){
return "0";
}
return result;
}
};
METHOD 2:
class Solution {
struct comp {
bool operator() (int a, int b) {
string comb1 = to_string(a) + to_string(b);
string comb2 = to_string(b) + to_string(a);
return comb1 > comb2;
}
} mycomp;
public:
string largestNumber(vector<int>& nums) {
sort(nums.begin(), nums.end(), mycomp);
if (nums[0] == 0) return "0";
string res = "";
for (auto num : nums) {
res = res + to_string(num);
}
return res;
}
};
----------------------------------------------------------------------------
3> Way of declearing and initializing STL vector using constructor
=>
* int numIslands(vector<vector<char>>& grid) {
if (grid.empty()) return 0;
int m = grid.size();
int n = grid[0].size();
vector<vector<bool>>visited(m, vector<bool>(n, false));
* int V = graph.size();
vector<int>res;
vector<int>dp(V, 0);
----------------------------------------------------------------------------
4> Deque in STL
=>
https://www.geeksforgeeks.org/deque-cpp-stl/
Example problem for it
https://leetcode.com/problems/sliding-window-maximum/
----------------------------------------------------------------------------
5> List in STL
=>
List in C++ Standard Template Library (STL)
Overview:->
sequence containers that allow non-contiguous memory allocation. As compared to
vector, list has slow traversal, but once a position has been found, insertion and
deletion are quick. Normally, when we say a List, we talk about doubly linked list.
----------------------------------------------------------------------------
6> unordered_set
=>
https://www.geeksforgeeks.org/unordered_set-in-cpp-stl/
----------------------------------------------------------------------------
7> set
=>
https://www.geeksforgeeks.org/insertion-deletion-stl-set-c/
----------------------------------------------------------------------------
8> unordered_map
=>
https://www.geeksforgeeks.org/unordered_map-in-cpp-stl/
----------------------------------------------------------------------------
----------------------------------------------------------------------------
9> Rounding a number without ANY library function
=>
https://www.codechef.com/APRIL21C/problems/BOLT
first we hand to calculate the speed , then divide 100/speed to get the time
and then round that time to 2 decimal palces , then compare it with 9.58
To do this simply first multiply t by 100 and round it.
the round function produces an integer so we again divide it by 100
we now have 2 decimal palced rounded number , same can be generalized for
rounding to n decimal palces.
One more problem here is on compairing 9.58<9.58 was giving true i think this is
just float point comparision problem, i kept it integer and compared it with 958
float k1,k2,k3,v,t;
cin>>k1>>k2>>k3>>v;
v = v*k1*k2*k3;
t = 100/v;
t = round(t*100);
t = t/100;
t = t*100;
cout<<t<<endl;
if(t < 958){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
----------------------------------------------------------------------------