Reverse revision of Blind 75 🚀 Just cracked the problem 60 of LeetCode's Blind 75 series! 🎉 🔍 Problem: Set Matrix Zeroes 🔗 Problem link: https://lnkd.in/d72YF3r8 💡 Solution: 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 class Solution { public: void setZeroes(vector<vector<int>>& matrix) { //brute force int m=matrix.size(),n=matrix[0].size(); vector<vector<int>>zeros(m+1,vector<int>(n+1,-1)); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(matrix[i][j]==0) { int new_i=0,new_j=0; while(new_i<m) zeros[new_i++][j]=0; while(new_j<n) zeros[i][new_j++]=0; } } } for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(zeros[i][j]==-1) { zeros[i][j]=matrix[i][j]; } } } for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { matrix[i][j]=zeros[i][j]; } } //better approach int m=matrix.size(),n=matrix[0].size(); vector<int>rows(m+1,0),columns(n+1,0); for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(matrix[i][j]==0) { rows[i]=1; columns[j]=1; } } } for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(rows[i]==1||columns[j]==1) { matrix[i][j]=0; } } } //optimal approach int m=matrix.size(),n=matrix[0].size(); int row0=0,col0=0; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(i==0 && matrix[i][j]==0) row0=1; if(j==0 && matrix[i][j]==0) col0=1; } } for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { if(matrix[i][j]==0) { matrix[0][j]=0; matrix[i][0]=0; } } } for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { if(matrix[i][0]==0 || matrix[0][j]==0) { matrix[i][j]=0; } } } for(int i=0;i<n;i++) if(row0==1) matrix[0][i]=0; for(int i=0;i<m;i++) if(col0==1) matrix[i][0]=0; } }; 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 Excited to dive deeper into the Blind 75 series and tackle more challenges! 💪 #LeetCode #Blind75 #CodingJourney #Programming #CPP
Abhishek Kumar Singh’s Post
More Relevant Posts
-
Reverse revision of Blind 75 🚀 Just cracked the problem 62 of LeetCode's Blind 75 series! 🎉 🔍 Problem: Rotate Image 🔗 Problem link: https://lnkd.in/gHbn76iH 💡 Solution: 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 //Watch this video for better understanding https://lnkd.in/gAaMUt6m class Solution { public: //BRUTE FORCE //create another matrix to store the correct matrix values //TC=O(n^2) //SC=O(n^2) // void rotate(vector<vector<int>>& matrix) { // int n=matrix[0].size(); // int m=matrix.size(); // vector<vector<int>> res(m+1,vector<int>(n+1,0)); // for(int i=0;i<m;i++) // { // for(int j=0;j<n;j++) // { // res[j][n-1-i]=matrix[i][j]; // } // } // for(int i=0;i<m;i++) // { // for(int j=0;j<n;j++) // { // matrix[i][j]=res[i][j]; // } // } // } //Optimized approach //first transpose the matrix and then reverse the columns //TC=O(n^2) //SC=O(1) void rotate(vector<vector<int>>& matrix) { int n=matrix[0].size(); int m=matrix.size(); for(int i=0;i<m;i++) { for(int j=0;j<=i;j++) { swap(matrix[i][j],matrix[j][i]); } } for(int i=0;i<m;i++) { reverse(matrix[i].begin(),matrix[i].end()); } } }; 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 Excited to dive deeper into the Blind 75 series and tackle more challenges! 💪 #LeetCode #Blind75 #CodingJourney #Programming #CPP
To view or add a comment, sign in
-
-
Reverse revision of Blind 75 🚀 Just cracked the problem 51 of LeetCode's Blind 75 series! 🎉 🔍 Problem: Longest consecutive sequence 🔗 Problem link: https://lnkd.in/dtVpSrYu 💡 Solution: 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 class Solution { public: bool search(vector<int>& nums,int x) { for(int i=0;i<nums.size();i++) { if(x==nums[i]) { return true; } } return false; } int longestConsecutive(vector<int>& nums) { //brute force //we'll first stick to 1 element and check // for its next element int longest=0,x=0,count=0; for(int i=0;i<nums.size();i++) { int x=nums[i]; int count=1; while(search(nums,x+1)==true) { count++; x++; } longest=max(longest,count); } return longest; } store all elements in a set and check for the next and previous elements int longestConsecutive(vector<int>& nums) { if(nums.empty()) return 0; unordered_set<int>st; int n=nums.size(),longest=1; for(int i=0;i<n;i++) { st.insert(nums[i]); } for(int i=0;i<n;i++) { if(st.find(nums[i]-1)==st.end()) { int x=nums[i]; int count=1; while(st.find(x+1)!=st.end()) { count++; x++; } longest=max(longest,count); } } return longest; } // optimized approach int longestConsecutive(vector<int>& nums) { if (nums.empty()) return 0; unordered_map<int, bool> vis; int longest = 0, count = 0, vtx = 0; queue<int> q; for (int num : nums) { vis[num] = false; } for (int num : nums) { if (vis.count(num) && !vis[num]) { q.push(num); vis[num] = true; count = 0; while (!q.empty()) { vtx = q.front(); q.pop(); count++; if (vis.count(vtx + 1) && !vis[vtx + 1]) { vis[vtx + 1] = true; q.push(vtx + 1); } if (vis.count(vtx - 1) && !vis[vtx - 1]) { vis[vtx - 1] = true; q.push(vtx - 1); } } longest = max(longest, count); } } return longest; } }; 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 Excited to dive deeper into the Blind 75 series and tackle more challenges! 💪 #LeetCode #Blind75 #CodingJourney #Programming #CPP
To view or add a comment, sign in
-
-
Reverse revision of Blind 75 🚀 Just cracked the problem 55 of LeetCode's Blind 75 series! 🎉 🔍 Problem: Merge Intervals 🔗 Problem link: https://lnkd.in/dyteRgvf 💡 Solution: 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 class Solution { public: //BRUTE FORCE //First sort the vector of vectors //then check for every interval //if first[i][1]>second[i][0] //then merge //otherwise //continue //TC=O(nlogn) + O(2n) //SC=O(n) // vector<vector<int>> merge(vector<vector<int>>& intervals) { // sort(intervals.begin(),intervals.end()); // vector<vector<int>>ans; // for(int i=0;i<intervals.size();i++) // { // int start=intervals[i][0]; // int end=intervals[i][1]; // if(!ans.empty() && end<=ans.back()[1]) // { // continue; // } // for(int j=i+1;j<intervals.size();j++) // { // if(intervals[j][0] <=end ) // { // end=max(end,intervals[j][1]); // } // else // { // break; // } // } // ans.push_back({start,end}); // } // return ans; // } //optimal solution //first sort //then check if the current interval's start is less than ans intervals end //if it is then add the current interval in the merged //otherwise move to next interval //TC=O(nlogn) + O(n) //SC=O(n) vector<vector<int>> merge(vector<vector<int>>& intervals) { vector<vector<int>>ans; sort(intervals.begin(),intervals.end()); int n=intervals.size(); for(int i=0;i<n;i++) { if(ans.empty() || intervals[i][0] > ans.back()[1] ) { ans.push_back(intervals[i]); } else { ans.back()[1]=max(ans.back()[1],intervals[i][1]); } } return ans; } }; 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴 Excited to dive deeper into the Blind 75 series and tackle more challenges! 💪 #LeetCode #Blind75 #CodingJourney #Programming #CPP
To view or add a comment, sign in
-
-
UVM hierarchical structure has been broadly classified into components and objects which are useful in construction of Testbench are essentially part of Directed Acyclic Graph (DAG) Although both of them forms the testbench infrastructure but there are certain differences. Also in this post commonly used classes and macros are discussed which are sufficient enough to build the Testbench. [1] UVM Components are static in nature and they stay in effect for the entire duration of simulation while UVM Objects are transient by nature and their scope of lifetime during the period of simulation is limited. [2] UVM Components are stored in uvm_component_registry class and factory regsitration is done by using `uvm_component_utils macro while UVM objects are stored in uvm_object_registry and factory registration is done by using `uvm_object_utils macro. [3] The entire Testbench hierarchy are constructed using the uvm_component class while objects in case of UVM are the form of transaction or sequence items. [4] UVM Components can access the configuration and other Testbench parameters directly since they are explicitly typed and statically linked to the hierarchy while UVM Objects wouldn't able to directly access the Testbench parameters since they are not tied to an interface and in some cases require the use of p_sequencer. [5] uvm_driver, uvm_monitor, uvm_agent, etc are some of the example of component class in UVM while uvm_sequence_item and uvm_sequence are the examples of object class in UVM. ✓✓Commonly used Class & Macros in UVM: ✔️Class: uvm_driver, uvm_monitor, uvm_agent, uvm_env, uvm_sequence_item, uvm_sequence, uvm_test, uvm_subscriber, uvm_sequencer, uvm_component, uvm_object, uvm_objection, uvm_callback, uvm_callbacks#(T,CB), uvm_pool, uvm_queue, uvm_config_db, uvm_resource_db, uvm_domain, uvm_scoreboard, uvm_transaction, uvm_reg, uvm_mem, uvm_reg_block, uvm_reg_field, uvm_reg_map, uvm_phase, uvm_bottomup_phase, uvm_topdown_phase, uvm_task_phase, uvm_factory, uvm_report_handler, uvm_component_registry, uvm_object_registry, uvm_analysis_port Total = 35 ✔️Macros: `uvm_component_utils, `uvm_object_utils, `uvm_field_int, `uvm_field_array_int, `uvm_field_enum, `uvm_object_param_utils, `uvm_component_param_utils, `uvm_do, `uvm_do_on, `uvm_warning, `uvm_fatal, `uvm_error, `uvm_info, `uvm_analysis_imp_decl, `uvm_field_object, `uvm_declare_p_sequencer Total = 16 #vlsi #asic #electronics #electricalengineering
To view or add a comment, sign in
-
Design "Sath Nibhana Sathiya" Mode: Scan mode Clock: SasuMa's command 2 Clock: Main core clock1: Kokila Clock2: Mota Bhabhi Data: Daughter In Law's action Setup/Hold slack violation: On Viewer's patience Scene: Mother in law takes keys from daughter in law and giving to Sister in law. Data launched with the command "Rashi, Chabi mujhe do" (Rashi, give me keys). Data shifts-in. Now this data has to travel: going through multiple scenes (net) with no combinational action logic. Camera going round & round, round & round, O my god round & round, ...... 30 sec path delay, again round & round.................60 sec, still round & round,. .....................................................3.5 Min. Seriously???. During this beautiful camera rotation technique, also showed expressions of people in between for safety purposes to save us from "Dizziness" . That's all. No combo action. Finally keys (data) gets captured (Mother in law got keys). At this point, hold time of my patience had negative slack. (Thanks to the scroll bar with fast forward option). O do you think, Scan Shift is done??? No no no no . Data which shifts-in (keys) now have to go through more flops and then shifts out. Right? So these keys (shifted data) will get shifted next to her Sister in law ( next scan register) in ANOTHER 3.5 min. My hold patience violation at every stage was increasing. Thank God tool checks violation at every capture stage and reports the worst. Yes It was worst after 2nd shift. This post is the result!!!!! Aha you want to test by yourself?.....Test it!!!! Video: https://lnkd.in/gpVHy7Ce
To view or add a comment, sign in
-
-
#Exp_Fractal_Force_Index - expert advisor for MetaTrader 5 https://lnkd.in/eKf-7HRd # The simplest EA based on the fractal Force_Index. Sells at downward crossing of indicator zero level, buys at the upward crossing of this level. The signal is formed when a bar is closing if there is crossing of the level. The Fractal_Force_Index_HTF indicator in the EA i...
To view or add a comment, sign in
-
#Exp_Fractal_Force_Index - expert advisor for MetaTrader 5 https://lnkd.in/eKf-7HRd # The simplest EA based on the fractal Force_Index. Sells at downward crossing of indicator zero level, buys at the upward crossing of this level. The signal is formed when a bar is closing if there is crossing of the level. The Fractal_Force_Index_HTF indicator in the EA i...
#Exp_Fractal_Force_Index - expert advisor for MetaTrader 5 https://tradingrobots.net/exp_fractal_force_index-expert-advisor-for-metatrader-5/ # The simplest EA based on the fractal Force_Index. Sells at downward crossing of indicator zero level, buys at the upward crossing of this level. The signal is formed when a bar is closing if there is crossing of the level. Th...
https://tradingrobots.net
To view or add a comment, sign in
-
🐀 **Solving the Amazing Rat in a Maze Problem Using Recursion!** 🧀 🔍 In this fascinating problem, we explore the adventures of a brave rat 🐭 navigating through a maze 🏰. The goal is simple: find a path from the starting point to the destination, overcoming obstacles along the way. 🛠️ Using the power of recursion, we've devised a clever algorithm to solve this maze conundrum. Let's dive into the code and see the magic unfold: ```cpp #include <iostream> #include <vector> using namespace std; // A function to determine if a move is safe bool isSafe(int srcx, int srcy, int newx, int newy, int maze[][4], int row, int col, vector<vector<bool>> &visited) { if ( (newx >= 0 && newx < row) && (newy >= 0 && newy < col) && (maze[newx][newy] == 1) && (!visited[newx][newy]) ) { return true; } else { return false; } } // A recursive function to print all possible paths void printAllPaths(int maze[][4], int row, int col, int srcx, int srcy, string &output, vector<vector<bool>> &visited) { // Base case: Reached destination if (srcx == row - 1 && srcy == col - 1) { cout << output << endl; return; } // Explore all possible moves: UP, RIGHT, DOWN, LEFT vector<pair<int, int>> moves = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; vector<char> directions = {'U', 'R', 'D', 'L'}; for (int i = 0; i < 4; ++i) { int newx = srcx + moves[i].first; int newy = srcy + moves[i].second; if (isSafe(srcx, srcy, newx, newy, maze, row, col, visited)) { visited[newx][newy] = true; output.push_back(directions[i]); printAllPaths(maze, row, col, newx, newy, output, visited); output.pop_back(); // Backtrack visited[newx][newy] = false; } } } int main() { // Maze representation int maze[4][4] = { {1, 0, 0, 0}, {1, 1, 0, 0}, {1, 1, 1, 0}, {1, 1, 1, 1} }; int row = 4; int col = 4; // Starting position int srcx = 0; int srcy = 0; // Initialize output string and visited array string output = ""; vector<vector<bool>> visited(row, vector<bool>(col, false)); // Check if starting position is valid if (maze[0][0] == 0) { cout << "No Path Exists" << endl; } else { visited[srcx][srcy] = true; printAllPaths(maze, row, col, srcx, srcy, output, visited); } return 0; } ``` 🎉 Voila! With the power of recursion, our brave rat 🐀 successfully navigates through the maze, finding all possible paths to the cheese 🧀. What an adventure! #RatInAMaze #Recursion #Backtracking #Algorithm #ProblemSolving #Coding #Programming Feel free to like, share, and comment your thoughts below! 🚀
To view or add a comment, sign in