great vid if(num < 10) { return mapping[num] } is not an optimization tho its required. if you have 0 in the original array then without this condition the mappedNum will always be 0. this is because while(num) will never run something like this would acheive the same if(num == 0) { return mapping[0] }
second approach time complexity would be : //T.C : O(n*log(d) + nlogn) , Here, n is the size of the nums vector, and d is the average number of digits in the numbers. //S.C : O(n)
Sir i know your schedule is very hectic but still can you please try to make videos on segment trees because its last video is uploaded on 13th July 🥺and use some problems from codeforces as leetcode have very less amount of problems on segment trees Btw great content 👍 and keep going 😊♥️
class Solution { public: vector sortJumbled(vector& mapping, vector& nums) {
unordered_map mp; for(auto ele:nums) { int val = 0; int n = ele; if(n == 0) { mp[ele] = mapping[ele]; continue;} int mul = 1; while(n) { int unit = n%10; val = mapping[unit]*mul + val; mul *=10; n /=10; }
class Solution { public: // can solve using stack also but this is better int change(int val, unordered_map& mpp) { if (val == 0) { return mpp[0]; } int result = 0; int factor = 1; while (val > 0) { int digit = val % 10; result += mpp[digit] * factor; factor *= 10; val /= 10; } return result; } vector sortJumbled(vector& mapping, vector& nums) { unordered_map mpp; int n = nums.size(); for (int i = 0; i < mapping.size(); i++) { mpp[i] = mapping[i]; } vector vec(n); for (int i = 0; i < nums.size(); i++) { int mapped_number = change(nums[i], mpp); vec[i] = make_pair(mapped_number, nums[i]); } // important auto lambda = [&](const pair& a, const pair& b) { return a.first < b.first; // if(a.first==b.first)return false; // if // we dont use stable sort }; // stable_sort: stable_sort(vec.begin(), vec.end(),lambda); // inherently preserves the relative order of // elements with equivalent values. vector final; for (const auto& it : vec) { final.push_back(it.second); } return final; } };
I just understand the question and solved using java with Comparable approach:----- class Solution { static class Mapping implements Comparable{ int num; int val; int idx; Mapping(int num,int val,int idx){ this.num=num; this.val=val; this.idx=idx; } public int compareTo(Mapping othr){ if(this.valothr.val){ return 1; }else if(this.idxothr.idx ){ return 1; } return 0; } } public int[] sortJumbled(int[] mapping, int[] nums) { int[] res=new int[nums.length]; Listrres=new ArrayList(); for(int i=0;i0){ int dig=val%10; temp+=mapping[dig]*mul; val=val/10; mul*=10; flag=true; } if(temp==0 && flag==false){ temp=mapping[val]; }
rres.add(new Mapping(nums[i],temp,i)); } Collections.sort(rres); int index=0; for(Mapping mp:rres){ res[index++]=mp.num; } return res; } } Hpoe it helps others and some learners like me.
I have implemented lambda sorting on this problem. Though not the fastest or the most elegant but, it works. Code: vector sortJumbled(vector& mapping, vector& nums) { sort(nums.begin(),nums.end(),[mapping](int x, int y) { int a=0,b=0,d=0; if(x==0) a=mapping[0]; if(y==0) b=mapping[0]; while(x>0) { a+=((mapping[x%10])*pow(10,d)); d++; x/=10; } d=0; while(y>0) { b+=((mapping[y%10])*pow(10,d)); d++; y/=10; } return a
@@HaritAcharya pairing the mapped num with actual num instead of index is what I meant. And that being said, we can use a comparator function in case if its equal na.
@@TheAI-Tutor that's what I understood and replied for. Also the comparator would just switch between ascending and descending order but from what I understand we need to maintain the order in nums while being in asc order. Correct me if I'm wrong, in the same boat as you.
@@HaritAcharya the comparator function can handle the equal to cases as well, You may check 23rd July's daily LC as well. Here's my code for today's problem - class Solution { public: int mapit(int n, vector& mapping) { int mult = 1; int mapped_num = 0; if(n==0){ int t = n % 10; n /= 10; int mapped_dig = mapping[t]; mapped_num += (mult * mapped_dig); mult *= 10; } while (n) { int t = n % 10; n /= 10; int mapped_dig = mapping[t]; mapped_num += (mult * mapped_dig); mult *= 10; } return mapped_num; } vector sortJumbled(vector& mapping, vector& nums) { vector v; for (int i = 0; i < nums.size(); i++) { int mapped_value = mapit(nums[i], mapping); v.push_back({mapped_value, nums[i]}); } sort(v.begin(), v.end(), [](const pair& a, const pair& b) { return a.first < b.first; }); for (int i = 0; i < nums.size(); i++) { nums[i] = v[i].second; } return nums; } };
he posted solution on community. it was simple question and similar questions were done before. you can look in community section of his channel if you need help
Sir what is wrong with this code (stoi) 😳 pls see once : -> vector sortJumbled(vector& mapping, vector& nums) { unordered_map mp; for (auto& val : nums) { string temp = ""; int curr = val; if (val == 0) { temp = to_string(mapping[0]); } else { while (val) { int digit = val % 10; temp = to_string(mapping[digit]) + temp; val /= 10; } } mp[curr] = temp; } for (auto& num : mp) { cout
When mapping digits, leading zeros are not correctly handled and preserved. In the example provided, 338 mapped to 007 should result in 7, and 38 mapped to 07 should result in 7 too. Removing leading zeros must be considered when converting mapped strings back to integers. class Solution { public: vector sortJumbled(vector& mapping, vector& nums) { auto getMappedValue = [&](int num) -> long long { string mappedStr = ""; if (num == 0) { mappedStr = to_string(mapping[0]); } else { while (num > 0) { int digit = num % 10; mappedStr = to_string(mapping[digit]) + mappedStr; num /= 10; } } return stoll(mappedStr); }; // Create a vector of pairs (mappedValue, originalIndex) to maintain stable sorting vector mappedNums; for (int i = 0; i < nums.size(); ++i) { mappedNums.push_back({getMappedValue(nums[i]), i}); } // Sort based on the mapped values, maintaining the original index for stable sorting sort(mappedNums.begin(), mappedNums.end()); // Prepare the result array based on the sorted order vector sortedNums; for (const auto& p : mappedNums) { sortedNums.push_back(nums[p.second]); } return sortedNums; } };
Nehi bro jab hum stoi me convert karte hai to wo 007->7 and 07-> 7 ki traha treat karta hai Mere upar ka code ka Sirf ye hi galti tha ki jo auto &val: nums kia hu na usme se & hata na tha BTW thanks for your comments 🙂
/* My Code */ /* I will be glad if this helps anybody */ class Solution { static class Pair implements Comparable{ int val, original, idx; Pair(int v, int o, int i){ this.val = v; this.original = o; this.idx = i; } public int compareTo(Pair second){ if(this.val != second.val){ return Integer.compare(this.val, second.val); } else{ return Integer.compare(this.idx, second.idx); } } } public int f(int curr, int[] map){ if(curr == 0) return map[0]; StringBuilder temp = new StringBuilder(); while(curr != 0){ int rem = curr % 10; temp.append(map[rem]); curr /= 10; } int len = temp.length(); int i = len - 1; while(i >= 0){ if(temp.charAt(i) != '0') break; i--; } StringBuilder str = new StringBuilder(temp.substring(0, i + 1)).reverse(); if(str.length() > 0) return Integer.parseInt(str.toString()); return 0; } public int[] sortJumbled(int[] map, int[] a) { int n = a.length; Pair[] arr = new Pair[n]; for(int i = 0; i < n; i++){ int curr = a[i]; int modVal = f(curr, map); arr[i] = new Pair(modVal, curr, i); } Arrays.sort(arr); for(int i = 0; i < n; i++){ a[i] = arr[i].original; } return a; } }
First like and comment from Hazaribagh The Star City.😁
Unbelievable explanation sir
Please keep it up
You are great ❣️🙏🙏🙏
oh god , second approach was best best!!!
not much
class Solution {
public:
vector sortJumbled(vector& mapping, vector& nums) {
string map = "";
vector mp;
vector ans,res;
for(int n : mapping){
map += to_string(n);
}
for(int n : nums){
string num = "";
string str = to_string(n);
for(char ch : str){
num += map[ch-'0'];
}
ans.push_back(stoi(num));
}
for(int i = 0;i < ans.size(); i++){
mp.push_back({ans[i],i});
}
sort(begin(mp),end(mp));
for(auto vec : mp){
int idx = vec[1];
res.push_back(nums[idx]);
}
return res;
}
};❤❤❤❤
great vid
if(num < 10) {
return mapping[num]
}
is not an optimization tho its required. if you have 0 in the original array then without this condition the mappedNum will always be 0.
this is because while(num) will never run
something like this would acheive the same
if(num == 0) {
return mapping[0]
}
second approach time complexity would be :
//T.C : O(n*log(d) + nlogn) , Here, n is the size of the nums vector, and d is the average number of digits in the numbers.
//S.C : O(n)
Thanks a lot bhaiya ❤️❤️
Congrats for 58k subs🥳
class Solution {
public:
static bool compare(pair &a, pair &b)
{
//if(a.second == b.second) return a.first>b.first;
return (a.second < b.second);
}
int jumble(int num, vector& map)
{
if(num == 0) return map[0];
int nn=0;
int cc=1;
while(num)
{
int rem = num%10;
rem = map[rem];
nn = nn+(rem*cc);
num /= 10;
cc *= 10;
}
return nn;
}
vector sortJumbled(vector& mapping, vector& nums) {
int n = nums.size();
vector vp;
for(int i=0; i
Thanks well explained brother
2nd approach was really good. i agree, lot of minute things to learn from this video.
Hmm
Sir i know your schedule is very hectic but still can you please try to make videos on segment trees because its last video is uploaded on 13th July 🥺and use some problems from codeforces as leetcode have very less amount of problems on segment trees
Btw great content 👍 and keep going 😊♥️
Let me plan soon ❤️🙏
@@codestorywithMIK Ji Bhaiya Please 🙏🙏🙏🙏🙏🙏
Yes Bhaiya ❤❤
class Solution {
public:
vector sortJumbled(vector& mapping, vector& nums) {
unordered_map mp;
for(auto ele:nums)
{
int val = 0;
int n = ele;
if(n == 0)
{ mp[ele] = mapping[ele];
continue;}
int mul = 1;
while(n)
{
int unit = n%10;
val = mapping[unit]*mul + val;
mul *=10;
n /=10;
}
mp[ele] = val;
}
sort(nums.begin() , nums.end() , [&](int &a ,int &b){
return mp[a] < mp[b];
});
return nums;
}
};
class Solution {
public: // can solve using stack also but this is better
int change(int val, unordered_map& mpp) {
if (val == 0) {
return mpp[0];
}
int result = 0;
int factor = 1;
while (val > 0) {
int digit = val % 10;
result += mpp[digit] * factor;
factor *= 10;
val /= 10;
}
return result;
}
vector sortJumbled(vector& mapping, vector& nums) {
unordered_map mpp;
int n = nums.size();
for (int i = 0; i < mapping.size(); i++) {
mpp[i] = mapping[i];
}
vector vec(n);
for (int i = 0; i < nums.size(); i++) {
int mapped_number = change(nums[i], mpp);
vec[i] = make_pair(mapped_number, nums[i]);
}
// important
auto lambda = [&](const pair& a, const pair& b) {
return a.first < b.first; // if(a.first==b.first)return false; // if
// we dont use stable sort
};
// stable_sort:
stable_sort(vec.begin(), vec.end(),lambda); // inherently preserves the relative order of
// elements with equivalent values.
vector final;
for (const auto& it : vec) {
final.push_back(it.second);
}
return final;
}
};
class Solution {
public:
int formNum(int num, unordered_map&mp){
if(num==0) return mp[0];
int temp=0, init=1;
while(num>0){
int digit=num%10;
digit=mp[digit];
temp=init*digit+temp;
init*=10;
num/=10;
}
return temp;
}
vector sortJumbled(vector& map, vector& nums) {
vectorres;
vectorans;
unordered_mapmp; // index vs val
for(int i=0;i
Congratulations on 58K
I just understand the question and solved using java with Comparable approach:-----
class Solution {
static class Mapping implements Comparable{
int num;
int val;
int idx;
Mapping(int num,int val,int idx){
this.num=num;
this.val=val;
this.idx=idx;
}
public int compareTo(Mapping othr){
if(this.valothr.val){
return 1;
}else if(this.idxothr.idx
){
return 1;
}
return 0;
}
}
public int[] sortJumbled(int[] mapping, int[] nums) {
int[] res=new int[nums.length];
Listrres=new ArrayList();
for(int i=0;i0){
int dig=val%10;
temp+=mapping[dig]*mul;
val=val/10;
mul*=10;
flag=true;
}
if(temp==0 && flag==false){
temp=mapping[val];
}
rres.add(new Mapping(nums[i],temp,i));
}
Collections.sort(rres);
int index=0;
for(Mapping mp:rres){
res[index++]=mp.num;
}
return res;
}
}
Hpoe it helps others and some learners like me.
Thank You.
Good explanation please update leetcode link🎉❤
Please can you Make video on Parallel courses 3
Loved it 🫡
Where is this community?
I tried implementing lambda sorting, but it failed for some test cases. Could you please explain how lambda sorting works for this problem
I have implemented lambda sorting on this problem. Though not the fastest or the most elegant but, it works.
Code:
vector sortJumbled(vector& mapping, vector& nums) {
sort(nums.begin(),nums.end(),[mapping](int x, int y) {
int a=0,b=0,d=0;
if(x==0) a=mapping[0];
if(y==0) b=mapping[0];
while(x>0) {
a+=((mapping[x%10])*pow(10,d));
d++;
x/=10;
}
d=0;
while(y>0) {
b+=((mapping[y%10])*pow(10,d));
d++;
y/=10;
}
return a
But bhai, instead of pushing the index in the pair, you can also push the nums element na?
pushing the element would sort wrt vec.second in case of vec.first being equal, hence won't maintain the order in nums.
@@HaritAcharya pairing the mapped num with actual num instead of index is what I meant. And that being said, we can use a comparator function in case if its equal na.
@@TheAI-Tutor that's what I understood and replied for.
Also the comparator would just switch between ascending and descending order but from what I understand we need to maintain the order in nums while being in asc order.
Correct me if I'm wrong, in the same boat as you.
@@HaritAcharya the comparator function can handle the equal to cases as well, You may check 23rd July's daily LC as well.
Here's my code for today's problem -
class Solution {
public:
int mapit(int n, vector& mapping) {
int mult = 1;
int mapped_num = 0;
if(n==0){
int t = n % 10;
n /= 10;
int mapped_dig = mapping[t];
mapped_num += (mult * mapped_dig);
mult *= 10;
}
while (n) {
int t = n % 10;
n /= 10;
int mapped_dig = mapping[t];
mapped_num += (mult * mapped_dig);
mult *= 10;
}
return mapped_num;
}
vector sortJumbled(vector& mapping, vector& nums) {
vector v;
for (int i = 0; i < nums.size(); i++) {
int mapped_value = mapit(nums[i], mapping);
v.push_back({mapped_value, nums[i]});
}
sort(v.begin(), v.end(), [](const pair& a, const pair& b) {
return a.first < b.first;
});
for (int i = 0; i < nums.size(); i++) {
nums[i] = v[i].second;
}
return nums;
}
};
/* Better Approach code */
class Solution {
static class Pair implements Comparable{
int val, idx;
Pair(int v, int i){
this.val = v;
this.idx = i;
}
public int compareTo(Pair second){
if(this.val != second.val){
return Integer.compare(this.val, second.val);
}
else{
return Integer.compare(this.idx, second.idx);
}
}
}
public int f(int curr, int[] map){
if(curr == 0) return map[0];
int mappedNo = 0;
int pow = 1;
while(curr != 0){
int rem = curr % 10;
mappedNo += map[rem] * pow;
pow *= 10;
curr /= 10;
}
return mappedNo;
}
public int[] sortJumbled(int[] map, int[] a) {
int n = a.length;
int[] result = new int[n];
Pair[] arr = new Pair[n];
for(int i = 0; i < n; i++){
int curr = a[i];
int modVal = f(curr, map);
arr[i] = new Pair(modVal, i);
}
Arrays.sort(arr);
for(int i = 0; i < n; i++){
int idx = arr[i].idx;
result[i] = a[idx];
}
return result;
}
}
why u not uploaded yesterday's quest leetcode1636
he posted solution on community. it was simple question and similar questions were done before. you can look in community section of his channel if you need help
Bhaiya please solve ,3213 leetcode
007,07
Thala for a reason
Sir what is wrong with this code (stoi) 😳 pls see once : ->
vector sortJumbled(vector& mapping, vector& nums) {
unordered_map mp;
for (auto& val : nums) {
string temp = "";
int curr = val;
if (val == 0) {
temp = to_string(mapping[0]);
} else {
while (val) {
int digit = val % 10;
temp = to_string(mapping[digit]) + temp;
val /= 10;
}
}
mp[curr] = temp;
}
for (auto& num : mp) {
cout
When mapping digits, leading zeros are not correctly handled and preserved. In the example provided, 338 mapped to 007 should result in 7, and 38 mapped to 07 should result in 7 too. Removing leading zeros must be considered when converting mapped strings back to integers.
class Solution {
public:
vector sortJumbled(vector& mapping, vector& nums) {
auto getMappedValue = [&](int num) -> long long {
string mappedStr = "";
if (num == 0) {
mappedStr = to_string(mapping[0]);
} else {
while (num > 0) {
int digit = num % 10;
mappedStr = to_string(mapping[digit]) + mappedStr;
num /= 10;
}
}
return stoll(mappedStr);
};
// Create a vector of pairs (mappedValue, originalIndex) to maintain stable sorting
vector mappedNums;
for (int i = 0; i < nums.size(); ++i) {
mappedNums.push_back({getMappedValue(nums[i]), i});
}
// Sort based on the mapped values, maintaining the original index for stable sorting
sort(mappedNums.begin(), mappedNums.end());
// Prepare the result array based on the sorted order
vector sortedNums;
for (const auto& p : mappedNums) {
sortedNums.push_back(nums[p.second]);
}
return sortedNums;
}
};
Nehi bro jab hum stoi me convert karte hai to wo 007->7 and 07-> 7 ki traha treat karta hai
Mere upar ka code ka Sirf ye hi galti tha ki jo auto &val: nums kia hu na usme se & hata na tha
BTW thanks for your comments 🙂
@@newglobal2056 Acha haan bhai. sahi bola aapne. my bad bro
@@gui-codes koi na bro 👍 thanks again
Bhai Kari late ho gaya
/* My Code */
/* I will be glad if this helps anybody */
class Solution {
static class Pair implements Comparable{
int val, original, idx;
Pair(int v, int o, int i){
this.val = v;
this.original = o;
this.idx = i;
}
public int compareTo(Pair second){
if(this.val != second.val){
return Integer.compare(this.val, second.val);
}
else{
return Integer.compare(this.idx, second.idx);
}
}
}
public int f(int curr, int[] map){
if(curr == 0) return map[0];
StringBuilder temp = new StringBuilder();
while(curr != 0){
int rem = curr % 10;
temp.append(map[rem]);
curr /= 10;
}
int len = temp.length();
int i = len - 1;
while(i >= 0){
if(temp.charAt(i) != '0') break;
i--;
}
StringBuilder str = new StringBuilder(temp.substring(0, i + 1)).reverse();
if(str.length() > 0) return Integer.parseInt(str.toString());
return 0;
}
public int[] sortJumbled(int[] map, int[] a) {
int n = a.length;
Pair[] arr = new Pair[n];
for(int i = 0; i < n; i++){
int curr = a[i];
int modVal = f(curr, map);
arr[i] = new Pair(modVal, curr, i);
}
Arrays.sort(arr);
for(int i = 0; i < n; i++){
a[i] = arr[i].original;
}
return a;
}
}