{{ message }}

Instantly share code, notes, and snippets.

# Bhavesh Munot bhaveshmunot1

Created Oct 10, 2020
Hackerrank N-Sum
View Hackerrank_NSum.cpp
 vector> ans; // Stores the final answer. /* Skip the duplicates. */ int increment(vector& nums, int cur) { int n = nums.size(); int j=cur+1; while (j < n && nums[j] == nums[cur]) { j++; } return j;
Last active Sep 24, 2020
Leetcode 18. 4Sum
View Leetcode_18.cpp
 /* Watch video explanation on Youtube: */ class Solution { vector> ans; // Stores the final answer. /* Skip the duplicates. */ int increment(vector& nums, int cur) { int n = nums.size(); int j=cur+1; while (j < n && nums[j] == nums[cur]) { j++;
Last active Sep 24, 2020
Leetcode 15. 3Sum (https://www.InterviewRecipes.com/3sum) (https://youtu.be/MmhfOzYMYPk)
View Leetcode_15.cpp
 /* Watch video explanation here: https://youtu.be/MmhfOzYMYPk */ class Solution { vector> ans; // Stores the final answer. /* Skip the duplicates. */ int increment(vector& nums, int cur) { int n = nums.size(); int j=cur+1; while (j < n && nums[j] == nums[cur]) { j++;
Last active Sep 13, 2020
Leetcode 167. Two Sum II - Input array is sorted (https://interviewrecipes.com/two-sum-ii) (https://youtu.be/i2y6LTIz_WU)
View Leetcode_167.cpp
 // Video Explanation: https://youtu.be/i2y6LTIz_WU class Solution { public: vector twoSum(vector& numbers, int target) { int n = numbers.size(); int left = 0; // Smallest number. int right = n-1; // Largest number. while (numbers[left] + numbers[right] != target) { if (numbers[left] + numbers[right] < target) { // Current sum is not sufficient // to reach the target.
Created Aug 21, 2020
Leetcode 70. Climbing Stairs (https://www.InterviewRecipes.com/leetcode-70)
View Leetcode_70.cpp
 class Solution { public: int climbStairs(int n) { vector dp(n+1) = {0, 1, 2}; // Stores number of ways in which // i-th step can be reached, at // location dp[i]. for (int i=3; i<=n; i++) { // For each step - (bottom to top) dp[i] = dp[i-1] + dp[i-2]; // All the ways in which (i-1)-th // step can be reached AND all the
Last active Aug 16, 2020
Leetcode 1. Two Sum (https://www.InterviewRecipes.com/leetcode-1)
View Leetcode_1.cpp
 class Solution { public: vector twoSum(vector& nums, int target) { unordered_map m; // To store the integer and its index. int n = nums.size(); // Total integers. for (int i=0; i
Created Jul 31, 2020
Leetcode #1529: Bulb Switcher IV (https://www.InterviewRecipes.com/leetcode-1529)
View Leetcode_1529.cpp
 class Solution { inline char toggle(char c) { return c == '0' ? '1' : '0'; } public: int minFlips(string target) { int n = target.size(); // Total bulbs. int flips = 0; // Final answer. char status = '0'; // This stores the status of bulbs that // are ahead of current index `i`.
Created Jul 5, 2020
Leetcode #1503: Last Moment Before All Ants Fall Out of a Plank (https://www.InterviewRecipes.com/leetcode-1503)
View Leetcode_1503.cpp
 class Solution { public: int getLastMoment(int n, vector& left, vector& right) { // The one that is farthest from the left end, but desires to go in the left // direction, will be the last one to go off of the plank from the left side. int maxLeft = left.empty() ? 0 : *max_element(left.begin(), left.end()); // Similarly, // The one that is farthest from the right end, but desires to go in the right // direction, will be the last one to go off of the plank from the right side.
Created Jul 4, 2020
Leetcode #1487: Making File Names Unique (https://www.InterviewRecipes.com/leetcode-1487)
View Leetcode_1487.cpp
 class Solution { public: unordered_map nameSuffix; // Folder name -> Next available value // for the suffix. string addSuffix(string name) { string newName = ""; do { newName = name + "(" + to_string(nameSuffix[name]) + ")"; // Generate a name. nameSuffix[name]++; // Increase the count by one
Last active Jun 26, 2020
Leetcode #1488: Avoid Flood in The City (https://www.InterviewRecipes.com/leetcode-1488)
View Leetcode_1488.cpp
 class Solution { public: vector avoidFlood(vector& rains) { vector ans; // Store the final answer here. int n = rains.size(); unordered_map fulllakes; // Lake number -> day on which it became full. set drydays; // Set of available days that can be used for drying a full lake. for (int i=0; i