This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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++; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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++; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
NewerOlder