Skip to content

Commit b0dfd05

Browse files
Surrounded Regions
1 parent bf75ec7 commit b0dfd05

File tree

5 files changed

+287
-15
lines changed

5 files changed

+287
-15
lines changed

14.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
3+
Cheapest Flights Within K Stops
4+
-------------------------------
5+
6+
There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w.
7+
8+
Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1.
9+
10+
Example 1:
11+
Input:
12+
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
13+
src = 0, dst = 2, k = 1
14+
Output: 200
15+
Explanation:
16+
The graph looks like this:
17+
18+
19+
The cheapest price from city 0 to city 2 with at most 1 stop costs 200, as marked red in the picture.
20+
Example 2:
21+
Input:
22+
n = 3, edges = [[0,1,100],[1,2,100],[0,2,500]]
23+
src = 0, dst = 2, k = 0
24+
Output: 500
25+
Explanation:
26+
The graph looks like this:
27+
28+
29+
The cheapest price from city 0 to city 2 with at most 0 stop costs 500, as marked blue in the picture.
30+
31+
32+
Constraints:
33+
34+
The number of nodes n will be in range [1, 100], with nodes labeled from 0 to n - 1.
35+
The size of flights will be in range [0, n * (n - 1) / 2].
36+
The format of each flight will be (src, dst, price).
37+
The price of each flight will be in the range [1, 10000].
38+
k is in the range of [0, n - 1].
39+
There will not be any duplicated flights or self cycles.
40+
41+
*/
42+
43+
class Solution {
44+
public:
45+
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int K) {
46+
int min_num = 100000000;
47+
vector<int> cost(n, min_num);
48+
cost[src] = 0;
49+
for(int i=0; i<=K; i++){
50+
vector<int> current_cost(cost);
51+
for(int j=0; j<flights.size(); j++) {
52+
current_cost[flights[j][1]] = min(current_cost[flights[j][1]], cost[flights[j][0]] + flights[j][2]);
53+
}
54+
cost = current_cost;
55+
}
56+
return cost[dst] == min_num ? -1 : cost[dst];
57+
}
58+
};

15.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
3+
Search in a Binary Search Tree
4+
------------------------------
5+
6+
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.
7+
8+
For example,
9+
10+
Given the tree:
11+
4
12+
/ \
13+
2 7
14+
/ \
15+
1 3
16+
17+
And the value to search: 2
18+
You should return this subtree:
19+
20+
2
21+
/ \
22+
1 3
23+
In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.
24+
25+
Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.
26+
27+
*/
28+
29+
class Solution {
30+
public:
31+
TreeNode* searchBST(TreeNode* root, int val) {
32+
if(root == NULL || root->val == val) return root;
33+
else if(root->val > val) return searchBST(root->left, val);
34+
else return searchBST(root->right, val);
35+
36+
}
37+
};

16.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
3+
Validate IP Address
4+
-------------------
5+
6+
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.
7+
8+
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
9+
10+
Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.
11+
12+
IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).
13+
14+
However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.
15+
16+
Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.
17+
18+
Note: You may assume there is no extra space or special characters in the input string.
19+
20+
Example 1:
21+
Input: "172.16.254.1"
22+
23+
Output: "IPv4"
24+
25+
Explanation: This is a valid IPv4 address, return "IPv4".
26+
Example 2:
27+
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334"
28+
29+
Output: "IPv6"
30+
31+
Explanation: This is a valid IPv6 address, return "IPv6".
32+
Example 3:
33+
Input: "256.256.256.256"
34+
35+
Output: "Neither"
36+
37+
Explanation: This is neither a IPv4 address nor a IPv6 address.
38+
39+
40+
*/
41+
42+
43+
class Solution {
44+
public:
45+
string validIPAddress(string IP) {
46+
stringstream ss(IP);
47+
string block;
48+
if (IP.substr(0, 4).find('.') != string::npos) {
49+
for (int i = 0; i < 4; ++i) {
50+
if (!getline(ss, block, '.') || !isValidIPv4Block(block)) {
51+
return "Neither";
52+
}
53+
}
54+
if (ss.eof()) {
55+
return "IPv4";
56+
}
57+
} else if (IP.substr(0, 5).find(':') != string::npos) {
58+
for (int i = 0; i < 8; ++i) {
59+
if (!getline(ss, block, ':') || !isValidIPv6Block(block)) {
60+
return "Neither";
61+
}
62+
}
63+
if (ss.eof()) {
64+
return "IPv6";
65+
}
66+
}
67+
68+
return "Neither";
69+
}
70+
71+
private:
72+
bool isValidIPv4Block(const string& block) {
73+
int num = 0;
74+
if (block.size() > 0 && block.size() <= 3) {
75+
for (int i = 0; i < block.size(); ++i) {
76+
char c = block[i];
77+
if (!isalnum(c) || (i == 0 && c == '0' && block.size() > 1)) {
78+
return false;
79+
} else {
80+
num *= 10;
81+
num += c - '0';
82+
}
83+
}
84+
return num <= 255;
85+
}
86+
return false;
87+
}
88+
89+
bool isValidIPv6Block(const string& block) {
90+
if (block.size() > 0 && block.size() <= 4) {
91+
for (int i = 0; i < block.size(); ++i) {
92+
char c = block[i];
93+
if (!isxdigit(c)) {
94+
return false;
95+
}
96+
}
97+
return true;
98+
}
99+
return false;
100+
}
101+
};

17.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
3+
Surrounded Regions
4+
------------------
5+
6+
Solution
7+
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
8+
9+
A region is captured by flipping all 'O's into 'X's in that surrounded region.
10+
11+
Example:
12+
13+
X X X X
14+
X O O X
15+
X X O X
16+
X O X X
17+
After running your function, the board should be:
18+
19+
X X X X
20+
X X X X
21+
X X X X
22+
X O X X
23+
Explanation:
24+
25+
Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
26+
27+
*/
28+
29+
class Solution {
30+
public:
31+
void floodFill(vector<vector<char> >& board, int x, int y, char prevV, char newV)
32+
{
33+
if (x < 0 || x >= board.size() || y < 0 || y >= board[0].size()) return;
34+
if (board[x][y] != prevV) return;
35+
board[x][y] = newV;
36+
37+
floodFill(board, x+1, y, prevV, newV);
38+
floodFill(board, x-1, y, prevV, newV);
39+
floodFill(board, x, y+1, prevV, newV);
40+
floodFill(board, x, y-1, prevV, newV);
41+
}
42+
43+
void solve(vector<vector<char>>& board) {
44+
int m = board.size();
45+
if(m == 0) return;
46+
int n = board[0].size();
47+
for (int i=0; i<m; i++) {
48+
for (int j=0; j<n; j++) {
49+
if (board[i][j] == 'O') board[i][j] = '-';
50+
}
51+
}
52+
for (int i=0; i<m; i++) {
53+
if (board[i][0] == '-') floodFill(board, i, 0, '-', 'O');
54+
}
55+
for (int i=0; i<m; i++) {
56+
if (board[i][n-1] == '-') floodFill(board, i, n-1, '-', 'O');
57+
}
58+
for (int i=0; i<n; i++) {
59+
if (board[0][i] == '-') floodFill(board, 0, i, '-', 'O');
60+
}
61+
for (int i=0; i<n; i++) {
62+
if (board[m-1][i] == '-') floodFill(board, m-1, i, '-', 'O');
63+
}
64+
65+
for (int i=0; i<m; i++) {
66+
for (int j=0; j<n; j++) {
67+
if (board[i][j] == '-') board[i][j] = 'X';
68+
}
69+
}
70+
71+
}
72+
};

README.md

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
# LeetCode June Challenge
22

3-
| S No | Problem | URL |
4-
|:----:|:------------------------------:|:------------------------------------------------------------------------------------:|
5-
| 1 | Invert Binary Tree | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/1.cpp) |
6-
| 2 | Delete Node in a Linked List | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/2.cpp) |
7-
| 3 | Two City Scheduling | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/3.cpp) |
8-
| 4 | Reverse String | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/4.cpp) |
9-
| 5 | Random Pick with Weight | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/5.cpp) |
10-
| 6 | Queue Reconstruction by Height | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/6.cpp) |
11-
| 7 | Coin Change 2 | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/7.cpp) |
12-
| 8 | Power of Two | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/8.cpp) |
13-
| 9 | Is Subsequence | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/9.cpp) |
14-
| 10 | Search Insert Position | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/10.cpp) |
15-
| 11 | Sort Colors | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/11.cpp) |
16-
| 12 | Insert Delete GetRandom O(1) | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/12.cpp) |
17-
| 13 | Largest Divisible Subset | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/13.cpp) |
3+
| S No | Problem | Solution |
4+
|:----:|:-------------------------------:|:------------------------------------------------------------------------------------:|
5+
| 1 | Invert Binary Tree | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/1.cpp) |
6+
| 2 | Delete Node in a Linked List | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/2.cpp) |
7+
| 3 | Two City Scheduling | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/3.cpp) |
8+
| 4 | Reverse String | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/4.cpp) |
9+
| 5 | Random Pick with Weight | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/5.cpp) |
10+
| 6 | Queue Reconstruction by Height | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/6.cpp) |
11+
| 7 | Coin Change 2 | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/7.cpp) |
12+
| 8 | Power of Two | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/8.cpp) |
13+
| 9 | Is Subsequence | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/9.cpp) |
14+
| 10 | Search Insert Position | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/10.cpp) |
15+
| 11 | Sort Colors | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/11.cpp) |
16+
| 12 | Insert Delete GetRandom O(1) | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/12.cpp) |
17+
| 13 | Largest Divisible Subset | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/13.cpp) |
18+
| 14 | Cheapest Flights Within K Stops | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/14.cpp) |
19+
| 15 | Search in a Binary Search Tree | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/15.cpp) |
20+
| 16 | Validate IP Address | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/16.cpp) |
21+
| 17 | Surrounded Regions | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/17.cpp) |

0 commit comments

Comments
 (0)