Skip to content

[PDKhan] WEEK 07 solutions #1454

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 6 commits into from
May 11, 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
20 changes: 20 additions & 0 deletions longest-substring-without-repeating-characters/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int result = 0;
int start = 0;
unordered_map<char, int> map;

for(int end = 0; end < s.length(); end++){
char ch = s[end];

if(map.count(ch) && map[ch] >= start)
start = map[ch] + 1;

map[ch] = end;
result = max(result, end - start + 1);
}

return result;
}
};
29 changes: 29 additions & 0 deletions number-of-islands/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
void search(int r, int c, vector<vector<char>>& grid){
if(r < 0 || c < 0 || r >= grid.size() || c >= grid[r].size() || grid[r][c] == '0')
return;

grid[r][c] = '0';

search(r-1, c, grid);
search(r+1, c, grid);
search(r, c-1, grid);
search(r, c+1, grid);
}

int numIslands(vector<vector<char>>& grid) {
int cnt = 0;

for(int i = 0; i < grid.size(); i++){
for(int j = 0; j < grid[i].size(); j++){
if(grid[i][j] == '1'){
search(i, j, grid);
cnt++;
}
}
}

return cnt;
}
};
16 changes: 16 additions & 0 deletions reverse-linked-list/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* new_head = NULL;

while(head){
ListNode* next = head->next;

head->next = new_head;
new_head = head;
head = next;
}

return new_head;
}
};
46 changes: 46 additions & 0 deletions set-matrix-zeroes/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();

bool firstRowZero = false;
bool firstColZero = false;

for(int j = 0; j < cols; j++){
if(matrix[0][j] == 0)
firstRowZero = true;
}

for(int i = 0; i < rows; i++){
if(matrix[i][0] == 0)
firstColZero = true;
}

for(int i = 1; i < rows; i++){
for(int j = 1; j < cols; j++){
if(matrix[i][j] == 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}

for(int i = 1; i < matrix.size(); i++){
for(int j = 1; j < matrix[0].size(); j++){
if(matrix[0][j] == 0 || matrix[i][0] == 0)
matrix[i][j] = 0;
}
}

if(firstRowZero){
for(int j = 0; j < cols; j++)
matrix[0][j] = 0;
}

if(firstColZero){
for(int i = 0; i < rows; i++)
matrix[i][0] = 0;
}
}
};
22 changes: 22 additions & 0 deletions unique-paths/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m, vector<int>(n, 0));

dp[0][0] = 1;

for(int i = 1; i < m; i++)
dp[i][0] = 1;

for(int j = 1; j < n; j++)
dp[0][j] = 1;

for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}

return dp[m - 1][n - 1];
}
};