C++ || STL || standard template libraries

Image
                                       C++ STL  1.      Pair :  Syntax :      pair <int,char> p1;     it creates a pair {2,'c'} like this.           pair <int,int> p2;     it creates a pair {2,3} like this .  Basic Code :  #include <bits/stdc++.h> using namespace std; int main() {     // cout<<"hello world\n";     pair <int,int> p1 = {1,2};     cout<<p1.first<< " "<< p1.second<<endl;          pair <int, pair<int ,char>> p2 = {1,{2,'c'}};     cout<<p2.first<<" "<<p2.second.first<<" "<<p2.second.second<<endl;          pair <int,int> arr[] = {{2,3},{4,5},{6,7}};     cout...

Row with maximum ones || Leetcode Solution || Simple Approach || 🔥🔥🔥💯💯💯✅✅✅||

             

                                2643. Row With Maximum Ones


Solution :-

Solving this problem using dictionary concept:

Approach Of solving the problem : 

  1. Create a empty dictionary d = {} , initialise count = 0 , lst = []
  2. Iterate over the mat list, now add dictionary for its index and count
  3. from the dictionary find the maximum value and store it in v_max variable
  4. Now iterate over the dictionary using .items() method
  5. Check condition if(value == v_max) append the key and value to the list and terminate the loop
  6. now, return the list

Complexity

  • Time complexity: 
O(n)

    Since we used for loops 

Code in Python:

class Solution:
    def rowAndMaximumOnes(self, mat: List[List[int]]) -> List[int]:
        count = 0 
        d = {}
        for i in mat:
            d[count] = i.count(1)
            count += 1 
        v_list = d.values()
        v_max = max(v_list)
        lst = []
        for key,value in d.items():
            if(value == v_max):
                lst.append(key)
                lst.append(value)
                break 
        return lst 

Comments

Popular posts from this blog

Power of Millets || Happy And Healthy Life

C++ || STL || standard template libraries

1050. Actors and Directors Who Cooperated At Least Three Times || Leetcode sql solution || Simple and Easy Approach || ✅✅✅✅💯💯💯🔥🔥🔥🔥