Skip to content

Commit bcaf0c5

Browse files
committed
feat: add solutions to lc problems: No.1103,2096
* No.1103.Distribute Candies to People * No.2096.Step-By-Step Directions From a Binary Tree Node to Another
1 parent 5308064 commit bcaf0c5

File tree

9 files changed

+346
-18
lines changed

9 files changed

+346
-18
lines changed

solution/1100-1199/1103.Distribute Candies to People/README.md

+57-2
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,82 @@
5151
<li><code>1 &lt;= num_people &lt;= 1000</code></li>
5252
</ul>
5353

54-
5554
## 解法
5655

5756
<!-- 这里可写通用的实现逻辑 -->
5857

58+
直接暴力模拟发糖即可。
59+
5960
<!-- tabs:start -->
6061

6162
### **Python3**
6263

6364
<!-- 这里可写当前语言的特殊实现逻辑 -->
6465

6566
```python
66-
67+
class Solution:
68+
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
69+
ans = [0] * num_people
70+
i = 0
71+
while candies > 0:
72+
ans[i % num_people] += min(candies, i + 1)
73+
candies -= min(candies, i + 1)
74+
i += 1
75+
return ans
6776
```
6877

6978
### **Java**
7079

7180
<!-- 这里可写当前语言的特殊实现逻辑 -->
7281

7382
```java
83+
class Solution {
84+
public int[] distributeCandies(int candies, int num_people) {
85+
int[] ans = new int[num_people];
86+
for (int i = 0; candies > 0; ++i) {
87+
ans[i % num_people] += Math.min(candies, i + 1);
88+
candies -= Math.min(candies, i + 1);
89+
}
90+
return ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
vector<int> distributeCandies(int candies, int num_people) {
101+
vector<int> ans(num_people);
102+
for (int i = 0; candies > 0; ++i)
103+
{
104+
ans[i % num_people] += min(candies, i + 1);
105+
candies -= min(candies, i + 1);
106+
}
107+
return ans;
108+
}
109+
};
110+
```
74111
112+
### **Go**
113+
114+
```go
115+
func distributeCandies(candies int, num_people int) []int {
116+
ans := make([]int, num_people)
117+
for i := 0; candies > 0; i++ {
118+
ans[i%num_people] += min(candies, i+1)
119+
candies -= min(candies, i+1)
120+
}
121+
return ans
122+
}
123+
124+
func min(a, b int) int {
125+
if a < b {
126+
return a
127+
}
128+
return b
129+
}
75130
```
76131

77132
### **...**

solution/1100-1199/1103.Distribute Candies to People/README_EN.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,74 @@ On the fourth turn, ans[0] += 4, and the final array is [5,2,3].
4747
<li>1 &lt;= num_people &lt;= 1000</li>
4848
</ul>
4949

50-
5150
## Solutions
5251

5352
<!-- tabs:start -->
5453

5554
### **Python3**
5655

5756
```python
58-
57+
class Solution:
58+
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
59+
ans = [0] * num_people
60+
i = 0
61+
while candies > 0:
62+
ans[i % num_people] += min(candies, i + 1)
63+
candies -= min(candies, i + 1)
64+
i += 1
65+
return ans
5966
```
6067

6168
### **Java**
6269

6370
```java
71+
class Solution {
72+
public int[] distributeCandies(int candies, int num_people) {
73+
int[] ans = new int[num_people];
74+
for (int i = 0; candies > 0; ++i) {
75+
ans[i % num_people] += Math.min(candies, i + 1);
76+
candies -= Math.min(candies, i + 1);
77+
}
78+
return ans;
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
vector<int> distributeCandies(int candies, int num_people) {
89+
vector<int> ans(num_people);
90+
for (int i = 0; candies > 0; ++i)
91+
{
92+
ans[i % num_people] += min(candies, i + 1);
93+
candies -= min(candies, i + 1);
94+
}
95+
return ans;
96+
}
97+
};
98+
```
6499
100+
### **Go**
101+
102+
```go
103+
func distributeCandies(candies int, num_people int) []int {
104+
ans := make([]int, num_people)
105+
for i := 0; candies > 0; i++ {
106+
ans[i%num_people] += min(candies, i+1)
107+
candies -= min(candies, i+1)
108+
}
109+
return ans
110+
}
111+
112+
func min(a, b int) int {
113+
if a < b {
114+
return a
115+
}
116+
return b
117+
}
65118
```
66119

67120
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
vector<int> distributeCandies(int candies, int num_people) {
4+
vector<int> ans(num_people);
5+
for (int i = 0; candies > 0; ++i)
6+
{
7+
ans[i % num_people] += min(candies, i + 1);
8+
candies -= min(candies, i + 1);
9+
}
10+
return ans;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func distributeCandies(candies int, num_people int) []int {
2+
ans := make([]int, num_people)
3+
for i := 0; candies > 0; i++ {
4+
ans[i%num_people] += min(candies, i+1)
5+
candies -= min(candies, i+1)
6+
}
7+
return ans
8+
}
9+
10+
func min(a, b int) int {
11+
if a < b {
12+
return a
13+
}
14+
return b
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
class Solution {
22
public int[] distributeCandies(int candies, int num_people) {
3-
int[] res = new int[num_people];
4-
for (int i = 0, cur = 1; candies > 0; ++i, ++cur) {
5-
if (i == num_people) {
6-
i = 0;
7-
}
8-
if (candies >= cur) {
9-
res[i] += cur;
10-
candies -= cur;
11-
} else {
12-
res[i] += candies;
13-
candies = 0;
14-
}
3+
int[] ans = new int[num_people];
4+
for (int i = 0; candies > 0; ++i) {
5+
ans[i % num_people] += Math.min(candies, i + 1);
6+
candies -= Math.min(candies, i + 1);
157
}
16-
return res;
8+
return ans;
179
}
18-
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
3+
ans = [0] * num_people
4+
i = 0
5+
while candies > 0:
6+
ans[i % num_people] += min(candies, i + 1)
7+
candies -= min(candies, i + 1)
8+
i += 1
9+
return ans

solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README.md

+67
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
先预处理父子节点的关系,然后 DFS 搜索即可。
59+
5860
<!-- tabs:start -->
5961

6062
### **Python3**
@@ -177,6 +179,71 @@ class Solution {
177179
}
178180
```
179181

182+
### **C++**
183+
184+
```cpp
185+
/**
186+
* Definition for a binary tree node.
187+
* struct TreeNode {
188+
* int val;
189+
* TreeNode *left;
190+
* TreeNode *right;
191+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
192+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
193+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
194+
* };
195+
*/
196+
class Solution {
197+
public:
198+
unordered_map<int, vector<pair<int, char>>> edges;
199+
unordered_set<int> visited;
200+
string ans;
201+
202+
string getDirections(TreeNode* root, int startValue, int destValue) {
203+
ans = "";
204+
traverse(root);
205+
string t = "";
206+
dfs(startValue, destValue, t);
207+
return ans;
208+
}
209+
210+
void traverse(TreeNode* root) {
211+
if (!root) return;
212+
if (root->left)
213+
{
214+
edges[root->val].push_back({root->left->val, 'L'});
215+
edges[root->left->val].push_back({root->val, 'U'});
216+
}
217+
if (root->right)
218+
{
219+
edges[root->val].push_back({root->right->val, 'R'});
220+
edges[root->right->val].push_back({root->val, 'U'});
221+
}
222+
traverse(root->left);
223+
traverse(root->right);
224+
}
225+
226+
void dfs(int start, int dest, string& t) {
227+
if (visited.count(start)) return;
228+
if (start == dest)
229+
{
230+
if (ans == "" || ans.size() > t.size()) ans = t;
231+
return;
232+
}
233+
visited.insert(start);
234+
if (edges.count(start))
235+
{
236+
for (auto& item : edges[start])
237+
{
238+
t += item.second;
239+
dfs(item.first, dest, t);
240+
t.pop_back();
241+
}
242+
}
243+
}
244+
};
245+
```
246+
180247
### **TypeScript**
181248
182249
<!-- 这里可写当前语言的特殊实现逻辑 -->

solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/README_EN.md

+65
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,71 @@ class Solution {
165165
}
166166
```
167167

168+
### **C++**
169+
170+
```cpp
171+
/**
172+
* Definition for a binary tree node.
173+
* struct TreeNode {
174+
* int val;
175+
* TreeNode *left;
176+
* TreeNode *right;
177+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
178+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
179+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
180+
* };
181+
*/
182+
class Solution {
183+
public:
184+
unordered_map<int, vector<pair<int, char>>> edges;
185+
unordered_set<int> visited;
186+
string ans;
187+
188+
string getDirections(TreeNode* root, int startValue, int destValue) {
189+
ans = "";
190+
traverse(root);
191+
string t = "";
192+
dfs(startValue, destValue, t);
193+
return ans;
194+
}
195+
196+
void traverse(TreeNode* root) {
197+
if (!root) return;
198+
if (root->left)
199+
{
200+
edges[root->val].push_back({root->left->val, 'L'});
201+
edges[root->left->val].push_back({root->val, 'U'});
202+
}
203+
if (root->right)
204+
{
205+
edges[root->val].push_back({root->right->val, 'R'});
206+
edges[root->right->val].push_back({root->val, 'U'});
207+
}
208+
traverse(root->left);
209+
traverse(root->right);
210+
}
211+
212+
void dfs(int start, int dest, string& t) {
213+
if (visited.count(start)) return;
214+
if (start == dest)
215+
{
216+
if (ans == "" || ans.size() > t.size()) ans = t;
217+
return;
218+
}
219+
visited.insert(start);
220+
if (edges.count(start))
221+
{
222+
for (auto& item : edges[start])
223+
{
224+
t += item.second;
225+
dfs(item.first, dest, t);
226+
t.pop_back();
227+
}
228+
}
229+
}
230+
};
231+
```
232+
168233
### **TypeScript**
169234
170235
```ts

0 commit comments

Comments
 (0)