- 320
- 22 931
ExpertFunda
India
Registrace 9. 06. 2022
ExpertFunda is the right place for the preparation of Leetcode daily questions and their solution in multiple approaches with the best time complexity and space complexity.
Learning Data Structure and Algorithm is very important for the product base Interview. I started preparation a few months back before starting this ExpertFunda, and have found that there is no such place that can guide us in deep skill in DSA. If we found some discussion form that takes time to filter who is providing the correct approaches with a better solution for the such interview?
I have planned to provide the daily solution in this journey and will keep posting the daily video on our youtube channel, as well as I will share the post on this website. Please subscribe to the channel and help me in growing by liking and share of daily videos. I will share my LinkedIn profile as well as my offical ExpertFunda profile.
Please DM me on LinkedIn for any help and comment me on CZcams for better reach.
Learning Data Structure and Algorithm is very important for the product base Interview. I started preparation a few months back before starting this ExpertFunda, and have found that there is no such place that can guide us in deep skill in DSA. If we found some discussion form that takes time to filter who is providing the correct approaches with a better solution for the such interview?
I have planned to provide the daily solution in this journey and will keep posting the daily video on our youtube channel, as well as I will share the post on this website. Please subscribe to the channel and help me in growing by liking and share of daily videos. I will share my LinkedIn profile as well as my offical ExpertFunda profile.
Please DM me on LinkedIn for any help and comment me on CZcams for better reach.
Video
Check if Two Chessboard Squares Have the Same Color || Weekly Contest 413
zhlédnutí 131Před 16 hodinami
Weekly Contest 413
Q3. Find the Count of Good Integers || Biweekly Contest 138 || TLE
zhlédnutí 268Před 16 hodinami
You are given two positive integers n and k. An integer x is called k-palindromic if: x is a palindrome. x is divisible by k. An integer is called good if its digits can be rearranged to form a k-palindromic integer. For example, for k = 2, 2020 can be rearranged to form the k-palindromic integer 2002, whereas 1010 cannot be rearranged to form a k-palindromic integer. Return the count of good i...
Q1. Find the Key of the Numbers || Biweekly Contest 138
zhlédnutí 16Před 16 hodinami
You are given three positive integers num1, num2, and num3. The key of num1, num2, and num3 is defined as a four-digit number such that:...
Q2. Hash Divided String || Biweekly Contest 138
zhlédnutí 41Před 16 hodinami
You are given a string s of length n and an integer k, where n is a multiple of k. Your task is to hash the string s into a new string called result, which has a length of n / k. First, divide s into n / k substrings , each with a length of k. Then, initialize result as an empty string. For each substring in order from the beginning: The hash value of a character is the index of that character ...
Odd Even Jump || Leetcode || Asked in Google Interview || Improved code
zhlédnutí 27Před dnem
You are given an integer array arr. From some starting index, you can make a series of jumps. The 1st, 3rd, 5th, ... jumps in the series are called odd-numbered jumps, and the (2nd, 4th, 6th, ... jumps in the series are called even-numbered jumps. Note that the jumps are numbered, not the indices. ... Return the number of good starting indices.
Bellman-Ford Algorithm - Single Source Shortest Path | Dynamic Programming
zhlédnutí 11Před 14 dny
The Bellman-Ford algorithm: How to find the shortest path in a graph
Dijkstra's Algorithm Implementation in Java | Step-by-Step Java Program
zhlédnutí 14Před 14 dny
Dijkstra's Algorithm Implementation in Detail Using Java Program
How to traverse a graph in depth first search || Java
zhlédnutí 6Před 14 dny
DFS graph traversal: a hands-on tutorial
Q2. Maximum Energy Boost From Two Drinks || Weekly Contest 411
zhlédnutí 448Před 21 dnem
Q2. Maximum Energy Boost From Two Drinks
Maximum Value Sum by Placing Three Rooks I
zhlédnutí 44Před 21 dnem
Maximum Value Sum by Placing Three Rooks I
Count Substrings That Satisfy K-Constraint I
zhlédnutí 191Před 21 dnem
Count Substrings That Satisfy K-Constraint I
Q1. Find the Power of K-Size Subarrays I || Biweekly Contest 137
zhlédnutí 709Před 21 dnem
Q1. Find the Power of K-Size Subarrays I || Biweekly Contest 137 You are given an array of integers nums of length n and a positive integer k. The power of an array is defined as: Its maximum element if all of its elements are consecutive and sorted in ascending order. -1 otherwise. You need to find the power of all subarrays of nums of size k. Return an integer array results of size n - k 1, w...
Breadth First Search or BFS for a Graph || Java
zhlédnutí 11Před 21 dnem
Breadth First Search (BFS) is a fundamental graph traversal algorithm.
How Adjacency List Works in Graphs || Java
zhlédnutí 12Před 21 dnem
How Adjacency List Works in Graphs || Java
3250. Find the Count of Monotonic Pairs I
zhlédnutí 57Před 21 dnem
3250. Find the Count of Monotonic Pairs I
Leetcode 3251. Find the Count of Monotonic Pairs II
zhlédnutí 58Před 21 dnem
Leetcode 3251. Find the Count of Monotonic Pairs II
Basics of Graph || Types of Graph || Graph Terminology || Representations of Graphs
zhlédnutí 13Před 28 dny
Basics of Graph || Types of Graph || Graph Terminology || Representations of Graphs
Balanced Binary Tree VS Unbalanced Binary Tree || Detail Explanation
zhlédnutí 58Před 28 dny
Balanced Binary Tree VS Unbalanced Binary Tree || Detail Explanation
Leetcode Weekly Contest 409 || Q3. Shortest Distance After Road Addition Queries II
zhlédnutí 17Před měsícem
Leetcode Weekly Contest 409 || Q3. Shortest Distance After Road Addition Queries II
Leetcode 3240. Minimum Number of Flips to Make Binary Grid Palindromic II Contest’s 136
zhlédnutí 86Před měsícem
Leetcode 3240. Minimum Number of Flips to Make Binary Grid Palindromic II Contest’s 136
Leetcode Q3. Shortest Distance After Road Addition Queries II
zhlédnutí 989Před měsícem
Leetcode Q3. Shortest Distance After Road Addition Queries II
Leetcode Q2. Shortest Distance After Road Addition Queries I (Weekly Contest 409)
zhlédnutí 631Před měsícem
Leetcode Q2. Shortest Distance After Road Addition Queries I (Weekly Contest 409)
Leetcode Q1. Design Neighbor Sum Service || Weekly Contest 409
zhlédnutí 207Před měsícem
Leetcode Q1. Design Neighbor Sum Service || Weekly Contest 409
Find the Number of Winning Players || Minimum Number of Flips to Make Binary Grid Palindromic
zhlédnutí 359Před měsícem
Find the Number of Winning Players || Minimum Number of Flips to Make Binary Grid Palindromic
Leetcode 404. Sum of Left Leaves(Recursive and Iterative Approaches) || Must Watch | FAANG Asked
zhlédnutí 13Před měsícem
Leetcode 404. Sum of Left Leaves(Recursive and Iterative Approaches) || Must Watch | FAANG Asked
good work plz keep uploading regularly. i was looking for a Java based LC contest videos. subscribed
nice
Java code used in the video:- class Solution { public String stringHash(String s, int k) { int n = s.length(); StringBuilder result = new StringBuilder(); // Iterate through each substring for (int i = 0; i < n; i += k) { String substring = s.substring(i, i + k); int sum = 0; // Calculate the sum of hash values of the characters in the substring for (char c : substring.toCharArray()) { sum += c - 'a'; // 'a' → 0, 'b' → 1, ..., 'z' → 25 } // Find the hashed character int hashedChar = sum % 26; char hashedCharacter = (char) ('a' + hashedChar); // Append the hashed character to the result result.append(hashedCharacter); } return result.toString(); } }
Java Code Used in the program:- class Solution { public int generateKey(int num1, int num2, int num3) { // Convert numbers to strings and pad with leading zeros if necessary String s1 = String.format("%04d", num1); String s2 = String.format("%04d", num2); String s3 = String.format("%04d", num3); StringBuilder key = new StringBuilder(); // Iterate through each digit position (1 to 4) for (int i = 0; i < 4; i++) { // Find the smallest digit at the ith position char minDigit = (char) Math.min(s1.charAt(i), Math.min(s2.charAt(i), s3.charAt(i))); key.append(minDigit); } // Convert the resulting key to an integer to remove any leading zeros return Integer.parseInt(key.toString()); } }
Please correct TLE before push to leetcode ==================================== import java.util.*; class Solution { private boolean isPalindrome(String s) { int i = 0, j = s.length() - 1; while (i < j) { if (s.charAt(i) != s.charAt(j)) { return false; } i++; j--; } return true; } private boolean canRearrangeToPalindromeAndDivisible(String s, int k) { // Generate all permutations char[] arr = s.toCharArray(); Arrays.sort(arr); do { String perm = new String(arr); if (perm.charAt(0) != '0' && isPalindrome(perm) && Long.parseLong(perm) % k == 0) { return true; } } while (nextPermutation(arr)); return false; } private boolean nextPermutation(char[] arr) { int i = arr.length - 2; while (i >= 0 && arr[i] >= arr[i + 1]) { i--; } if (i < 0) { return false; } int j = arr.length - 1; while (arr[j] <= arr[i]) { j--; } swap(arr, i, j); reverse(arr, i + 1); return true; } private void swap(char[] arr, int i, int j) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } private void reverse(char[] arr, int start) { int end = arr.length - 1; while (start < end) { swap(arr, start, end); start++; end--; } } public long countGoodIntegers(int n, int k) { long count = 0; int start = (int)Math.pow(10, n - 1); int end = (int)Math.pow(10, n) - 1; for (int i = start; i <= end; i++) { if (canRearrangeToPalindromeAndDivisible(String.valueOf(i), k)) { count++; } } return count; } }
why we are doing ress++ only when higher[i] and not for lower[i] it should be OR btwn them know?
If higher[i] is true, it means starting from index i with an odd jump can eventually reach the end of the array, so we increment res.
Answer to Your Question: Why increment res only when higher[i] is true and not for lower[i]? The problem statement defines a "good" starting index as one from which you can reach the end by making some number of jumps. The first jump from any index is always an odd-numbered jump. Therefore, we are only interested in whether the odd jump (higher[i]) from that index can eventually lead to the end of the array. If higher[i] is true, it means starting at index i, you can reach the end by following the defined jumping rules. lower[i] being true is relevant only for subsequent jumps (even-numbered jumps), but it doesn't directly determine if i is a good starting index. If you were to increment res for lower[i], you would incorrectly count indices that can only be reached via even-numbered jumps but not as starting points themselves.
class Solution { public int countKConstraintSubstrings(String s, int k) { int n = s.length(); int count = 0; // Iterate through all possible substrings for (int i = 0; i < n; i++) { int zeroCount = 0; int oneCount = 0; // Expand the substring for (int j = i; j < n; j++) { // Update counts based on the current character if (s.charAt(j) == '0') { zeroCount++; } else { oneCount++; } // Check if the current substring satisfies the k-constraint if (zeroCount <= k || oneCount <= k) { count++; } } } return count; } }
share 3rd and 4th in comments
it is not accepting sir
grt bro
❤
Yeah it was great video
Thank you so much 😀
Thank you for the explanation.
import java.util.HashMap; class Solution { public int[] shortestDistanceAfterQueries(int n, int[][] queries) { // Result array to store answers for each query int[] ans = new int[queries.length]; // Map to store the largest node reachable from a given node HashMap<Integer, Integer> map = new HashMap<>(); // Initialize the map to link each node to its immediate successor for (int i = 0; i < n - 1; i++) { map.put(i, i + 1); } // Process each query for (int i = 0; i < queries.length; i++) { int u = queries[i][0]; int v = queries[i][1]; // If u is not in the map or its range already includes v, the map size remains the same if (!map.containsKey(u) || map.get(u) >= v) { ans[i] = map.size(); continue; } // Remove all nodes between u and v (not including v) int end = map.get(u); while (end < v) { end = map.remove(end); } // Update the range for u map.put(u, v); // Store the result which is the current size of the map ans[i] = map.size(); } return ans; } }
why are u sahring video if it is giving tle?????
import java.util.*; class Solution { public int[] shortestDistanceAfterQueries(int n, int[][] queries) { // Create an adjacency list to represent the graph List<List<int[]>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } // Initially add the default roads for (int i = 0; i < n - 1; i++) { graph.get(i).add(new int[]{i + 1, 1}); // road to i+1 with weight 1 } // Result array to store shortest path lengths after each query int[] result = new int[queries.length]; // Process each query for (int i = 0; i < queries.length; i++) { int[] query = queries[i]; int u = query[0]; int v = query[1]; // Add the new road to the graph graph.get(u).add(new int[]{v, 1}); // road to v with weight 1 // Perform Dijkstra's algorithm to find the shortest path from city 0 to city n-1 result[i] = dijkstra(graph, n); } return result; } private int dijkstra(List<List<int[]>> graph, int n) { // Distance array to keep track of shortest distance from source int[] dist = new int[n]; Arrays.fill(dist, Integer.MAX_VALUE); dist[0] = 0; // Priority queue for Dijkstra's algorithm (min-heap) PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1])); pq.offer(new int[]{0, 0}); // {city, distance} while (!pq.isEmpty()) { int[] current = pq.poll(); int node = current[0]; int distance = current[1]; if (distance > dist[node]) continue; for (int[] neighbor : graph.get(node)) { int nextNode = neighbor[0]; int weight = neighbor[1]; if (dist[node] + weight < dist[nextNode]) { dist[nextNode] = dist[node] + weight; pq.offer(new int[]{nextNode, dist[nextNode]}); } } } // The shortest path to city n-1 return dist[n - 1] == Integer.MAX_VALUE ? -1 : dist[n - 1]; } }
@@expertfunda isnt it still giving tle??
@@expertfunda solve it using segment tree
/*I tried with wellman and dikshetra still getting problem, and I will fix and will provide the code */ import java.util.*; public class Solution { // Function to perform BFS and find shortest path from src to dest private int bfs(int src, int dest, Map<Integer, List<Integer>> graph, int n) { Queue<Integer> queue = new LinkedList<>(); boolean[] visited = new boolean[n]; int[] distance = new int[n]; Arrays.fill(distance, -1); queue.offer(src); visited[src] = true; distance[src] = 0; while (!queue.isEmpty()) { int node = queue.poll(); if (node == dest) return distance[node]; for (int neighbor : graph.get(node)) { if (!visited[neighbor]) { visited[neighbor] = true; queue.offer(neighbor); distance[neighbor] = distance[node] + 1; } } } return -1; // If no path is found } public int[] shortestDistanceAfterQueries(int n, int[][] queries) { // Initialize graph Map<Integer, List<Integer>> graph = new HashMap<>(); for (int i = 0; i < n; i++) { graph.put(i, new ArrayList<>()); } for (int i = 0; i < n - 1; i++) { graph.get(i).add(i + 1); } int[] result = new int[queries.length]; for (int i = 0; i < queries.length; i++) { // Add new road from queries[i][0] to queries[i][1] int u = queries[i][0]; int v = queries[i][1]; graph.get(u).add(v); // Compute shortest path from 0 to n - 1 result[i] = bfs(0, n - 1, graph, n); } return result; } }
yes, i will provide the fix
Bro can i get solution for the code
give
@@hawkop2386 import java.util.*; class Solution { public int[] shortestDistanceAfterQueries(int n, int[][] queries) { // Create an adjacency list to represent the graph List<List<int[]>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } // Initially add the default roads for (int i = 0; i < n - 1; i++) { graph.get(i).add(new int[]{i + 1, 1}); // road to i+1 with weight 1 } // Result array to store shortest path lengths after each query int[] result = new int[queries.length]; // Process each query for (int i = 0; i < queries.length; i++) { int[] query = queries[i]; int u = query[0]; int v = query[1]; // Add the new road to the graph graph.get(u).add(new int[]{v, 1}); // road to v with weight 1 // Perform Dijkstra's algorithm to find the shortest path from city 0 to city n-1 result[i] = dijkstra(graph, n); } return result; } private int dijkstra(List<List<int[]>> graph, int n) { // Distance array to keep track of shortest distance from source int[] dist = new int[n]; Arrays.fill(dist, Integer.MAX_VALUE); dist[0] = 0; // Priority queue for Dijkstra's algorithm (min-heap) PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1])); pq.offer(new int[]{0, 0}); // {city, distance} while (!pq.isEmpty()) { int[] current = pq.poll(); int node = current[0]; int distance = current[1]; if (distance > dist[node]) continue; for (int[] neighbor : graph.get(node)) { int nextNode = neighbor[0]; int weight = neighbor[1]; if (dist[node] + weight < dist[nextNode]) { dist[nextNode] = dist[node] + weight; pq.offer(new int[]{nextNode, dist[nextNode]}); } } } // The shortest path to city n-1 return dist[n - 1] == Integer.MAX_VALUE ? -1 : dist[n - 1]; } }
import java.util.HashMap; class Solution { public int[] shortestDistanceAfterQueries(int n, int[][] queries) { // Result array to store answers for each query int[] ans = new int[queries.length]; // Map to store the largest node reachable from a given node HashMap<Integer, Integer> map = new HashMap<>(); // Initialize the map to link each node to its immediate successor for (int i = 0; i < n - 1; i++) { map.put(i, i + 1); } // Process each query for (int i = 0; i < queries.length; i++) { int u = queries[i][0]; int v = queries[i][1]; // If u is not in the map or its range already includes v, the map size remains the same if (!map.containsKey(u) || map.get(u) >= v) { ans[i] = map.size(); continue; } // Remove all nodes between u and v (not including v) int end = map.get(u); while (end < v) { end = map.remove(end); } // Update the range for u map.put(u, v); // Store the result which is the current size of the map ans[i] = map.size(); } return ans; } }
import java.util.HashMap; class Solution { public int[] shortestDistanceAfterQueries(int n, int[][] queries) { // Result array to store answers for each query int[] ans = new int[queries.length]; // Map to store the largest node reachable from a given node HashMap<Integer, Integer> map = new HashMap<>(); // Initialize the map to link each node to its immediate successor for (int i = 0; i < n - 1; i++) { map.put(i, i + 1); } // Process each query for (int i = 0; i < queries.length; i++) { int u = queries[i][0]; int v = queries[i][1]; // If u is not in the map or its range already includes v, the map size remains the same if (!map.containsKey(u) || map.get(u) >= v) { ans[i] = map.size(); continue; } // Remove all nodes between u and v (not including v) int end = map.get(u); while (end < v) { end = map.remove(end); } // Update the range for u map.put(u, v); // Store the result which is the current size of the map ans[i] = map.size(); } return ans; } }
Already done palindromic || nhi hua
import java.util.HashMap; class Solution { public int[] shortestDistanceAfterQueries(int n, int[][] queries) { // Result array to store answers for each query int[] ans = new int[queries.length]; // Map to store the largest node reachable from a given node HashMap<Integer, Integer> map = new HashMap<>(); // Initialize the map to link each node to its immediate successor for (int i = 0; i < n - 1; i++) { map.put(i, i + 1); } // Process each query for (int i = 0; i < queries.length; i++) { int u = queries[i][0]; int v = queries[i][1]; // If u is not in the map or its range already includes v, the map size remains the same if (!map.containsKey(u) || map.get(u) >= v) { ans[i] = map.size(); continue; } // Remove all nodes between u and v (not including v) int end = map.get(u); while (end < v) { end = map.remove(end); } // Update the range for u map.put(u, v); // Store the result which is the current size of the map ans[i] = map.size(); } return ans; } }
it will be more beneficial if you would write the code line by line and explain simultaneously.
Thanks I will do
Great 👍 excellent
Thank you! Cheers!
try to give intution instead of jusst reading the question and solution
Problem Intuition: You are given an array where you can jump between indices following specific rules: On odd-numbered jumps, you must jump to a higher or equal value. On even-numbered jumps, you must jump to a lower or equal value. Your goal is to determine how many starting indices allow you to reach the end of the array by following these jumping rules. Example: Consider the array [10, 13, 12, 14, 15]. Starting from index 0, the first jump (odd) must go to a higher value. The smallest higher value after 10 is 12, found at index 2. From 12, you would attempt an even jump to a smaller value, but there isn’t any, so you get stuck. Starting from index 3, the first jump (odd) can go to the value 15, reaching the end of the array. Approach Intuition: 1. Backward Thinking: Start by thinking backward from the end of the array: If you start at the last index, you’re already at the end-so it's trivially a "good" index. For each previous index, determine if you can reach a "good" index by following the jumping rules. 2. Dynamic Decision Making: For each index i, you need to answer: Odd Jump: Can I jump to a higher value and eventually reach the end? Even Jump: Can I jump to a lower value and eventually reach the end? These questions are interdependent. If an odd jump from i reaches a "good" index that is itself reachable by an even jump, then i is a "good" index. 3. Efficient Calculation with TreeMap: To efficiently find the smallest/largest values you can jump to: Use a TreeMap to maintain a sorted order of values as you process the array. The ceilingEntry gives the smallest higher/equal value (for odd jumps). The floorEntry gives the largest lower/equal value (for even jumps). 4. Why Only Count Odd Jumps (higher[i])? The first jump from any index will always be an odd jump. If you can make a valid odd jump (higher[i]), you have a path to reach the end, so you count this index as "good." Even jumps (lower[i]) are crucial for decision-making but don’t determine if an index is "good" as a starting point.
Nice bro keep Going
I will try my best. Thank you bro
the explaination of solution was very confusing, please give us the idea behind the solution and the first thought process, if possible.
Thanks for letting me know I will update it
Problem Intuition: You are given an array where you can jump between indices following specific rules: On odd-numbered jumps, you must jump to a higher or equal value. On even-numbered jumps, you must jump to a lower or equal value. Your goal is to determine how many starting indices allow you to reach the end of the array by following these jumping rules. Example: Consider the array [10, 13, 12, 14, 15]. Starting from index 0, the first jump (odd) must go to a higher value. The smallest higher value after 10 is 12, found at index 2. From 12, you would attempt an even jump to a smaller value, but there isn’t any, so you get stuck. Starting from index 3, the first jump (odd) can go to the value 15, reaching the end of the array. Approach Intuition: 1. Backward Thinking: Start by thinking backward from the end of the array: If you start at the last index, you’re already at the end-so it's trivially a "good" index. For each previous index, determine if you can reach a "good" index by following the jumping rules. 2. Dynamic Decision Making: For each index i, you need to answer: Odd Jump: Can I jump to a higher value and eventually reach the end? Even Jump: Can I jump to a lower value and eventually reach the end? These questions are interdependent. If an odd jump from i reaches a "good" index that is itself reachable by an even jump, then i is a "good" index. 3. Efficient Calculation with TreeMap: To efficiently find the smallest/largest values you can jump to: Use a TreeMap to maintain a sorted order of values as you process the array. The ceilingEntry gives the smallest higher/equal value (for odd jumps). The floorEntry gives the largest lower/equal value (for even jumps). 4. Why Only Count Odd Jumps (higher[i])? The first jump from any index will always be an odd jump. If you can make a valid odd jump (higher[i]), you have a path to reach the end, so you count this index as "good." Even jumps (lower[i]) are crucial for decision-making but don’t determine if an index is "good" as a starting point.
Nice, just what I needed
thank u
Can u take the hard version of this problem too please? (Its in leetcode)
Sure I’ll post link here Thank you
class Solution { public int superEggDrop(int k, int n) { // DP array to store the minimum attempts needed for i eggs and j floors int[][] dp = new int[k + 1][n + 1]; // Initialize base cases for (int i = 1; i <= k; i++) { dp[i][0] = 0; // 0 floors -> 0 attempts dp[i][1] = 1; // 1 floor -> 1 attempt } for (int j = 1; j <= n; j++) { dp[1][j] = j; // 1 egg -> j attempts for j floors } // Fill the DP table for (int i = 2; i <= k; i++) { for (int j = 2; j <= n; j++) { dp[i][j] = Integer.MAX_VALUE; for (int x = 1; x <= j; x++) { int broken = dp[i - 1][x - 1]; // Egg breaks int notBroken = dp[i][j - x]; // Egg doesn't break int worst = 1 + Math.max(broken, notBroken); dp[i][j] = Math.min(dp[i][j], worst); } } } // The result is in dp[k][n] return dp[k][n]; } }
class Solution { public long maxArrayValue(int[] nums) { int n = nums.length; int largest = 0; if(n == 1){ return nums[0]; } for(int i = 0; i < n - 1; i++){ if(nums[i] <= nums[i + 1]){ largest = nums[i] + nums[i + 1]; nums[i + 1] = largest; nums[i] = 0; } } //printArray(nums); // 0, 0, 12, 9 for(int i = 0; i < n - 1; i++){ if(nums[i] < largest) { largest = largest + nums[i]; } } return largest; } }
thanks bro ❤❤❤❤❤❤❤❤❤❤
Welcome 🤓
So well
This lecture is very nice sir
Thanks for liking
Can u take up the super egg drop problem from leetcode please
Could you please share me link and I will go thru exact question ? Thank you :)
czcams.com/video/3CdeZNpiS6Q/video.html
why is it required to sort on each row? as it is nowhere mentioned in the question to make the score minimum
We have to take max value from completing each operations result. We are storing in list and then will get top element to find the score. Thanks
Live code:- class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> answer = new ArrayList<>(); int rows = matrix.length; // M int cols = matrix[0].length; // N //4 directions for move int up = 0, left = 0, right = cols - 1, down = rows - 1; while(answer.size() < rows*cols){ for(int col = left; col <= right; col++){ // Move from left to right answer.add(matrix[up][col]); } for(int row = up + 1; row <= down; row++){ // Move downwards answer.add(matrix[row][right]); } if(up != down){//we should always be at different row //Now move from right to left for(int col = right - 1; col >= left; col--){ answer.add(matrix[down][col]); } } if(left != right){//we should always on different columns //now move upwards for(int row = down - 1; row > up; row--){ answer.add(matrix[row][left]); } } left++; right--; up++; down--; } return answer; } } //TC = O(M*N) //SC = O(1)
*Promosm* 🤘
Thank you Leon
hello, please make video in sequence, means line by line
Thank you sure will take care of this
class Solution { public int maxDivScore(int[] nums, int[] divisors) { Map<Integer, Integer> divisibilityScores = new HashMap<>(); int maxDivisor = Integer.MAX_VALUE; int maxDivisibilityScore = Integer.MIN_VALUE; for (int divisor : divisors) { int divisibilityScore = 0; for (int num : nums) { if (num % divisor == 0) { divisibilityScore++; } } divisibilityScores.put(divisor, divisibilityScore); if (divisibilityScore > maxDivisibilityScore) { maxDivisibilityScore = divisibilityScore; maxDivisor = divisor; } else if (divisibilityScore == maxDivisibilityScore) { maxDivisor = Math.min(maxDivisor, divisor); } } return maxDivisor; } }
class Solution { public int[] rowAndMaximumOnes(int[][] mat) { int m = mat.length; int n = mat[0].length; int maxRow = 0; int maxCount = 0; for (int i = 0; i < m; i++) { int count = 0; for (int j = 0; j < n; j++) { if (mat[i][j] == 1) { count++; } } if (count > maxCount) { maxCount = count; maxRow = i; } } int[] result = new int[2]; result[0] = maxRow; result[1] = maxCount; return result; } }
Thank you
Thanks
plz Explain this
Thank you for asking today I will upload and ping you to watch. Thanks for reminding me
@Priyanshu please watch czcams.com/video/oIGSvc_qfIU/video.html
variable road[2] is not city C, its distance between city A and city B, if i understand the question
Yes road[2] is distance Thanks
programming language bhi likh diya kijiye title me.. like JAVA
Yes Akshay bilkul sahi kaha I forgot
do the dry run also..
Sure, I will
nice approach
What is time complexity of this solution?
The time complexity of this solution is O(m * (r - l + 1)), where m is the number of queries and (r - l + 1) is the length of each query range. In the outer loop, we iterate m times over the queries array. In the inner loop, we iterate (r - l + 1) times over the words array for each query, where r and l are the right and left indices of the query range, respectively. Therefore, the total time complexity is O(m * (r - l + 1)). thank you
This solution give TLE.
accepted code is here czcams.com/video/JeHkfrbdCNA/video.html thank you
public int[] vowelStrings(String[] words, int[][] queries) { int n = words.length; int[] dp = new int[n + 1]; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; String word = words[i - 1]; if (isVowel(word.charAt(0)) && isVowel(word.charAt(word.length() - 1))) { dp[i]++; } } int[] ans = new int[queries.length]; for (int i = 0; i < queries.length; i++) { int li = queries[i][0]; int ri = queries[i][1]; ans[i] = dp[ri + 1] - dp[li]; } return ans; }
Please write code Here
class Solution { public int[] vowelStrings(String[] words, int[][] queries) { int n = words.length; int[] dp = new int[n + 1]; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; String word = words[i - 1]; if (isVowel(word.charAt(0)) && isVowel(word.charAt(word.length() - 1))) { dp[i]++; } } int[] ans = new int[queries.length]; for (int i = 0; i < queries.length; i++) { int li = queries[i][0]; int ri = queries[i][1]; ans[i] = dp[ri + 1] - dp[li]; } return ans; } private boolean isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } }
2 line code. if(n==1)return 1; return n-1;
Thanks
Counting Sort Approach is also Very inefficient. Better algo is complicated (few 100lines)
Yes, there can be optimized way. Please suggest if you think there can be another approach. Also like share and subscribe for more videos
Help please sir🙃 why first code was not working and second is working: first code: class Solution { public: vector<vector> sortTheStudents(vector<vector>& score, int k) { sort(score.begin(),score.end(),k { return b[k]-a[k]; }); return score; } }; second code: class Solution { public: vector<vector> sortTheStudents(vector<vector>& score, int k) { sort(score.begin(), score.end(), [k](const vector& a, const vector& b) { return a[k] > b[k]; //Checking kth index }); return score; //After Sorting } }; but in java first one is working: java code: class Solution { public int[][] sortTheStudents(int[][] score, int k) { Arrays.sort(score,(l,m)->m[k]-l[k]); return score; } }
means b[k]-a[k] is not working in cpp but a[k]>b[k] is working ,why??please sir reply
@@nikhilrajsoni4541 In C++, you can use the sort() function from the algorithm library to sort the students by their scores in the kth exam. Here's an example of how you can sort a matrix score by the scores in the kth exam: sort(score.begin(), score.end(), [](const vector<int>& a, const vector<int>& b) { return a[k] > b[k]; }); Actually, Java has inbuild class Comparator and you have to override according to need.
@@expertfunda got you ,Thank you soo much I was so confused for many years
What's a and b here?
We have to override the compare method of comparator Like @Override public int compare(int a, int b) {} thanks
c++ ??
In C++, you can use the sort() function from the algorithm library to sort the students by their scores in the kth exam. Here's an example of how you can sort a matrix score by the scores in the kth exam: sort(score.begin(), score.end(), [](const vector<int>& a, const vector<int>& b) { return a[k] > b[k]; });
Thnks 😘 for strting in js
My pleasure 😊
I would just suggest if you would also include "leetcode" word in the title as well. May be because of that it did not come to search result
Yeah, Let me update the same. Thank you Kena Dave :)