Skip to content

[PDKhan] WEEK 03 solutions #1274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions combination-sum/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
void backtrack(vector<int>& candidates, int target, int index, vector<int>& curr, vector<vector<int>>& result){
if(target < 0)
return;

if(target == 0){
result.push_back(curr);
return;
}

for(int i = index; i < candidates.size(); i++){
curr.push_back(candidates[i]);
backtrack(candidates, target-candidates[i], i, curr, result);
curr.pop_back();
}
}

vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> result;
vector<int> curr;

backtrack(candidates, target, 0, curr, result);

return result;
}
};
28 changes: 28 additions & 0 deletions decode-ways/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
int numDecodings(string s) {
int n = s.length();

if(n == 0 || s[0] == '0')
return 0;

int next = 1;
int nextnext = 1;
int curr = 0;

for(int i = n - 1; i >= 0; i--){
if(s[i] == '0')
curr = 0;
else{
curr = next;
if(i < n - 1 && (s[i] == '1' || (s[i] == '2' && s[i+1] >= '0' && s[i+1] <= '6')))
curr += nextnext;
}

nextnext = next;
next = curr;
}

return curr;
}
};
19 changes: 19 additions & 0 deletions maximum-subarray/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int cur = nums[0];
int max = nums[0];

for(int i = 1; i < nums.size(); i++){
if(nums[i] > cur + nums[i])
cur = nums[i];
else
cur = cur + nums[i];

if(max < cur)
max = cur;
}

return max;
}
};
15 changes: 15 additions & 0 deletions number-of-1-bits/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int hammingWeight(int n) {
int cnt = 0;

while(n){
if(n & 1 == 1)
cnt++;

n >>= 1;
}

return cnt;
}
};
20 changes: 20 additions & 0 deletions valid-palindrome/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
bool isPalindrome(string s) {
int left = 0;
int right = s.length()-1;

while(left < right){
while(left < right && !isalnum(s[left])) left++;
while(left < right && !isalnum(s[right])) right--;

if(tolower(s[left]) != tolower(s[right]))
return false;

left++;
right--;
}

return true;
}
};