Skip to content

Commit a05e73a

Browse files
committed
leetcode 2536
1 parent 7805d40 commit a05e73a

File tree

8 files changed

+339
-0
lines changed

8 files changed

+339
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
int solve()
10+
{
11+
int n;
12+
cin >> n;
13+
int count = 1;
14+
15+
vector<pair<int, int>> v(n);
16+
for (int i = 0; i < n; i++)
17+
{
18+
cin >> v[i].first;
19+
v[i].second = i + 1;
20+
}
21+
22+
sort(v.begin(), v.end());
23+
int temp = -1;
24+
for (int i = 0; i < n; i++)
25+
{
26+
if (v[i].second < temp)
27+
{
28+
count++;
29+
}
30+
temp = v[i].second;
31+
}
32+
cout << count << endl;
33+
return 0;
34+
}
35+
int main()
36+
{
37+
int testCase = 1;
38+
while (testCase--)
39+
{
40+
solve();
41+
}
42+
return 0;
43+
}

Codeforces/1117B-Emotes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ int main()
7878
cin>>n>>m>>k;
7979
vl v(n,0);
8080
forin(v,n);
81+
// sorting the vector
8182
srt(v);
8283
ll x=v[n-1];
8384
ll y=v[n-2];
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
int n,a,b;
12+
cin>>n>>a>>b;
13+
if(n==1){
14+
if(a==b){
15+
cout<<1<<endl<<a<<":"<<b<<endl;
16+
return 0;
17+
}
18+
else{
19+
cout<<0<<endl<<a<<":"<<b<<endl;
20+
return 0;
21+
}
22+
}
23+
24+
vector<pair<int,int>> v(n+1);
25+
for(int i=1;i<=n;i++){
26+
v[i].first=0;
27+
v[i].second=0;
28+
}
29+
int draw=0;
30+
31+
int pos=1;
32+
for(;pos<=n;){
33+
if(a>0){
34+
a--;
35+
v[pos].first++;
36+
pos++;
37+
}
38+
else{
39+
break;
40+
}
41+
}
42+
if(a>0){
43+
v[1].first += a;
44+
}
45+
if(b){
46+
if(pos==n+1){
47+
v[1].first++;
48+
v[n].first--;
49+
v[n].second=b;
50+
}
51+
else{
52+
for(;pos<=n;){
53+
if(b>0){
54+
b--;
55+
v[pos].second++;
56+
pos++;
57+
}
58+
else{
59+
break;
60+
}
61+
}
62+
if(b>0){
63+
v[n].second += b;
64+
}
65+
}
66+
67+
}
68+
for(int i=1;i<=n;i++){
69+
if(v[i].first==v[i].second){
70+
draw++;
71+
}
72+
}
73+
cout<<draw<<endl;
74+
for(int i=1;i<=n;i++){
75+
cout<<v[i].first<<":"<<v[i].second<<endl;
76+
}
77+
78+
return 0;
79+
}
80+
int main()
81+
{
82+
int testCase=1;
83+
while(testCase--){
84+
solve();
85+
}
86+
return 0;
87+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
int solve()
10+
{
11+
int a, b, c, d;
12+
cin >> a >> b >> c >> d;
13+
14+
if ((b > a && d > c && c > a && d > b) ||
15+
(a > c && b > d && d > c && b > a) ||
16+
(c > d && a > b && b > d && a > c) ||
17+
(d > b && c > a && a > b && c > d))
18+
{
19+
cout << "YES" << endl;
20+
}
21+
else
22+
{
23+
cout << "NO" << endl;
24+
}
25+
return 0;
26+
}
27+
int main()
28+
{
29+
int testCase = 1;
30+
cin >> testCase;
31+
while (testCase--)
32+
{
33+
solve();
34+
}
35+
return 0;
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
int w,d,h,a,b,f,g;
12+
cin>>w>>d>>h>>a>>b>>f>>g;
13+
cout<<min({b+h+g+abs(f-a),abs(d-b)+h+abs(d-g)+abs(a-f),a+h+f+abs(g-b),abs(w-a)+h+abs(w-f)+abs(b-g)})<<endl;
14+
return 0;
15+
}
16+
int main()
17+
{
18+
int testCase=1;
19+
cin>>testCase;
20+
while(testCase--){
21+
solve();
22+
}
23+
return 0;
24+
}

GFG/Prefix Subsequences.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// { Driver Code Starts
2+
// Initial Template for C++
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
// } Driver Code Ends
8+
// User function Template for C++
9+
10+
class Solution
11+
{
12+
public:
13+
int maximumPrefix(int n, int m, string s, string t)
14+
{
15+
// code here
16+
int j = 0;
17+
for (int i = 0; i < n; i++)
18+
{
19+
bool flag = false;
20+
while (j < m)
21+
{
22+
if (t[j] == s[i])
23+
{
24+
flag = true;
25+
j++;
26+
break;
27+
}
28+
j++;
29+
}
30+
if (flag == false)
31+
{
32+
return i + 1;
33+
}
34+
}
35+
return -1;
36+
}
37+
};
38+
39+
// { Driver Code Starts.
40+
41+
int main()
42+
{
43+
int c;
44+
cin >> c;
45+
while (c--)
46+
{
47+
int n, m;
48+
cin >> n >> m;
49+
string s, t;
50+
cin >> s >> t;
51+
Solution obj;
52+
int ans = obj.maximumPrefix(n, m, s, t);
53+
cout << ans << "\n";
54+
}
55+
56+
return 0;
57+
}
58+
// } Driver Code Ends

GFG/Tree Formation.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// { Driver Code Starts
2+
// Initial Template for C++
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
// } Driver Code Ends
8+
// User function Template for C++
9+
10+
class Solution
11+
{
12+
public:
13+
long long minimumCost(int n, vector<int> &p)
14+
{
15+
// code here
16+
long long ans = 0;
17+
unordered_map<int, int> m;
18+
19+
m[1] = 0;
20+
for (int i = 1; i < p.size(); i++)
21+
{
22+
m[p[i]]++;
23+
m[i + 1]++;
24+
ans += m[p[i]];
25+
}
26+
27+
return ans;
28+
}
29+
};
30+
31+
// { Driver Code Starts.
32+
33+
int main()
34+
{
35+
int t;
36+
cin >> t;
37+
while (t--)
38+
{
39+
int n;
40+
cin >> n;
41+
vector<int> p(n);
42+
for (int i = 0; i < n; i++)
43+
cin >> p[i];
44+
Solution obj;
45+
long long ans = obj.minimumCost(n, p);
46+
cout << ans << endl;
47+
}
48+
return 0;
49+
}
50+
// } Driver Code Ends
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
typedef long long ll ;
6+
const ll INF=1e18;
7+
const ll mod1=1e9+7;
8+
const ll mod2=998244353;
9+
//Add main code here
10+
11+
class Solution
12+
{
13+
public:
14+
vector<vector<int>> rangeAddQueries(int n, vector<vector<int>> &queries)
15+
{
16+
vector<vector<int>> s(n, vector<int>(n, 0));
17+
for (auto &q : queries)
18+
{
19+
++q[2], ++q[3];
20+
s[q[0]][q[1]]++;
21+
if (q[2] < n)
22+
s[q[2]][q[1]]--;
23+
if (q[3] < n)
24+
s[q[0]][q[3]]--;
25+
if (q[2] < n && q[3] < n)
26+
s[q[2]][q[3]]++;
27+
}
28+
for (int i = 0; i < n; ++i)
29+
for (int j = 0; j < n; ++j)
30+
{
31+
if (i > 0)
32+
s[i][j] += s[i - 1][j];
33+
if (j > 0)
34+
s[i][j] += s[i][j - 1];
35+
if (i > 0 && j > 0)
36+
s[i][j] -= s[i - 1][j - 1];
37+
}
38+
return s;
39+
}
40+
};

0 commit comments

Comments
 (0)