Skip to content

Commit a89a3f9

Browse files
committed
Added tasks 392-918
1 parent 5d1baf2 commit a89a3f9

File tree

11 files changed

+1074
-0
lines changed

11 files changed

+1074
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 392\. Is Subsequence
5+
6+
Easy
7+
8+
Given two strings `s` and `t`, return `true` _if_ `s` _is a **subsequence** of_ `t`_, or_ `false` _otherwise_.
9+
10+
A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `"ace"` is a subsequence of `"abcde"` while `"aec"` is not).
11+
12+
**Example 1:**
13+
14+
**Input:** s = "abc", t = "ahbgdc"
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
**Input:** s = "axc", t = "ahbgdc"
21+
22+
**Output:** false
23+
24+
**Constraints:**
25+
26+
* `0 <= s.length <= 100`
27+
* <code>0 <= t.length <= 10<sup>4</sup></code>
28+
* `s` and `t` consist only of lowercase English letters.
29+
30+
**Follow up:** Suppose there are lots of incoming `s`, say <code>s<sub>1</sub>, s<sub>2</sub>, ..., s<sub>k</sub></code> where <code>k >= 10<sup>9</sup></code>, and you want to check one by one to see if `t` has its subsequence. In this scenario, how would you change your code?
31+
32+
## Solution
33+
34+
```csharp
35+
public class Solution {
36+
public bool IsSubsequence(string s, string t) {
37+
int i = 0;
38+
int j = 0;
39+
int n = t.Length;
40+
int m = s.Length;
41+
if (m == 0) {
42+
return true;
43+
}
44+
while (j < n) {
45+
if (s[i] == t[j]) {
46+
i++;
47+
if (i == m) {
48+
return true;
49+
}
50+
}
51+
j++;
52+
}
53+
return false;
54+
}
55+
}
56+
```
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 399\. Evaluate Division
5+
6+
Medium
7+
8+
You are given an array of variable pairs `equations` and an array of real numbers `values`, where <code>equations[i] = [A<sub>i</sub>, B<sub>i</sub>]</code> and `values[i]` represent the equation <code>A<sub>i</sub> / B<sub>i</sub> = values[i]</code>. Each <code>A<sub>i</sub></code> or <code>B<sub>i</sub></code> is a string that represents a single variable.
9+
10+
You are also given some `queries`, where <code>queries[j] = [C<sub>j</sub>, D<sub>j</sub>]</code> represents the <code>j<sup>th</sup></code> query where you must find the answer for <code>C<sub>j</sub> / D<sub>j</sub> = ?</code>.
11+
12+
Return _the answers to all queries_. If a single answer cannot be determined, return `-1.0`.
13+
14+
**Note:** The input is always valid. You may assume that evaluating the queries will not result in division by zero and that there is no contradiction.
15+
16+
**Example 1:**
17+
18+
**Input:** equations = \[\["a","b"],["b","c"]], values = [2.0,3.0], queries = \[\["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
19+
20+
**Output:** [6.00000,0.50000,-1.00000,1.00000,-1.00000]
21+
22+
**Explanation:**
23+
24+
Given: _a / b = 2.0_, _b / c = 3.0_
25+
26+
queries are: _a / c = ?_, _b / a = ?_, _a / e = ?_, _a / a = ?_, _x / x = ?_
27+
28+
return: [6.0, 0.5, -1.0, 1.0, -1.0 ]
29+
30+
**Example 2:**
31+
32+
**Input:** equations = \[\["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = \[\["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
33+
34+
**Output:** [3.75000,0.40000,5.00000,0.20000]
35+
36+
**Example 3:**
37+
38+
**Input:** equations = \[\["a","b"]], values = [0.5], queries = \[\["a","b"],["b","a"],["a","c"],["x","y"]]
39+
40+
**Output:** [0.50000,2.00000,-1.00000,-1.00000]
41+
42+
**Constraints:**
43+
44+
* `1 <= equations.length <= 20`
45+
* `equations[i].length == 2`
46+
* <code>1 <= A<sub>i</sub>.length, B<sub>i</sub>.length <= 5</code>
47+
* `values.length == equations.length`
48+
* `0.0 < values[i] <= 20.0`
49+
* `1 <= queries.length <= 20`
50+
* `queries[i].length == 2`
51+
* <code>1 <= C<sub>j</sub>.length, D<sub>j</sub>.length <= 5</code>
52+
* <code>A<sub>i</sub>, B<sub>i</sub>, C<sub>j</sub>, D<sub>j</sub></code> consist of lower case English letters and digits.
53+
54+
## Solution
55+
56+
```csharp
57+
using System;
58+
using System.Collections.Generic;
59+
60+
public class Solution {
61+
private Dictionary<string, string> root;
62+
private Dictionary<string, double> rate;
63+
64+
public double[] CalcEquation(
65+
IList<IList<string>> equations, double[] values, IList<IList<string>> queries) {
66+
root = new Dictionary<string, string>();
67+
rate = new Dictionary<string, double>();
68+
int n = equations.Count;
69+
foreach (var equation in equations) {
70+
string x = equation[0];
71+
string y = equation[1];
72+
root[x] = x;
73+
root[y] = y;
74+
rate[x] = 1.0;
75+
rate[y] = 1.0;
76+
}
77+
for (int i = 0; i < n; ++i) {
78+
string x = equations[i][0];
79+
string y = equations[i][1];
80+
union(x, y, values[i]);
81+
}
82+
double[] result = new double[queries.Count];
83+
for (int i = 0; i < queries.Count; ++i) {
84+
string x = queries[i][0];
85+
string y = queries[i][1];
86+
if (!root.ContainsKey(x) || !root.ContainsKey(y)) {
87+
result[i] = -1;
88+
continue;
89+
}
90+
string rootX = findRoot(x, x, 1.0);
91+
string rootY = findRoot(y, y, 1.0);
92+
result[i] = rootX.Equals(rootY) ? rate[x] / rate[y] : -1.0;
93+
}
94+
return result;
95+
}
96+
97+
private void union(string x, string y, double v) {
98+
string rootX = findRoot(x, x, 1.0);
99+
string rootY = findRoot(y, y, 1.0);
100+
root[rootX] = rootY;
101+
double r1 = rate[x];
102+
double r2 = rate[y];
103+
rate[rootX] = v * r2 / r1;
104+
}
105+
106+
private string findRoot(string originalX, string x, double r) {
107+
if (root[x].Equals(x)) {
108+
root[originalX] = x;
109+
rate[originalX] = r * rate[x];
110+
return x;
111+
}
112+
return findRoot(originalX, root[x], r * rate[x]);
113+
}
114+
}
115+
```
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 427\. Construct Quad Tree
5+
6+
Medium
7+
8+
Given a `n * n` matrix `grid` of `0's` and `1's` only. We want to represent the `grid` with a Quad-Tree.
9+
10+
Return _the root of the Quad-Tree_ representing the `grid`.
11+
12+
Notice that you can assign the value of a node to **True** or **False** when `isLeaf` is **False**, and both are **accepted** in the answer.
13+
14+
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
15+
16+
* `val`: True if the node represents a grid of 1's or False if the node represents a grid of 0's.
17+
* `isLeaf`: True if the node is leaf node on the tree or False if the node has the four children.
18+
```
19+
class Node {
20+
public boolean val;
21+
public boolean isLeaf;
22+
public Node topLeft;
23+
public Node topRight;
24+
public Node bottomLeft;
25+
public Node bottomRight;
26+
}
27+
```
28+
We can construct a Quad-Tree from a two-dimensional area using the following steps:
29+
30+
1. If the current grid has the same value (i.e all `1's` or all `0's`) set `isLeaf` True and set `val` to the value of the grid and set the four children to Null and stop.
31+
2. If the current grid has different values, set `isLeaf` to False and set `val` to any value and divide the current grid into four sub-grids as shown in the photo.
32+
3. Recurse for each of the children with the proper sub-grid.
33+
34+
![](https://assets.leetcode.com/uploads/2020/02/11/new_top.png)
35+
36+
If you want to know more about the Quad-Tree, you can refer to the [wiki](https://en.wikipedia.org/wiki/Quadtree).
37+
38+
**Quad-Tree format:**
39+
40+
The output represents the serialized format of a Quad-Tree using level order traversal, where `null` signifies a path terminator where no node exists below.
41+
42+
It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list `[isLeaf, val]`.
43+
44+
If the value of `isLeaf` or `val` is True we represent it as **1** in the list `[isLeaf, val]` and if the value of `isLeaf` or `val` is False we represent it as **0**.
45+
46+
**Example 1:**
47+
48+
![](https://assets.leetcode.com/uploads/2020/02/11/grid1.png)
49+
50+
**Input:** grid = \[\[0,1],[1,0]]
51+
52+
**Output:** [[0,1],[1,0],[1,1],[1,1],[1,0]]
53+
54+
**Explanation:**
55+
56+
The explanation of this example is shown below:
57+
Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree.
58+
59+
![](https://assets.leetcode.com/uploads/2020/02/12/e1tree.png)
60+
61+
**Example 2:**
62+
63+
![](https://assets.leetcode.com/uploads/2020/02/12/e2mat.png)
64+
65+
**Input:** grid = \[\[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
66+
67+
**Output:** [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
68+
69+
**Explanation:**
70+
71+
All values in the grid are not the same. We divide the grid into four sub-grids.
72+
The topLeft, bottomLeft and bottomRight each has the same value.
73+
The topRight have different values so we divide it into 4 sub-grids where each has the same value.
74+
Explanation is shown in the photo below:
75+
76+
![](https://assets.leetcode.com/uploads/2020/02/12/e2tree.png)
77+
78+
**Constraints:**
79+
80+
* `n == grid.length == grid[i].length`
81+
* <code>n == 2<sup>x</sup></code> where `0 <= x <= 6`
82+
83+
## Solution
84+
85+
```csharp
86+
using System.Text;
87+
88+
public class Node {
89+
public bool val;
90+
public bool isLeaf;
91+
public Node? topLeft;
92+
public Node? topRight;
93+
public Node? bottomLeft;
94+
public Node? bottomRight;
95+
96+
public Node(bool val, bool isLeaf) {
97+
this.val = val;
98+
this.isLeaf = isLeaf;
99+
this.topLeft = null;
100+
this.topRight = null;
101+
this.bottomLeft = null;
102+
this.bottomRight = null;
103+
}
104+
105+
public Node(
106+
bool val,
107+
bool isLeaf,
108+
Node? topLeft,
109+
Node? topRight,
110+
Node? bottomLeft,
111+
Node? bottomRight) {
112+
this.val = val;
113+
this.isLeaf = isLeaf;
114+
this.topLeft = topLeft;
115+
this.topRight = topRight;
116+
this.bottomLeft = bottomLeft;
117+
this.bottomRight = bottomRight;
118+
}
119+
120+
public override string ToString() {
121+
StringBuilder sb = new StringBuilder();
122+
sb.Append(GetNodeString(this));
123+
sb.Append(GetNodeString(topLeft));
124+
sb.Append(GetNodeString(topRight));
125+
sb.Append(GetNodeString(bottomLeft));
126+
sb.Append(GetNodeString(bottomRight));
127+
return sb.ToString();
128+
}
129+
130+
private string GetNodeString(Node? node) {
131+
if (node == null) {
132+
return "[]";
133+
}
134+
return $"[{(node.isLeaf ? "1" : "0")},{(node.val ? "1" : "0")}]";
135+
}
136+
}
137+
138+
/*
139+
// Definition for a QuadTree node.
140+
public class Node {
141+
public bool val;
142+
public bool isLeaf;
143+
public Node topLeft;
144+
public Node topRight;
145+
public Node bottomLeft;
146+
public Node bottomRight;
147+
148+
public Node() {
149+
val = false;
150+
isLeaf = false;
151+
topLeft = null;
152+
topRight = null;
153+
bottomLeft = null;
154+
bottomRight = null;
155+
}
156+
157+
public Node(bool _val, bool _isLeaf) {
158+
val = _val;
159+
isLeaf = _isLeaf;
160+
topLeft = null;
161+
topRight = null;
162+
bottomLeft = null;
163+
bottomRight = null;
164+
}
165+
166+
public Node(bool _val,bool _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
167+
val = _val;
168+
isLeaf = _isLeaf;
169+
topLeft = _topLeft;
170+
topRight = _topRight;
171+
bottomLeft = _bottomLeft;
172+
bottomRight = _bottomRight;
173+
}
174+
}
175+
*/
176+
public class Solution {
177+
public Node Construct(int[][] grid) {
178+
return OptimizedDfs(grid, 0, 0, grid.Length);
179+
}
180+
181+
private Node OptimizedDfs(int[][] grid, int rowStart, int colStart, int len) {
182+
int zeroCount = 0;
183+
int oneCount = 0;
184+
for (int row = rowStart; row < rowStart + len; row++) {
185+
bool isBreak = false;
186+
for (int col = colStart; col < colStart + len; col++) {
187+
if (grid[row][col] == 0) {
188+
zeroCount++;
189+
} else {
190+
oneCount++;
191+
}
192+
if (oneCount > 0 && zeroCount > 0) {
193+
// We really no need to scan all.
194+
// Once we know there are both 0 and 1 then we can break.
195+
isBreak = true;
196+
break;
197+
}
198+
}
199+
if (isBreak) {
200+
break;
201+
}
202+
}
203+
if (oneCount > 0 && zeroCount > 0) {
204+
int midLen = len / 2;
205+
Node topLeft = OptimizedDfs(grid, rowStart, colStart, midLen);
206+
Node topRight = OptimizedDfs(grid, rowStart, colStart + midLen, midLen);
207+
Node bottomLeft = OptimizedDfs(grid, rowStart + midLen, colStart, midLen);
208+
Node bottomRight = OptimizedDfs(grid, rowStart + midLen, colStart + midLen, midLen);
209+
bool isLeaf = false;
210+
return new Node(true, isLeaf, topLeft, topRight, bottomLeft, bottomRight);
211+
} else {
212+
bool resultVal = oneCount > 0;
213+
bool isLeaf = true;
214+
return new Node(resultVal, isLeaf);
215+
}
216+
}
217+
}
218+
```

0 commit comments

Comments
 (0)