Skip to content

Instantly share code, notes, and snippets.

Avatar

Bhavesh Munot bhaveshmunot1

View GitHub Profile
View Leetcode_15.cpp
/* 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++;
View Leetcode_167.cpp
// 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.
View Leetcode_70.cpp
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
View Leetcode_1.cpp
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.
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`.
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1503.cpp
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<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.
View Leetcode_1487.cpp
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
View Leetcode_1488.cpp
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.
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1465.cpp
Created Jun 24, 2020
Leetcode #1465: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts (https://www.InterviewRecipes.com/leetcode-1465)
View Leetcode_1465.cpp
class Solution {
const int mod = (1e9)+7; // As the question expects.
int getMax(int length, vector<int> &cuts) {
sort(cuts.begin(), cuts.end()); // Sort so that we will easily get the thickness
// of each piece after cutting it.
int n = cuts.size();
int maxCut = max(length-cuts[n-1], // Thickness of the last piece.
cuts[0]); // Thickness of the first piece.
for (int i=1; i<n; i++) { // For each cut -
@bhaveshmunot1
bhaveshmunot1 / Leetcode_1419.cpp
Last active Jun 23, 2020
Leetcode #1419: Minimum Number of Frogs Croaking (https://www.InterviewRecipes.com/leetcode-1419)
View Leetcode_1419.cpp
class Solution {
unordered_map<char, int> frequency; // Stores how many times each sound has occured.
// Sounds are c, r, o, a, k.
/*
* At any time, for a sequence to be valid, number of 'c' must not be less than 'r'.
* Similarly, #'r' shouldn't less than #'o', and so on.
*/
bool isStateValid() {
return (frequency['c'] >= frequency['r']) &&
(frequency['r'] >= frequency['o']) &&
You can’t perform that action at this time.