Skip to content

Commit f0a7e85

Browse files
authored
Merge branch 'main' into leetcodeque-409
2 parents 625a59f + a26a903 commit f0a7e85

File tree

2 files changed

+288
-0
lines changed

2 files changed

+288
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
id: plus-one
3+
title: Plus One
4+
sidebar_label: Plus-One
5+
tags:
6+
- Arrays
7+
- STL
8+
description: "This tutorial covers the solution to the Plus One problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
Given a non-negative number represented as a list of digits, add `1` to the number (increment the number represented by the digits). The digits are stored such that the most significant digit is first element of array.
12+
13+
14+
15+
## Examples
16+
17+
**Example 1:**
18+
19+
```
20+
Input:
21+
N = 3
22+
arr[] = {1, 2, 4}
23+
Output:
24+
1 2 5
25+
Explanation:
26+
124+1 = 125, and so the Output
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input:
33+
N = 3
34+
arr[] = {9,9,9}
35+
Output:
36+
1 0 0 0
37+
Explanation:
38+
999+1 = 1000, and so the output
39+
```
40+
41+
## Your Task
42+
43+
You don't need to read input or print anything. You only need to complete the function `increment()` that takes an integer N, and an array arr of size N as input and returns a list of integers denoting the new number which we get after adding one to the number denoted by the array arr.
44+
45+
Expected Time Complexity: $O(n)$
46+
47+
Expected Auxiliary Space: $O(1)$ for iterative approach.
48+
49+
## Constraints
50+
51+
* `1 ≤ n ≤ 10^5`
52+
53+
## Problem Explanation
54+
55+
Given a non-negative number represented as a list of digits, add `1` to the number (increment the number represented by the digits). The digits are stored such that the most significant digit is first element of array.
56+
57+
## Code Implementation
58+
59+
<Tabs>
60+
<TabItem value="Python" label="Python" default>
61+
<SolutionAuthor name="@arunimad6yuq"/>
62+
63+
```py
64+
def increment(digits):
65+
for i in range(len(digits) - 1, -1, -1):
66+
if digits[i] == 9:
67+
digits[i] = 0
68+
else:
69+
digits[i] += 1
70+
return digits
71+
digits.insert(0, 1)
72+
return digits
73+
74+
```
75+
76+
</TabItem>
77+
<TabItem value="C++" label="C++">
78+
<SolutionAuthor name="@YourUsername"/>
79+
80+
```cpp
81+
vector<int> increment(vector<int>& digits) {
82+
for (int i = digits.size() - 1; i >= 0; --i) {
83+
if (digits[i] == 9) {
84+
digits[i] = 0;
85+
} else {
86+
digits[i]++;
87+
return digits;
88+
}
89+
}
90+
digits.insert(digits.begin(), 1);
91+
return digits;
92+
}
93+
94+
```
95+
96+
</TabItem>
97+
98+
<TabItem value="Javascript" label="Javascript" default>
99+
<SolutionAuthor name="@Ishitamukherjee2004"/>
100+
101+
```javascript
102+
function increment(digits) {
103+
for (let i = digits.length - 1; i >= 0; --i) {
104+
if (digits[i] === 9) {
105+
digits[i] = 0;
106+
} else {
107+
digits[i]++;
108+
return digits;
109+
}
110+
}
111+
digits.unshift(1);
112+
return digits;
113+
}
114+
115+
116+
117+
```
118+
119+
</TabItem>
120+
121+
<TabItem value="Typescript" label="Typescript" default>
122+
<SolutionAuthor name="@Ishitamukherjee2004"/>
123+
124+
```typescript
125+
function increment(digits) {
126+
for (let i = digits.length - 1; i >= 0; --i) {
127+
if (digits[i] === 9) {
128+
digits[i] = 0;
129+
} else {
130+
digits[i]++;
131+
return digits;
132+
}
133+
}
134+
digits.unshift(1);
135+
return digits;
136+
}
137+
138+
139+
140+
```
141+
142+
</TabItem>
143+
144+
<TabItem value="Java" label="Java" default>
145+
<SolutionAuthor name="@Ishitamukherjee2004"/>
146+
147+
```java
148+
public List<Integer> increment(List<Integer> digits) {
149+
for (int i = digits.size() - 1; i >= 0; --i) {
150+
if (digits.get(i) == 9) {
151+
digits.set(i, 0);
152+
} else {
153+
digits.set(i, digits.get(i) + 1);
154+
return digits;
155+
}
156+
}
157+
digits.add(0, 1);
158+
return digits;
159+
}
160+
161+
162+
163+
```
164+
165+
</TabItem>
166+
</Tabs>
167+
168+
169+
## Time Complexity
170+
171+
* The iterative approach has a time complexity of $O(n)$.
172+
173+
## Space Complexity
174+
175+
* The space complexity is $O(1)$ since we are using only a fixed amount of extra space.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
id: delete-nodes-and-return-forest
3+
title: Delete Nodes And Return Forest
4+
sidebar_label: Delete Nodes And Return Forest
5+
tags: [Binary Tree, Depth-First Search, C++, Python, Java]
6+
description: Solve the problem of deleting nodes from a binary tree and returning the forest of remaining trees using depth-first search.
7+
---
8+
9+
## Problem Statement
10+
11+
### Problem Description
12+
13+
Given the root of a binary tree, each node in the tree has a distinct value.
14+
15+
After deleting all nodes with a value in `to_delete`, we are left with a forest (a disjoint union of trees).
16+
17+
Return the roots of the trees in the remaining forest. You may return the result in any order.
18+
19+
### Example
20+
21+
**Example 1:**
22+
```
23+
Input: root = [1, 2, 3, 4, 5, 6, 7], to_delete = [3, 5]
24+
Output: [[1, 2, null, 4], [6], [7]]
25+
```
26+
27+
### Constraints
28+
29+
- The number of nodes in the given tree is at most 1000.
30+
- Each node has a distinct value between 1 and 1000.
31+
- `to_delete` contains distinct values between 1 and 1000.
32+
33+
## Solution
34+
35+
### Intuition
36+
37+
The problem can be solved using a depth-first search (DFS) approach. The idea is to traverse the tree and, whenever a node needs to be deleted (i.e., its value is in the `to_delete` list), we handle its children by potentially adding them to the forest if they are not to be deleted. We maintain a set of nodes to be deleted for quick lookup and use a helper function to perform the DFS and manage the forest creation.
38+
39+
### Time Complexity and Space Complexity Analysis
40+
41+
- **Time Complexity**:
42+
- The solution involves a single traversal of the tree, making the time complexity $O(n)$, where `n` is the number of nodes in the tree.
43+
44+
- **Space Complexity**:
45+
- The space complexity is $O(n)$ due to the recursive call stack and storage for the result list.
46+
47+
### Code
48+
49+
#### C++
50+
51+
```cpp
52+
class Solution {
53+
public:
54+
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
55+
unordered_set<int> to_delete_set(to_delete.begin(), to_delete.end());
56+
vector<TreeNode*> forest;
57+
if (!to_delete_set.count(root->val)) {
58+
forest.push_back(root);
59+
}
60+
dfs(root, to_delete_set, forest);
61+
return forest;
62+
}
63+
64+
private:
65+
TreeNode* dfs(TreeNode* node, unordered_set<int>& to_delete_set, vector<TreeNode*>& forest) {
66+
if (!node) return nullptr;
67+
node->left = dfs(node->left, to_delete_set, forest);
68+
node->right = dfs(node->right, to_delete_set, forest);
69+
if (to_delete_set.count(node->val)) {
70+
if (node->left) forest.push_back(node->left);
71+
if (node->right) forest.push_back(node->right);
72+
return nullptr;
73+
}
74+
return node;
75+
}
76+
};
77+
```
78+
#### Python
79+
```python
80+
class Solution:
81+
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
82+
seats = [0] * (n + 1)
83+
for booking in bookings:
84+
first, last, seats_count = booking
85+
seats[first - 1] += seats_count
86+
if last < n:
87+
seats[last] -= seats_count
88+
for i in range(1, n):
89+
seats[i] += seats[i - 1]
90+
return seats[:-1]
91+
92+
```
93+
#### Java
94+
```java
95+
class Solution {
96+
public int[] corpFlightBookings(int[][] bookings, int n) {
97+
int[] seats = new int[n + 1];
98+
for (int[] booking : bookings) {
99+
int first = booking[0];
100+
int last = booking[1];
101+
int seatsCount = booking[2];
102+
seats[first - 1] += seatsCount;
103+
if (last < n) {
104+
seats[last] -= seatsCount;
105+
}
106+
}
107+
for (int i = 1; i < n; i++) {
108+
seats[i] += seats[i - 1];
109+
}
110+
return Arrays.copyOf(seats, n);
111+
}
112+
}
113+
```

0 commit comments

Comments
 (0)