diff --git a/longest-substring-without-repeating-characters/PDKhan.cpp b/longest-substring-without-repeating-characters/PDKhan.cpp new file mode 100644 index 000000000..b57a1719c --- /dev/null +++ b/longest-substring-without-repeating-characters/PDKhan.cpp @@ -0,0 +1,20 @@ +class Solution { + public: + int lengthOfLongestSubstring(string s) { + int result = 0; + int start = 0; + unordered_map 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; + } + }; diff --git a/number-of-islands/PDKhan.cpp b/number-of-islands/PDKhan.cpp new file mode 100644 index 000000000..891f19ead --- /dev/null +++ b/number-of-islands/PDKhan.cpp @@ -0,0 +1,29 @@ +class Solution { + public: + void search(int r, int c, vector>& 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>& 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; + } + }; diff --git a/reverse-linked-list/PDKhan.cpp b/reverse-linked-list/PDKhan.cpp new file mode 100644 index 000000000..71ff745f8 --- /dev/null +++ b/reverse-linked-list/PDKhan.cpp @@ -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; + } + }; diff --git a/set-matrix-zeroes/PDKhan.cpp b/set-matrix-zeroes/PDKhan.cpp new file mode 100644 index 000000000..975b59e45 --- /dev/null +++ b/set-matrix-zeroes/PDKhan.cpp @@ -0,0 +1,46 @@ +class Solution { + public: + void setZeroes(vector>& 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; + } + } + }; diff --git a/unique-paths/PDKhan.cpp b/unique-paths/PDKhan.cpp new file mode 100644 index 000000000..aca265348 --- /dev/null +++ b/unique-paths/PDKhan.cpp @@ -0,0 +1,22 @@ +class Solution { + public: + int uniquePaths(int m, int n) { + vector> dp(m, vector(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]; + } + };