Sort the Jumbled Numbers | 2 Approaches | Leetcode 2191 | codestorywithMIK

Поділитися
Вставка
  • Опубліковано 12 вер 2024

КОМЕНТАРІ • 48

  • @Imrockonn
    @Imrockonn Місяць тому +5

    First like and comment from Hazaribagh The Star City.😁

  • @tusharsingh3063
    @tusharsingh3063 Місяць тому +1

    Unbelievable explanation sir
    Please keep it up
    You are great ❣️🙏🙏🙏

  • @GunjanKumari-up1cs
    @GunjanKumari-up1cs Місяць тому +5

    oh god , second approach was best best!!!

  • @utkarshsahay9908
    @utkarshsahay9908 Місяць тому +2

    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;
    }
    };❤❤❤❤

  • @MrZiyak99
    @MrZiyak99 Місяць тому +1

    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]
    }

  • @Abhi_008
    @Abhi_008 Місяць тому

    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)

  • @gauravbanerjee2898
    @gauravbanerjee2898 Місяць тому +2

    Thanks a lot bhaiya ❤️❤️
    Congrats for 58k subs🥳

  • @nileshsinha7869
    @nileshsinha7869 Місяць тому

    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

  • @_PRANAYMATE
    @_PRANAYMATE Місяць тому

    Thanks well explained brother

  • @gui-codes
    @gui-codes Місяць тому

    2nd approach was really good. i agree, lot of minute things to learn from this video.

  • @_A__Mohit
    @_A__Mohit Місяць тому +2

    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 😊♥️

  • @tusharnanda3885
    @tusharnanda3885 Місяць тому

    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;
    }
    };

  • @chitranshjain9714
    @chitranshjain9714 Місяць тому +1

    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;
    }
    };

    • @Stressful_Jindagi
      @Stressful_Jindagi Місяць тому

      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

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Місяць тому

    Congratulations on 58K

  • @it37shubhamdubey55
    @it37shubhamdubey55 Місяць тому +1

    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.

  • @BhumikaNaik12
    @BhumikaNaik12 Місяць тому +1

    Thank You.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Місяць тому

    Good explanation please update leetcode link🎉❤

  • @abhimanyuyadav3138
    @abhimanyuyadav3138 Місяць тому

    Please can you Make video on Parallel courses 3

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Місяць тому

    Loved it 🫡

  • @allroundervlogs4789
    @allroundervlogs4789 Місяць тому

    Where is this community?

  • @aakasmaity
    @aakasmaity Місяць тому

    I tried implementing lambda sorting, but it failed for some test cases. Could you please explain how lambda sorting works for this problem

    • @siddharthmahato8356
      @siddharthmahato8356 Місяць тому

      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

  • @TheAI-Tutor
    @TheAI-Tutor Місяць тому

    But bhai, instead of pushing the index in the pair, you can also push the nums element na?

    • @HaritAcharya
      @HaritAcharya Місяць тому

      pushing the element would sort wrt vec.second in case of vec.first being equal, hence won't maintain the order in nums.

    • @TheAI-Tutor
      @TheAI-Tutor Місяць тому

      @@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.

    • @HaritAcharya
      @HaritAcharya Місяць тому

      @@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.

    • @TheAI-Tutor
      @TheAI-Tutor Місяць тому

      @@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;
      }
      };

  • @jewelchakraborty9717
    @jewelchakraborty9717 Місяць тому

    /* 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;
    }
    }

  • @SudhanshuGupta-jb1lo
    @SudhanshuGupta-jb1lo Місяць тому

    why u not uploaded yesterday's quest leetcode1636

    • @tyler_burdan
      @tyler_burdan Місяць тому +1

      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

  • @chitranshjain9714
    @chitranshjain9714 Місяць тому

    Bhaiya please solve ,3213 leetcode

  • @theindianshady
    @theindianshady Місяць тому +1

    007,07
    Thala for a reason

  • @newglobal7271
    @newglobal7271 Місяць тому +1

    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

    • @gui-codes
      @gui-codes Місяць тому +1

      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;
      }
      };

    • @newglobal2056
      @newglobal2056 Місяць тому

      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 🙂

    • @gui-codes
      @gui-codes Місяць тому +1

      @@newglobal2056 Acha haan bhai. sahi bola aapne. my bad bro

    • @newglobal2056
      @newglobal2056 Місяць тому +1

      @@gui-codes koi na bro 👍 thanks again

  • @sahebraojadhav9727
    @sahebraojadhav9727 Місяць тому

    Bhai Kari late ho gaya

  • @jewelchakraborty9717
    @jewelchakraborty9717 Місяць тому

    /* 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;
    }
    }