View Hackerrank_NSum.cpp
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; |
View Leetcode_18.cpp
/* 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++; |
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`. |
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. |
NewerOlder