Skip to content

Commit a0a070c

Browse files
committed
daily ques
1 parent 756fe6c commit a0a070c

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

Collecting_Numbers_II.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define pb push_back
5+
#define ppb pop_back
6+
#define mp make_pair
7+
#define ff first
8+
#define ss second
9+
#define set_bits __builtin_popcountll
10+
#define sz(x) ((int)(x).size())
11+
#define all(x) (x).begin(), (x).end()
12+
#define nl endl
13+
#define ll long long int
14+
15+
16+
17+
int solve(vector<int> &nums, int m, int n)
18+
{
19+
20+
vector<int> pos(n,0);
21+
int j = 1;
22+
for(auto i : nums)
23+
{
24+
pos[i] = j;
25+
j++;
26+
}
27+
28+
int ans = 1 , curr = 1;
29+
30+
for(int i = 1 ; i <= n ; ++i)
31+
{
32+
if(curr > pos[i]) ans++;
33+
curr = pos[i];
34+
}
35+
36+
return ans;
37+
38+
39+
}
40+
41+
int main()
42+
{
43+
44+
int n, m;
45+
cin>>n>>m;
46+
vector<int> nums(n);
47+
48+
for(int i = 0 ; i < n ; i++)
49+
{
50+
int x;
51+
cin>>x;
52+
nums[i] = x;
53+
}
54+
55+
while(m--)
56+
{
57+
int a,b;
58+
cin>>a>>b;
59+
swap(nums[--a],nums[--b]);
60+
cout<<solve(nums, m , n)<<endl;
61+
}
62+
63+
return 0;
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {
4+
5+
vector<tuple<int, int, int>> mappedList;
6+
for (int i = 0; i < nums.size(); i++) {
7+
string s = to_string(nums[i]);
8+
string n = "";
9+
for (char ch : s) {
10+
n += to_string(mapping[ch - '0']);
11+
}
12+
mappedList.push_back(make_tuple(nums[i], stoi(n), i));
13+
}
14+
15+
16+
sort(mappedList.begin(), mappedList.end(), [](const auto& a, const auto& b) {
17+
if (get<1>(a) != get<1>(b)) {
18+
return get<1>(a) < get<1>(b);
19+
} else {
20+
return get<2>(a) < get<2>(b);
21+
}
22+
});
23+
24+
25+
vector<int> sortedNums(nums.size());
26+
for (int i = 0; i < mappedList.size(); i++) {
27+
sortedNums[i] = get<0>(mappedList[i]);
28+
}
29+
30+
return sortedNums;
31+
}
32+
};

0 commit comments

Comments
 (0)