Skip to content

Commit 79617c3

Browse files
committed
Added tasks 79-221
1 parent 2076e43 commit 79617c3

File tree

31 files changed

+2356
-0
lines changed

31 files changed

+2356
-0
lines changed

README.md

Lines changed: 90 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 79\. Word Search
5+
6+
Medium
7+
8+
Given an `m x n` grid of characters `board` and a string `word`, return `true` _if_ `word` _exists in the grid_.
9+
10+
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/04/word2.jpg)
15+
16+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
17+
18+
**Output:** true
19+
20+
**Example 2:**
21+
22+
![](https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg)
23+
24+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
25+
26+
**Output:** true
27+
28+
**Example 3:**
29+
30+
![](https://assets.leetcode.com/uploads/2020/10/15/word3.jpg)
31+
32+
**Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
33+
34+
**Output:** false
35+
36+
**Constraints:**
37+
38+
* `m == board.length`
39+
* `n = board[i].length`
40+
* `1 <= m, n <= 6`
41+
* `1 <= word.length <= 15`
42+
* `board` and `word` consists of only lowercase and uppercase English letters.
43+
44+
**Follow up:** Could you use search pruning to make your solution faster with a larger `board`?
45+
46+
## Solution
47+
48+
```dart
49+
class Solution {
50+
bool backtrace(List<List<String>> board, List<List<bool>> visited,
51+
String word, int index, int x, int y) {
52+
if (index == word.length) {
53+
return true;
54+
}
55+
if (x < 0 || x >= board.length || y < 0 || y >= board[0].length ||
56+
visited[x][y]) {
57+
return false;
58+
}
59+
visited[x][y] = true;
60+
61+
if (word[index] == board[x][y]) {
62+
bool res =
63+
backtrace(board, visited, word, index + 1, x, y + 1) ||
64+
backtrace(board, visited, word, index + 1, x, y - 1) ||
65+
backtrace(board, visited, word, index + 1, x + 1, y) ||
66+
backtrace(board, visited, word, index + 1, x - 1, y);
67+
68+
if (!res) {
69+
visited[x][y] = false;
70+
}
71+
return res;
72+
} else {
73+
visited[x][y] = false;
74+
return false;
75+
}
76+
}
77+
78+
bool exist(List<List<String>> board, String word) {
79+
List<List<bool>> visited = List.generate(
80+
board.length, (_) => List.filled(board[0].length, false));
81+
82+
for (int i = 0; i < board.length; i++) {
83+
for (int j = 0; j < board[0].length; j++) {
84+
if (backtrace(board, visited, word, 0, i, j)) {
85+
return true;
86+
}
87+
}
88+
}
89+
return false;
90+
}
91+
}
92+
```
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 84\. Largest Rectangle in Histogram
5+
6+
Hard
7+
8+
Given an array of integers `heights` representing the histogram's bar height where the width of each bar is `1`, return _the area of the largest rectangle in the histogram_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg)
13+
14+
**Input:** heights = [2,1,5,6,2,3]
15+
16+
**Output:** 10
17+
18+
**Explanation:** The above is a histogram where width of each bar is 1.
19+
20+
The largest rectangle is shown in the red area, which has an area = 10 units.
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg)
25+
26+
**Input:** heights = [2,4]
27+
28+
**Output:** 4
29+
30+
**Constraints:**
31+
32+
* <code>1 <= heights.length <= 10<sup>5</sup></code>
33+
* <code>0 <= heights[i] <= 10<sup>4</sup></code>
34+
35+
## Solution
36+
37+
```dart
38+
class Solution {
39+
int largestRectangleArea(List<int> heights) {
40+
return largestArea(heights, 0, heights.length);
41+
}
42+
43+
int largestArea(List<int> a, int start, int limit) {
44+
if (a.isEmpty) {
45+
return 0;
46+
}
47+
if (start == limit) {
48+
return 0;
49+
}
50+
if (limit - start == 1) {
51+
return a[start];
52+
}
53+
if (limit - start == 2) {
54+
int maxOfTwoBars = a[start] > a[start + 1] ? a[start] : a[start + 1];
55+
int areaFromTwo = (a[start] < a[start + 1] ? a[start] : a[start + 1]) * 2;
56+
return maxOfThreeNums(maxOfTwoBars, areaFromTwo, 0);
57+
}
58+
if (checkIfSorted(a, start, limit)) {
59+
int maxWhenSorted = 0;
60+
for (int i = start; i < limit; i++) {
61+
if (a[i] * (limit - i) > maxWhenSorted) {
62+
maxWhenSorted = a[i] * (limit - i);
63+
}
64+
}
65+
return maxWhenSorted;
66+
} else {
67+
int minInd = findMinInArray(a, start, limit);
68+
return maxOfThreeNums(
69+
largestArea(a, start, minInd),
70+
a[minInd] * (limit - start),
71+
largestArea(a, minInd + 1, limit));
72+
}
73+
}
74+
75+
int findMinInArray(List<int> a, int start, int limit) {
76+
int min = a[start];
77+
int minIndex = start;
78+
for (int index = start + 1; index < limit; index++) {
79+
if (a[index] < min) {
80+
min = a[index];
81+
minIndex = index;
82+
}
83+
}
84+
return minIndex;
85+
}
86+
87+
bool checkIfSorted(List<int> a, int start, int limit) {
88+
for (int i = start + 1; i < limit; i++) {
89+
if (a[i] < a[i - 1]) {
90+
return false;
91+
}
92+
}
93+
return true;
94+
}
95+
96+
int maxOfThreeNums(int a, int b, int c) {
97+
return a > b ? (a > c ? a : c) : (b > c ? b : c);
98+
}
99+
}
100+
```
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 94\. Binary Tree Inorder Traversal
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, return _the inorder traversal of its nodes' values_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
13+
14+
**Input:** root = [1,null,2,3]
15+
16+
**Output:** [1,3,2]
17+
18+
**Example 2:**
19+
20+
**Input:** root = []
21+
22+
**Output:** []
23+
24+
**Example 3:**
25+
26+
**Input:** root = [1]
27+
28+
**Output:** [1]
29+
30+
**Example 4:**
31+
32+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg)
33+
34+
**Input:** root = [1,2]
35+
36+
**Output:** [2,1]
37+
38+
**Example 5:**
39+
40+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg)
41+
42+
**Input:** root = [1,null,2]
43+
44+
**Output:** [1,2]
45+
46+
**Constraints:**
47+
48+
* The number of nodes in the tree is in the range `[0, 100]`.
49+
* `-100 <= Node.val <= 100`
50+
51+
**Follow up:** Recursive solution is trivial, could you do it iteratively?
52+
53+
## Solution
54+
55+
```dart
56+
/**
57+
* Definition for a binary tree node.
58+
* class TreeNode {
59+
* int val;
60+
* TreeNode? left;
61+
* TreeNode? right;
62+
* TreeNode([this.val = 0, this.left, this.right]);
63+
* }
64+
*/
65+
class Stack<T> {
66+
final List<T> _items = [];
67+
68+
void push(T item) {
69+
_items.add(item);
70+
}
71+
72+
T pop() {
73+
return _items.removeLast();
74+
}
75+
76+
T peek() {
77+
return _items.last;
78+
}
79+
80+
bool get isEmpty {
81+
return _items.isEmpty;
82+
}
83+
84+
int get size {
85+
return _items.length;
86+
}
87+
}
88+
89+
class Solution {
90+
91+
List<int> inorderTraversal(TreeNode? root) {
92+
final result = List<int>.empty(growable: true);
93+
final stack = Stack<TreeNode>();
94+
95+
while (root != null || !stack.isEmpty) {
96+
while (root != null) {
97+
stack.push(root);
98+
root = root.left;
99+
}
100+
root = stack.pop();
101+
102+
result.add(root.val);
103+
104+
root = root.right;
105+
}
106+
107+
return result;
108+
}
109+
}
110+
```
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Dart/LeetCode-in-Dart?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Dart/LeetCode-in-Dart?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Dart/LeetCode-in-Dart/fork)
3+
4+
## 96\. Unique Binary Search Trees
5+
6+
Medium
7+
8+
Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg)
13+
14+
**Input:** n = 3
15+
16+
**Output:** 5
17+
18+
**Example 2:**
19+
20+
**Input:** n = 1
21+
22+
**Output:** 1
23+
24+
**Constraints:**
25+
26+
* `1 <= n <= 19`
27+
28+
## Solution
29+
30+
```dart
31+
class Solution {
32+
int numTrees(int n) {
33+
int result = 1;
34+
for (int i = 0; i < n; i++) {
35+
result = result * (2 * n - i) ~/ (i + 1);
36+
}
37+
result ~/= (n + 1);
38+
return result;
39+
}
40+
}
41+
```

0 commit comments

Comments
 (0)