Skip to content

Instantly share code, notes, and snippets.

View bhaveshmunot1's full-sized avatar

Bhavesh Munot bhaveshmunot1

View GitHub Profile
@bhaveshmunot1
bhaveshmunot1 / Hackerrank_NSum.cpp
Created October 10, 2020 23:28
Hackerrank N-Sum
vector<vector<int>> ans; // Stores the final answer.
/* Skip the duplicates. */
int increment(vector<int>& nums, int cur) {
int n = nums.size();
int j=cur+1;
while (j < n && nums[j] == nums[cur]) {
j++;
}
return j;
@bhaveshmunot1
bhaveshmunot1 / Leetcode_18.cpp
Last active September 24, 2020 07:13
Leetcode 18. 4Sum
/* Watch video explanation on Youtube: */
class Solution {
vector<vector<int>> ans; // Stores the final answer.
/* Skip the duplicates. */
int increment(vector<int>& nums, int cur) {
int n = nums.size();
int j=cur+1;
while (j < n && nums[j] == nums[cur]) {
j++;
/* Watch video explanation here: https://youtu.be/MmhfOzYMYPk */
class Solution {
vector<vector<int>> ans; // Stores the final answer.
/* Skip the duplicates. */
int increment(vector<int>& nums, int cur) {
int n = nums.size();
int j=cur+1;
while (j < n && nums[j] == nums[cur]) {
j++;
@bhaveshmunot1
bhaveshmunot1 / Leetcode_167.cpp
Last active September 13, 2020 14:14
Leetcode 167. Two Sum II - Input array is sorted (https://interviewrecipes.com/two-sum-ii) (https://youtu.be/i2y6LTIz_WU)
// Video Explanation: https://youtu.be/i2y6LTIz_WU
class Solution {
public:
vector<int> twoSum(vector<int>& 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.
class Solution {
public:
int climbStairs(int n) {
vector<int> 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
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> m; // To store the integer and its index.
int n = nums.size(); // Total integers.
for (int i=0; i<n; i++) { // For each integer, say K -
if (m.find(target-nums[i]) != m.end()) { // Check if (target - K) exists.
return {i, m[target-nums[i]]}; // Yay, found the answer.
}
m[nums[i]] = i; // Save the K in the map with it's index.
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`.
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1503.cpp
Created July 5, 2020 19:34
Leetcode #1503: Last Moment Before All Ants Fall Out of a Plank (https://www.InterviewRecipes.com/leetcode-1503)
class Solution {
public:
int getLastMoment(int n, vector<int>& left, vector<int>& 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.
class Solution {
public:
unordered_map<string, int> 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
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1488.cpp
Last active June 26, 2020 03:16
Leetcode #1488: Avoid Flood in The City (https://www.InterviewRecipes.com/leetcode-1488)
class Solution {
public:
vector<int> avoidFlood(vector<int>& rains) {
vector<int> ans; // Store the final answer here.
int n = rains.size();
unordered_map<int, int> fulllakes; // Lake number -> day on which it became full.
set<int> drydays; // Set of available days that can be used for drying a full lake.
for (int i=0; i<n; i++) { // For each day -
if (rains[i] == 0) { // No rain on this day.
drydays.insert(i); // This day can be used as a day to dry some lake.