Leetcode 26:
https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int lastUniqueElementIdx = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[lastUniqueElementIdx] != nums[i]) {
nums[lastUniqueElementIdx + 1] = nums[i];
lastUniqueElementIdx++;
}
}
return lastUniqueElementIdx + 1;
}
};π5π€―2
Leetcode 122:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
class Solution {
public:
int maxProfit(vector<int>& prices) {
int maxProfit = 0;
for(int i = 0; i < prices.size() - 1; i++) {
if(prices[i] < prices[i + 1]) {
int diff = prices[i + 1] - prices[i];
maxProfit += diff;
}
}
return maxProfit;
}
};π3
Leetcode 189:
https://leetcode.com/problems/rotate-array/
https://leetcode.com/problems/rotate-array/
#include <vector>
#include <iostream>
class Solution {
public:
void rotate(std::vector<int>& nums, int k) {
k = k % nums.size();
// Reversing the whole array
reverseNumsArr(nums, 0, nums.size());
// Reversing first k elements
reverseNumsArr(nums, 0, k);
// Reversing remaining elements
reverseNumsArr(nums, k, nums.size());
}
private:
void reverseNumsArr(std::vector<int>& nums, int start, int end) {
end--; // Adjusting end to be the last index
while (start < end) {
std::swap(nums[start], nums[end]);
start++;
end--;
}
}
};
β€2
Leetcode 217:
https://leetcode.com/problems/contains-duplicate/description/
https://leetcode.com/problems/contains-duplicate/description/
#include <vector>
#include <algorithm>
class Solution {
public:
bool containsDuplicate(std::vector<int>& nums) {
std::sort(nums.begin(), nums.end());
for (size_t i = 0; i < nums.size() - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
};
π€―4β€1
Leetcode 136:
https://leetcode.com/problems/single-number/
https://leetcode.com/problems/single-number/
class Solution {
public:
int singleNumber(vector<int>& nums) {
int finder = 0;
for(int i = 0; i < nums.size(); i++) {
finder = finder ^ nums[i];
}
return finder;
}
};π5
Leetcode 350: https://leetcode.com/problems/intersection-of-two-arrays-ii/
class Solution {
public:
std::vector<int> intersect(std::vector<int>& nums1, std::vector<int>& nums2) {
std::unordered_map<int, int> numsMap;
std::vector<int> intersectedNums;
for (int num : nums1) {
numsMap[num]++;
}
for (int num : nums2) {
if (numsMap.find(num) != numsMap.end() && numsMap[num] > 0) {
intersectedNums.push_back(num);
numsMap[num]--;
if (numsMap[num] == 0) {
numsMap.erase(num);
}
}
}
return intersectedNums;
}
};π8β€4π€―1π1
Leetcode 66:
https://leetcode.com/problems/plus-one/
https://leetcode.com/problems/plus-one/
class Solution {
public:
std::vector<int> plusOne(std::vector<int>& digits) {
int currentIndex = digits.size() - 1;
while (digits[currentIndex] == 9) {
if (currentIndex == 0) {
std::vector<int> resultArr(digits.size() + 1, 0);
resultArr[0] = 1;
return resultArr;
}
digits[currentIndex] = 0;
currentIndex--;
}
digits[currentIndex]++;
return digits;
}
};π6β€2π₯°1
Leetcode 283: https://leetcode.com/problems/move-zeroes/
class Solution {
public:
void moveZeroes(vector<int>& nums) {
if (nums.size() < 2) {
return;
}
int leftIdx = 0, rightIdx = 1;
while (rightIdx < nums.size()) {
if (nums[leftIdx] != 0) {
leftIdx++;
rightIdx++;
}
else if (nums[rightIdx] == 0) {
rightIdx++;
}
else {
swap(nums[leftIdx], nums[rightIdx]);
}
}
}
};π11β€8π₯1π1
Get Certified and gain knowledge accross different domains for free.
Join πhttps://t.me/udemyyfreecourses
Join πhttps://t.me/udemyyfreecourses
Telegram
Udemy Free Courses
π Quality matters here rather than quantity.
π― Our goal is to make quality education accessible to every learner for free.
π― Our goal is to make quality education accessible to every learner for free.
β€5π₯2
Are you π«΅ struggling with understanding technical terms
Check out my recent LinkedIn Post
πhttps://www.linkedin.com/posts/sai-pradeep-875742268_tech-learning-students-share-7442189348726349824-VSBz?
Check out my recent LinkedIn Post
πhttps://www.linkedin.com/posts/sai-pradeep-875742268_tech-learning-students-share-7442189348726349824-VSBz?
LinkedIn
#tech #learning #students #developers #careergrowth #selflearning #programming #coding #software #it #btech #be #ai #ml | Sai Pradeep
π€ I used to hear technical terms everywhere - in classes, videos, interviewsβ¦ and most of the time, I didnβt really understand the meaning of them.
I would tell myself, βIβll learn it later.β
But honestly, later never came.
Thatβs when I decided to fix thisβ¦
I would tell myself, βIβll learn it later.β
But honestly, later never came.
Thatβs when I decided to fix thisβ¦
Join our other channels, really helps you a lot.
Jobs For Freshers - Off Campus
π https://t.me/jobsforfreshers_offcampus
Udemy Free Courses
πhttps://t.me/udemyyfreecourses
Tech Jargon - Decoded
πhttps://t.me/tech_jargon_decoded
Leetcode For Interviews
πhttps://t.me/leetcode_for_interviews
Jobs For Freshers - Off Campus
π https://t.me/jobsforfreshers_offcampus
Udemy Free Courses
πhttps://t.me/udemyyfreecourses
Tech Jargon - Decoded
πhttps://t.me/tech_jargon_decoded
Leetcode For Interviews
πhttps://t.me/leetcode_for_interviews