Пікірлер
@mathematics3398
@mathematics3398 2 күн бұрын
class Solution: def isValid(self, str): ip = str.split(".") if len(ip) != 4: return False for x in ip: #print(f"{len(x)} and {x[0]}") if len(x) >= 2 and x[0] == '0': return False for element in x: if not element.isdigit(): return False int_x = int(x) if int_x < 0 or int_x > 255: return False return True
@mathematics3398
@mathematics3398 2 күн бұрын
class Solution { public: int valid_part(string sub_str) { if (sub_str.length() > 3) return false; if (sub_str.length() >= 2 and sub_str[0] == '0') return false; int x = stoi(sub_str); //cout <<x<<endl; if (x < 0 or x > 255) return false; return true; } int isValid(string str) { int num_dot = 0; string sub_str = ""; for (int i = 0; i < str.length(); i++) { if (str[i] == '.') { num_dot++; if (valid_part(sub_str) == false) return false; sub_str = ""; } else { if (!(str[i] >= '0' and str[i] <= '9')) return false; sub_str += str[i]; } } if (num_dot != 3) return false; if (valid_part(sub_str) == false) return false; return true; } };
@mathematics3398
@mathematics3398 2 күн бұрын
Table of Contents 0:00 Problem Statement 0:39 Solution 2:37 Pseudo Code 4:32 Code - Python 5:40 Code - C++
@mathematics3398
@mathematics3398 2 күн бұрын
from collections import deque class Solution: def bottomView(self, root): ans = [] if not root: return [] q = deque([[root, 0]]) dictionary = dict() start = sys.maxsize while len(q): root, index = q.popleft() if index not in dictionary: dictionary[index] = [] dictionary[index].append(root) start = min(start, index) if root.left: q.append([root.left, index - 1]) if root.right: q.append([root.right, index + 1]) n = len(list(dictionary.keys())) for i in range(start, n): if i in dictionary: ans.append(dictionary[i][-1].data) return ans
@mathematics3398
@mathematics3398 2 күн бұрын
Table of Contents 0:00 Problem Statement 0:42 Solution 4:35 Pseudo Code 8:12 Code
@mathematics3398
@mathematics3398 3 күн бұрын
class Solution: #Function to find the maximum number of meetings that can #be performed in a meeting room. def compare_func(self, value): return value[1] def maximumMeetings(self,n,start,end): time = [] for i in range(n): temp = [start[i], end[i]] time.append(temp) time.sort(key=self.compare_func) #print(time) end_time = -1 ans = 0 for i in range(n): if time[i][0] > end_time: end_time = time[i][1] ans += 1 return ans
@mathematics3398
@mathematics3398 3 күн бұрын
class Solution { public: // Function to find the maximum number of meetings that can // be performed in a meeting room. class Time { public: int start; int end; Time(int s, int e) { start = s; end = e; } int get_start() { return start; } int get_end() { return end; } }; static bool compare(Time x, Time y) { return (x.get_end() < y.get_end()); } int maxMeetings(int n, int start[], int end[]) { vector<Time> t; for (int i = 0; i < n; i++) { Time x = Time(start[i], end[i]); t.push_back(x); //t[i] = x; } sort(t.begin(), t.end(), compare); int ans = 0, end_time = -1; for (int i = 0; i < n; i++) { if (t[i].get_start() > end_time) { end_time = t[i].get_end(); ans++; } } return ans; } };
@mathematics3398
@mathematics3398 3 күн бұрын
Table of Contents 0:00 Problem Statement 0:46 Solution 3:48 Pseudo Code 7:22 Code - Python 8:22 Code - C++
@mathematics3398
@mathematics3398 4 күн бұрын
class Solution: def celebrity(self, mat): i = 0 j = len(mat) - 1 while i < j: if mat[j][i]: j -= 1 else: i += 1 celeb = i #print(celeb) for i in range(len(mat)): if i != celeb: if mat[celeb][i] != 0 or mat[i][celeb] != 1: return -1 return celeb
@mathematics3398
@mathematics3398 4 күн бұрын
class Solution { public: // Function to find if there is a celebrity in the party or not. int celebrity(vector<vector<int> >& mat) { int i = 0, j = mat.size() - 1; while (i < j) { if (mat[j][i]) j--; else i++; } int celeb = i; //cout<<celeb<<endl; for (i = 0; i < mat.size(); i++) { if (i != celeb) { /*cout <<"("<<celeb<<","<<i<<")"<<"= "<<mat[celeb][i]<<endl; cout <<"("<<i<<","<<celeb<<")"<<"= "<<mat[i][celeb]<<endl;*/ if (mat[celeb][i] != 0 or mat[i][celeb] != 1) return -1; } } return celeb; } };
@mathematics3398
@mathematics3398 4 күн бұрын
Table of Contents 0:00 Problem Statement 1:02 Solution 5:47 Pseudo Code 8:07 Code - Python 9:00 Code - C++
@mathematics3398
@mathematics3398 8 күн бұрын
class Solution: def longestCommonPrefix(self, arr): str1 = arr[0] for i in range(1, len(arr)): str2 = arr[i] i = 0 j = 0 prefix = "" while i < len(str1) and j < len(str2) and str1[i] == str2[j]: prefix = prefix + str1[i] i += 1 j += 1 str1 = prefix if len(str1): return str1 return "-1"
@mathematics3398
@mathematics3398 8 күн бұрын
class Solution { public: string longestCommonPrefix(vector<string> arr) { string str1 = arr[0]; for (int i = 1; i < arr.size(); i++) { int j = 0, k = 0; string str2 = arr[i]; string prefix = ""; while (j < str1.length() and k < str2.length() and str1[j] == str2[k]) { prefix += str1[j]; j++; k++; } str1 = prefix; } if (str1.length()) return str1; return "-1"; } };
@mathematics3398
@mathematics3398 8 күн бұрын
Table of Contents 0:00 Problem Statement 0:32 Solution 2:47 Pseudo Code 5:27 Code - Python 6:26 Code - C++
@mathematics3398
@mathematics3398 8 күн бұрын
class Solution: def prepare_path(self, path, index, direction): if index < len(path): path[index] = direction else: path.append(direction) def rat_path(self, m, i, j, path, index): if i > len(m) or i < 0 or j > len(m) or j < 0: return if i == len(m) - 1 and j == len(m) - 1: self.ans.append("".join(path[:index])) m[i][j] = 0 # UP if i - 1 >= 0 and m[i-1][j]: self.prepare_path(path, index, "U") self.rat_path(m, i - 1, j, path, index + 1) # Down if i + 1 < len(m) and m[i+1][j]: self.prepare_path(path, index, "D") self.rat_path(m, i + 1, j, path, index + 1) # LEFT if j - 1 >= 0 and m[i][j - 1]: self.prepare_path(path, index, "L") self.rat_path(m, i, j - 1, path, index + 1) # Right if j + 1 < len(m) and m[i][j + 1]: self.prepare_path(path, index, "R") self.rat_path(m, i, j + 1, path, index + 1) m[i][j] = 1 def findPath(self, m: List[List[int]]) -> List[str]: self.ans = [] if m[0][0] and m[len(m) - 1][len(m) - 1]: self.rat_path(m, 0, 0, [], 0) return self.ans
@mathematics3398
@mathematics3398 8 күн бұрын
Table of Contents 0:00 Problem Statement 1:25 Solution 9:36 Pseudo Code 17:33 Code
@mathematics3398
@mathematics3398 10 күн бұрын
#User function Template for python3 class Solution: def rowWithMax1s(self,arr): n = len(arr) next_j = len(arr[0]) - 1 ans = -1 for i in range(n): j = next_j while j >= 0 and arr[i][j]: ans = i j -= 1 next_j = j return ans
@mathematics3398
@mathematics3398 10 күн бұрын
class Solution { public: int rowWithMax1s(vector<vector<int> > arr) { int ans = -1; int next_j = arr[0].size() - 1; for(int i = 0; i < arr.size(); i++) { int j = next_j; while (j >= 0 and arr[i][j]) { ans = i; j--; } next_j = j; } return ans; } };
@mathematics3398
@mathematics3398 10 күн бұрын
Table of Contents 0:00 Problem Statement 0:41 Solution 3:36 Pseudo Code 5:04 Code - Python 6:01 Code - C++
@mathematics3398
@mathematics3398 11 күн бұрын
class Solution: def removeDups(self, str): duplicates = dict() ans = "" for s in str: if s not in duplicates: duplicates[s] = True ans += s return ans
@mathematics3398
@mathematics3398 11 күн бұрын
class Solution { public: string removeDups(string str) { map<char, bool> duplicates; string ans = ""; for (int i = 0; i < str.length(); i++) { if (duplicates.find(str[i]) == duplicates.end()) { duplicates[str[i]] = true; ans += str[i]; } } return ans; } };
@mathematics3398
@mathematics3398 11 күн бұрын
Table of Contents 0:00 Problem Statement 0:41 Solution 1:08 Code - Python 2:17 Code - C++
@TrinadhKatlagunta
@TrinadhKatlagunta 11 күн бұрын
You Deserve more No of subscribers Buddy, hope it will be faster and thankyou so much, I am lucky as I found a Diamond among all the other videos
@mathematics3398
@mathematics3398 11 күн бұрын
Thanks for your kind words!
@mathematics3398
@mathematics3398 11 күн бұрын
class Solution: def countMin(self, str): n = len(str) dp = [[0 for _ in range(n)] for _ in range(n)] for i in range(1, n): I = 0 for j in range(i, n): if str[I] == str[j]: dp[I][j] = dp[I+1][j-1] else: dp[I][j] = 1 + min(dp[I+1][j], dp[I][j-1]) I += 1 #print(dp) return dp[0][n-1]
@mathematics3398
@mathematics3398 11 күн бұрын
class Solution{ public: int countMin(string str){ int n = str.length(); vector<vector<int>> dp(n, vector<int>(n, 0)); for (int x = 1; x < n; x++) { int i = 0; for (int j = x; j < n; j++) { if (str[i] == str[j]) dp[i][j] = dp[i + 1][j -1]; else dp[i][j] = 1 + min(dp[i+1][j], dp[i][j-1]); i++; } } return dp[0][n-1]; } };
@mathematics3398
@mathematics3398 11 күн бұрын
Table of Contents 0:00 Problem Statement 0:31 Solution 14:11 Pseudo Code 16:07 Code - Python 16:54 Code C++
@mathematics3398
@mathematics3398 15 күн бұрын
class Solution { public: Node *prev; bool is_bst_util(Node *root) { if (!root) return true; if (!is_bst_util(root->left)) return false; if (prev and root->data <= prev->data) return false; prev = root; return is_bst_util(root->right); } bool isBST(Node* root) { prev = NULL; return is_bst_util(root); } };
@mathematics3398
@mathematics3398 15 күн бұрын
class Solution: def is_bst_util(self, root): if not root: return True if not self.is_bst_util(root.left): return False if self.prev and root.data <= self.prev.data: return False self.prev = root return self.is_bst_util(root.right) #Function to check whether a Binary Tree is BST or not. def isBST(self, root): self.prev = None return self.is_bst_util(root)
@mathematics3398
@mathematics3398 15 күн бұрын
Table of Contents 0:00 Problem Statement 0:52 Solution - Python 2:29 Solution - C++
@mathematics3398
@mathematics3398 16 күн бұрын
Table of Contents 0:00 Problem Statement 0:36 Solution
@mathematics3398
@mathematics3398 16 күн бұрын
class Solution: def inorder(self, root, ans): if not root: return self.inorder(root.left, ans) ans.append(root.data) self.inorder(root.right, ans) def merge(self, root1, root2): l1, l2 = [], [] self.inorder(root1, l1) self.inorder(root2, l2) return sorted(l1 + l2)
@mathematics3398
@mathematics3398 18 күн бұрын
class Solution: def RemoveHalfNodes(self, root): if root is None: return None root.left = self.RemoveHalfNodes(root.left) root.right = self.RemoveHalfNodes(root.right) if root.left is None and root.right is None: return root if root.left is None: return root.right if root.right is None: return root.left return root
@mathematics3398
@mathematics3398 18 күн бұрын
class Solution { public: Node *RemoveHalfNodes(Node *root) { if (!root) return root; root->left = RemoveHalfNodes(root->left); root->right = RemoveHalfNodes(root->right); if (!root->left and !root->right) return root; if (!root->left) return root->right; if (!root->right) return root->left; return root; } };
@mathematics3398
@mathematics3398 18 күн бұрын
Table of Contents 0:00 Problem Statement 0:41 Solution 7:22 Pseudo Code 10:08 Code - Python 10:59 Code - C++
@tanishksarode601
@tanishksarode601 19 күн бұрын
You got a new subscriber 😇
@mathematics3398
@mathematics3398 18 күн бұрын
Thanks and welcome!
@mathematics3398
@mathematics3398 19 күн бұрын
class Solution: def merge(self, array, ans, start, mid, end): temp = [] i = start j = mid + 1 while i <= mid and j <= end: if array[i][0] > array[j][0]: ans[array[i][1]] += (end - j + 1) temp.append(array[i]) i += 1 else: temp.append(array[j]) j += 1 while i <= mid: temp.append(array[i]) i += 1 while j <= end: temp.append(array[j]) j += 1 i = start j = 0 while i <= end: array[i] = temp[j] i += 1 j += 1 #print(temp) def merge_sort(self, array, ans, start, end): if start < end: mid = start + (end - start)//2 self.merge_sort(array, ans, start, mid) self.merge_sort(array, ans, mid+1, end) self.merge(array, ans, start, mid, end) def constructLowerArray(self,arr): temp_arr = [] ans = [0 for _ in range(len(arr))] for i in range(len(arr)): temp_arr.append([arr[i], i]) #print(temp_arr) self.merge_sort(temp_arr, ans, 0, len(arr) - 1) return ans
@mathematics3398
@mathematics3398 19 күн бұрын
Table of Contents 0:00 Problem Statement 0:47 Solution 7:16 Pseudo Code 16:04 Code
@mathematics3398
@mathematics3398 21 күн бұрын
class Solution: #Function to construct binary tree from parent array. def createTree(self,parent): dp = dict() root = None for i in range(len(parent)): if i in dp: node = dp[i] else: node = Node(i) dp[i] = node if parent[i] == -1: root = node PARENT = None if parent[i] in dp: PARENT = dp[parent[i]] elif parent[i] != -1: PARENT = Node(parent[i]) dp[parent[i]] = PARENT if PARENT: if not PARENT.left: PARENT.left = node else: PARENT.right = node #for key in dp: # print(dp[key].key, end=" ") #print(" ") return root
@mathematics3398
@mathematics3398 21 күн бұрын
class Solution { public: Node *create_node(int key) { Node *node = (Node*)calloc(1, sizeof(Node*)); node->data = key; node->left = node->right = NULL; return node; } // Function to construct binary tree from parent array. Node *createTree(vector<int> parent) { map<int, Node*> dp; Node *root = NULL; for (int i = 0; i < parent.size(); i++) { Node *node = NULL; Node *PARENT = NULL; if (dp.find(i) == dp.end()) { node = create_node(i); dp.insert({i, node}); } else node = dp[i]; if (parent[i] == -1) root = node; if (parent[i] != -1) { if (dp.find(parent[i]) == dp.end()) { PARENT = create_node(parent[i]); dp.insert({parent[i], PARENT}); } else PARENT = dp[parent[i]]; } if (PARENT) { if (not PARENT->left) PARENT->left = node; else PARENT->right = node; } } return root; } };
@mathematics3398
@mathematics3398 21 күн бұрын
Table of Contents 0:00 Problem Statement 1:37 Solution 7:03 Pseudo Code 11:15 Code Python 12:56 Code C++
@mathematics3398
@mathematics3398 23 күн бұрын
class Solution { public: string printString(string s, char ch, int count) { string ans = ""; int index = -1; for (int i = 0; i < s.length(); i++) { if (ch == s[i]) count--; if (count == 0) { index = i + 1; break; } } if (index == -1 or index >= s.length()) return ans; for (int i = index; i < s.length(); i++) ans += s[i]; return ans; } };
@mathematics3398
@mathematics3398 23 күн бұрын
class Solution: def printString(self, s, ch, count): index = -1 for i in range(len(s)): if s[i] == ch: count -= 1 if count == 0: index = i + 1 break if index == -1 or index >= len(s): return "" return s[index:]
@mathematics3398
@mathematics3398 23 күн бұрын
Table of Contents 0:00 Problem Statement 0:53 Python Code
@naveenr5513
@naveenr5513 23 күн бұрын
Yes sir please do continue
@SaurabhSingh-in7lr
@SaurabhSingh-in7lr 23 күн бұрын
Please contuinue this series very helpful sir
@mathematics3398
@mathematics3398 23 күн бұрын
class Solution: def smallestNumber(self, s, d): if s < 1 or s > 9*d: return "-1" ans = "" while d: if d > 1: x1 = s - 1 else: x1 = s if x1 > 9: x = 9 else: x = x1 ans = str(x) + ans d -= 1 s -= x return ans
@mathematics3398
@mathematics3398 23 күн бұрын
class Solution { public: string smallestNumber(int s, int d) { if (s < 1 or s > 9*d) return "-1"; string ans = ""; while (d) { int x1; int x; if (d > 1) x1 = s - 1; else x1 = s; if (x1 > 9) x = 9; else x = x1; ans = to_string(x) + ans; s -= x; d--; } return ans; } };
@mathematics3398
@mathematics3398 23 күн бұрын
Table of Contents 0:00 Problem Statement 0:38 Solution 9:04 Pseudo Code 10:39 Code Python 11:26 Code C++
@SaurabhSingh-in7lr
@SaurabhSingh-in7lr 23 күн бұрын
Please sir contuinue this series its very helpful
@mathematics3398
@mathematics3398 25 күн бұрын
Table of Contents 0:00 Problem Statement 0:32 Python Code 3:18 C++ Code