├── 3Sum.cpp ├── CountAndSay.cpp ├── FavouriteGenre.cpp ├── KokoEatingBananas.cpp ├── LICENSE ├── LongestPalindromicSubstring.cpp ├── MergeIntervals.cpp ├── ProductofArrayExceptSelf.cpp ├── README.md ├── RemoveElement.cpp ├── RottenOranges.cpp ├── SearchSuggestionSystem.cpp ├── SortedArraytoBalancedBST.cpp ├── SurroundedRegions.cpp ├── TrappingRainwater.cpp ├── TwoSum.cpp ├── UniquePathsII.cpp ├── ValidAnagram.cpp └── WordBreak.cpp /3Sum.cpp: -------------------------------------------------------------------------------- 1 | vector> threeSum(vector& nums) { 2 | int n = nums.size(); 3 | if(n < 3) return {}; 4 | vector> result; 5 | sort(nums.begin(), nums.end()); 6 | for(int i = 0; i < n-2; ++i){ 7 | if(i == 0 || nums[i] != nums[i-1]){ 8 | int j = i + 1, k = n-1; 9 | while(j < k){ 10 | int sum = nums[i] + nums[j] + nums[k]; 11 | if(sum == 0){ 12 | result.push_back({nums[i], nums[j], nums[k]}); 13 | while(j < k && nums[j] == nums[j+1]) j++; 14 | while(j < k && nums[k] == nums[k-1]) k--; 15 | j++; k--; 16 | } 17 | else if (sum > 0) k--; 18 | else j++; 19 | } 20 | } 21 | } 22 | return result; 23 | } 24 | 25 | //LeetCode Link: https://leetcode.com/problems/3sum/ 26 | //Space Complexity: O(n) 27 | //Time: O(n^2) 28 | //Intuition: Fix ith element and apply two sum logic from i+1(j) to n-1(k) and when nums[i]+nums[j]+nums[k]==0, append it to the list. 29 | //Make sure that if duplicate elements occur, skip them. 30 | -------------------------------------------------------------------------------- /CountAndSay.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string countAndSay(int n) { 4 | vectorres(n+1); 5 | res[0]="1"; 6 | res[1]="11"; 7 | 8 | for(int i=2;i<=n;i++) 9 | { 10 | string pre=res[i-1]; 11 | string temp = ""; 12 | for(int j=0;j 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | map> getUserFavouriteGenres(map> userSongs, map> songGenres){ 11 | map> userFavGenres; 12 | 13 | map songToGenre; // maps the song name to its genre 14 | for (auto it = songGenres.begin(); it != songGenres.end(); ++it){ 15 | string genre = it->first; 16 | vector songs = it->second; 17 | 18 | for (string song : songs){ 19 | songToGenre[song] = genre; 20 | } 21 | } 22 | 23 | for (auto it = userSongs.begin(); it != userSongs.end(); ++it){ 24 | string user = it->first; 25 | vector userFavSongs = it->second; 26 | unordered_map songGenresCount; 27 | int maxCount = 0; 28 | for (string song : userFavSongs){ 29 | string songGenre = songToGenre[song]; 30 | songGenresCount[songGenre]++; 31 | maxCount = max(maxCount, songGenresCount[songGenre]); 32 | } 33 | 34 | for (auto ii = songGenresCount.begin(); ii != songGenresCount.end(); ++ii){ 35 | if(ii->second == maxCount){ 36 | if(userFavGenres.find(user) == userFavGenres.end()) 37 | userFavGenres[user] = {}; 38 | userFavGenres[user].push_back(ii->first); 39 | } 40 | } 41 | } 42 | return userFavGenres; 43 | } 44 | 45 | int main(void){ 46 | map> userSongs; 47 | map> songGenres; 48 | 49 | userSongs["David"] = {"song1", "song2", "song3", "song4", "song8"}; 50 | userSongs["Emma"] = {"song5", "song6", "song7"}; 51 | 52 | songGenres["Rock"] = {"song1", "song3"}; 53 | songGenres["Dubstep"] = {"song7"}; 54 | songGenres["Techno"] = {"song2", "song4"}; 55 | songGenres["Pop"] = {"song5", "song6"}; 56 | songGenres["Jazz"] = {"song8", "song9"}; 57 | 58 | map> result = getUserFavouriteGenres(userSongs, songGenres); 59 | for (auto it = result.begin(); it != result.end(); ++it){ 60 | cout << it->first << ": "; 61 | for (string g : it->second){ 62 | cout << g << ", "; 63 | } 64 | cout << endl; 65 | } 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /KokoEatingBananas.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | bool canEatInTime(vector& piles, int K, int H) 5 | { 6 | int hours=0; 7 | for(int i=0;i& piles, int H) { 20 | int left=1, right=*max_element(piles.begin(), piles.end()); 21 | while(left<=right) 22 | { 23 | int mid=left+(right-left)/2; 24 | if(canEatInTime(piles, mid, H)) 25 | { 26 | right=mid-1; 27 | } 28 | else 29 | { 30 | left=mid+1; 31 | } 32 | } 33 | return left; 34 | 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Shreya Prasad 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LongestPalindromicSubstring.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestPalindrome(string s) { 4 | 5 | if(s.length()<1) 6 | { 7 | return ""; 8 | } 9 | 10 | int start=0, end=0; 11 | 12 | for(int i=0;i end - start) 19 | { 20 | start = i - (len-1)/2; 21 | end=i+len/2; 22 | } 23 | } 24 | 25 | return s.substr(start,end+1-start); 26 | } 27 | 28 | int expandAroundCenter(string s, int left, int right) 29 | { 30 | int L=left, R=right; 31 | while(L>=0 && R merge(vector& intervals) { 4 | //base case 5 | if(intervals.size()==0) return {}; 6 | 7 | //main code 8 | sort(intervals.begin(),intervals.end()); 9 | 10 | vector temp=intervals[0]; 11 | vector res; 12 | for(int i=1; i productExceptSelf(vector& nums) { 4 | int n=nums.size(); 5 | vectorright(n), left(n); 6 | right[0]=1; 7 | left[n-1]=1; 8 | 9 | for(int i=1;i=0;i--) 15 | { 16 | left[i]=nums[i+1]*left[i+1]; 17 | } 18 | 19 | vectorans(n); 20 | 21 | for(int i=0;i& nums, int val) { 5 | nums.erase(remove(nums.begin(), nums.end(), val), nums.end()); 6 | return nums.size(); 7 | } 8 | }; 9 | 10 | //Method 2: Iteration 11 | 12 | int removeElement(vector& nums, int val) { 13 | for(auto it=nums.begin();it!=nums.end();it++){ 14 | if(*it==val){ 15 | nums.erase(it); 16 | --it; 17 | } 18 | } 19 | return nums.size(); 20 | } 21 | 22 | 23 | //Runtime: 4ms 24 | //LeetCode: https://leetcode.com/problems/remove-element/ 25 | 26 | -------------------------------------------------------------------------------- /RottenOranges.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int orangesRotting(vector>& grid) { 4 | queue> rotten; 5 | int r = grid.size(), c = grid[0].size(), fresh = 0, t = 0; 6 | for(int i = 0; i < r; ++i){ 7 | for(int j = 0; j < c; ++j){ 8 | if(grid[i][j] == 2) rotten.push({i, j}); 9 | else if(grid[i][j] == 1) fresh++; 10 | } 11 | } 12 | 13 | while(!rotten.empty()){ 14 | int num = rotten.size(); 15 | for(int i = 0; i < num; ++i){ 16 | int x = rotten.front().first, y = rotten.front().second; 17 | rotten.pop(); 18 | if(x > 0 && grid[x-1][y] == 1){ grid[x-1][y] = 2; fresh--; rotten.push({x-1, y});}; 19 | if(y > 0 && grid[x][y-1] == 1){ grid[x][y-1] = 2; fresh--; rotten.push({x, y-1});}; 20 | if(x < r-1 && grid[x+1][y] == 1){ grid[x+1][y] = 2; fresh--; rotten.push({x+1, y});}; 21 | if(y < c-1 && grid[x][y+1] == 1){ grid[x][y+1] = 2; fresh--; rotten.push({x, y+1});}; 22 | } 23 | if(!rotten.empty()) t++; 24 | } 25 | return (fresh == 0) ? t : -1; 26 | } 27 | }; 28 | 29 | //Link: https://leetcode.com/problems/rotting-oranges/ 30 | -------------------------------------------------------------------------------- /SearchSuggestionSystem.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> suggestedProducts(vector& products, string searchWord) { 4 | sort(products.begin(),products.end()); 5 | vector>ans; 6 | vectormid_ans; 7 | string new_str=""; 8 | int c=0; 9 | for(int i=0;i&nums, int start, int end) 5 | { 6 | if(start>end) 7 | { 8 | return NULL; 9 | } 10 | 11 | int mid= (start+end)/2; 12 | 13 | TreeNode* root = new TreeNode(nums[mid]); 14 | 15 | root->left = arrToBST(nums, start, mid-1); 16 | root->right = arrToBST(nums,mid+1,end); 17 | 18 | return root; 19 | } 20 | 21 | TreeNode* sortedArrayToBST(vector& nums) { 22 | 23 | int n=nums.size()-1; 24 | TreeNode* ans = arrToBST(nums, 0, n); 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /SurroundedRegions.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | 4 | void floodFill(vector>& board,int x, int y, char prevV,char newV) 5 | { 6 | // Base cases 7 | if (x < 0 || x >= board.size() || y < 0 || y >= board[0].size() || 8 | board[x][y] != prevV) 9 | return; 10 | 11 | 12 | // Replace the color at (x, y) 13 | board[x][y] = newV; 14 | 15 | // Recur for north, east, south and west 16 | floodFill(board, x+1, y, prevV, newV); 17 | floodFill(board, x-1, y, prevV, newV); 18 | floodFill(board, x, y+1, prevV, newV); 19 | floodFill(board, x, y-1, prevV, newV); 20 | } 21 | 22 | void solve(vector>& board) { 23 | if(board.empty()) 24 | { 25 | return; 26 | } 27 | for(int i=0;i& height) { 4 | if(height.empty()) 5 | { 6 | return 0; 7 | } 8 | 9 | int count=0; 10 | int n=height.size(); 11 | vectorleft(n); 12 | vectorright(n); 13 | 14 | 15 | left[0]=height[0]; 16 | right[n-1]=height[n-1]; 17 | 18 | //max in right subarray 19 | for(int i=1;i=0;i--) 25 | { 26 | right[i]=max(right[i+1], height[i]); 27 | } 28 | 29 | for(int i=0;i twoSum(vector& nums, int target) { 4 | vectorans; int j=1; 5 | if(!nums.size()) 6 | ans.push_back(0); 7 | 8 | unordered_mapM; 9 | int x; 10 | for(int i =0; isecond); 16 | } 17 | M[nums[i]] = i; 18 | } 19 | return ans; 20 | } 21 | }; 22 | 23 | //Time Complexity: O(n) 24 | //LeetCode Link: https://leetcode.com/problems/two-sum/ 25 | -------------------------------------------------------------------------------- /UniquePathsII.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePathsWithObstacles(vector > &obstacleGrid) { 4 | int m = obstacleGrid.size() , n = obstacleGrid[0].size(); 5 | vector> dp(m+1,vector(n+1,0)); 6 | dp[0][1] = 1; 7 | for(int i = 1 ; i <= m ; ++i) 8 | for(int j = 1 ; j <= n ; ++j) 9 | if(!obstacleGrid[i-1][j-1]) 10 | dp[i][j] = dp[i-1][j]+dp[i][j-1]; 11 | return dp[m][n]; 12 | } 13 | }; 14 | 15 | //Time Complexity: O(mn) 16 | //LeetCode Link:https://leetcode.com/problems/unique-paths-ii/ 17 | -------------------------------------------------------------------------------- /ValidAnagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | if(s.length()!=t.length()) 5 | { 6 | return false; 7 | } 8 | unordered_mapm; 9 | for(int i=0;isecond!=0) 17 | { 18 | return false; 19 | } 20 | } 21 | 22 | return true; 23 | } 24 | }; 25 | 26 | //Time Complexity: O(n) 27 | //Space Complexity: O(1) because the size of the hash table remains constant. It has a fixed value which is why space has O(1). 28 | //If the size would have depended on input dynamically then space complexity would have been O(n). 29 | -------------------------------------------------------------------------------- /WordBreak.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool wordBreak(string s, vector& wordDict) { 4 | set st; 5 | for(auto v:wordDict){ 6 | st.insert(v); 7 | } 8 | vector dp(s.size()+1); 9 | for(int i=0;i