For all the problem solvers and maybe in coming days for technology enthusiasts, this channel serves as a hub for finding solutions to tech problems and expanding general knowledge about technology. What began as a hobby has evolved into a dream of making a significant impact, with the goal of becoming a valuable resource for technology enthusiasts everywhere. Our mission is to provide helpful, insightful content that empowers and inspires the tech community.
public String convert(String s, int numRows) { if (numRows == 1) return s; // Edge case for a single row StringBuilder[] rows = new StringBuilder[numRows]; for (int i = 0; i < numRows; i++) { rows[i] = new StringBuilder(); } int i = 0; while (i < s.length()) { for (int index = 0; index < numRows && i < s.length(); index++) { rows[index].append(s.charAt(i++)); } for (int index = numRows - 2; index > 0 && i < s.length(); index--) { rows[index].append(s.charAt(i++)); } } StringBuilder sb = new StringBuilder(); for (StringBuilder str : rows) { sb.append(str.toString()); } return sb.toString(); } We can use StringBuilder instead of String for memory optimization
Hello didi, i was trying this question. My code is slightly different from you. class Solution { List<List<Integer>> ans = new ArrayList<>(); List<Integer> res = new ArrayList<>(); public List<List<Integer>> combinationSum3(int k, int n) { // helper(1, k, n); helper2(1, k, n, 0); return ans; } public void helper2(int i, int size, int target, int sum) { if (i > 9) { return; } if (sum == target && res.size() == size) { ans.add(new ArrayList<>(res)); return; } sum += i; res.add(i); helper2(i + 1, size, target, sum); sum -= i; res.remove(res.size() - 1); helper2(i + 1, size, target, sum); } } Can you please tell my why it is failing for the case (9, 45)
Thank you mam, this video was very helpful in my case.. i had come up with nearly same intuition but failed on test case : *( . I never think before that stack can also be useful this way. This is really genuine and creative way to solve... Thank you very much mam ❤
#include <vector> #include <algorithm> #include <climits> using namespace std; class Solution { public: int solve(vector<vector<int>>& books, int shelfWidth, int i, int currWidth, int currHeight, vector<vector<int>>& dp) { if (i >= books.size()) { return currHeight; } if (dp[i][currWidth] != -1) { return dp[i][currWidth]; } int minHeight = INT_MAX; // Option 1: Place the current book on the same shelf if it fits if (currWidth + books[i][0] <= shelfWidth) { minHeight = solve(books, shelfWidth, i + 1, currWidth + books[i][0], max(currHeight, books[i][1]), dp); } // Option 2: Place the current book on a new shelf minHeight = min(minHeight, currHeight + solve(books, shelfWidth, i + 1, books[i][0], books[i][1], dp)); return dp[i][currWidth] = minHeight; } public: int minHeightShelves(vector<vector<int>>& books, int shelfWidth) { int n = books.size(); vector<vector<int>> dp(n + 1, vector<int>(shelfWidth + 1, -1)); return solve(books, shelfWidth, 0, 0, 0, dp); } }; shame shame but diffelent
Some tips as a audience perspective - 1. Try to stick to one thumbnail for a category videos for example - daily problem solving series must have a same thumbnail design that will help your previous audience to catch that thumbnail quickly and it will look well maintained. 2. Enhance your thumbnail quality using any image upscaler. All the best
Nice explanation! ..But we don't need else condition where u r putting b as 0 because we are already checking b > 0 in if so it does not satisfy that then b will 0 only , no need to set
Your videos are really helpful. First it helps understand the concepts plus it reminds me to get the daily streak, so thank you very much pls keep on making the daily content.
Thanks for the video Aditi, because of your video I realised why it's a dp problem and more importantly how we can optimize it. 1-D dp solution [C++] [Memoization] class Solution{ private: bool solve(int ind,string &s,int str_len,unordered_map<string,int> &mp,vector<int> &dp){ if(ind==str_len) return true; if(dp[ind]!=-1) return dp[ind]; for(int i=ind;i<str_len;i++){ if(mp.find(s.substr(ind,i-ind+1))!=mp.end()){ if(solve(i+1,s,str_len,mp,dp)) return dp[ind]=true; } } return dp[ind]=false; } public: bool wordBreak(string s, vector<string>& wordDict) { int size=wordDict.size(); unordered_map<string,int> mp; for(int i=0;i<size;i++) mp[wordDict[i]]=1; int str_len=s.size(); vector<int> dp(str_len+1,-1); return solve(0,s,str_len,mp,dp); } }; PS: You can convert it to tabulation to eliminate aux stack space