Skip to content
Open
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
66 changes: 54 additions & 12 deletions JP Morgan/problem9.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@

## Problem Description


Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.



Example 1:

Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1

Example 2:

Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3


Constraints:

Expand All @@ -37,3 +33,49 @@ Constraints:
- 1 <= m, n <= 300
- grid[i][j] is '0' or '1'.

/// C++ Solution

class Solution {
public:

void bfs(int row,int col,vector<vector<int>> &vis,vector<vector<char>>&grid){
vis[row][col]=1;
queue<pair<int,int>> q;
q.push({row,col});
int n = grid.size();
int m = grid[0].size();
vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

while(!q.empty()){
int row = q.front().first;
int col = q.front().second;
q.pop();
for (auto [dr,dc] : directions){
int nrow = row + dr ;
int ncol = col + dc ;
if(nrow >=0 && nrow < n && ncol >= 0 && ncol <m &&
grid[nrow][ncol]=='1' && !vis[nrow][ncol]){
vis[nrow][ncol] = 1 ;
q.push({nrow,ncol}) ;
}
}
}
}

int numIslands(vector<vector<char>>& grid) {
int n = grid.size();
int m = grid[0].size();
vector<vector<int>>vis(n,vector<int>(m,0));
int count = 0 ;
for(int row = 0 ; row<n;row++){
for(int col = 0 ;col<m;col++){
if(!vis[row][col] && grid[row][col]=='1'){
count++;
bfs(row,col,vis,grid);
}
}
}
return count ;
}

};
53 changes: 53 additions & 0 deletions Microsoft/problem13.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,56 @@ Constraints:
- 1 <= s.length <= 1000
- s consist of only digits and English letters.



/// C++ Solution

class Solution {
public:
string longestPalindrome(string s) {

if(s.length() <= 1) return s;
int max_len = 1;
int n = s.length();
int st = 0, end = 0;

// Odd length
for(int i = 0; i < n-1; ++i){
int l = i, r = i;
while(l >= 0 && r < n){
if(s[l] == s[r]){
l--; r++;
}else
break;
}
int len = r-l-1;
if(len > max_len){
max_len = len;
st = l+1;
end = r-1;
}
}

// Even length
for(int i = 0; i < n-1; ++i){
int l = i, r = i+1;
while(l >= 0 && r < n){
if(s[l] == s[r]){
l--; r++;
}else
break;
}
int len = r-l-1;
if(len > max_len){
max_len = len;
st = l+1;
end = r-1;
}
}

return s.substr(st, max_len);

}
};