Skip to content

Commit c4a2665

Browse files
committed
27-remove-element.md Added hints.
1 parent 5c599ec commit c4a2665

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

images/examples/27_hint_2.png

32.8 KB
Loading

solutions/1-1000/27-remove-element.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,34 @@ It does not matter what you leave beyond the returned k (hence they are undersco
3131
- `0 <= nums[i] <= 50`
3232
- `0 <= val <= 100`
3333

34+
<details>
35+
<summary>Hint 1</summary>
36+
The problem statement clearly asks us to modify the array in-place and it also says that the element beyond the new length of the array can be anything. Given an element, we need to remove all the occurrences of it from the array. We don't technically need to remove that element per-say, right?
37+
</details>
38+
39+
<details>
40+
<summary>Hint 2</summary>
41+
We can move all the occurrences of this element to the end of the array. Use two pointers!
42+
</details>
43+
44+
<details>
45+
<summary>Hint 3</summary>
46+
Yet another direction of thought is to consider the elements to be removed as non-existent. In a single pass, if we keep copying the visible elements in-place, that should also solve this problem for us.
47+
</details>
48+
3449
## Intuition behind the Solution
3550
The goal is to remove the elements in the array that are equal to `val`, and the order of the remaining elements is not important.
3651

37-
### Solution 1
52+
### Solution 1 (easier to think of)
3853
Then we only need to use the following elements that are not equal to `val` to occupy the elements that are equal to `val`.
3954

40-
### Solution 2
55+
![](../../images/examples/27_hint_2.png)
56+
57+
### Solution 2 (more concise and easier to code)
4158
You only need to traverse the array once and keep all numbers that are not equal to `val` at the front of the array.
4259

60+
`slowIndex` is used to save the current front position.
61+
4362
## Complexity
4463
* Time: `O(n)`.
4564
* Space: `O(1)`.
@@ -73,7 +92,7 @@ class Solution {
7392
}
7493
```
7594

76-
### Solution 2: Fast and Slow Pointers (more concise)
95+
### Solution 2: Fast and Slow Pointers (more concise and easier to code)
7796
```java
7897
class Solution {
7998
public int removeElement(int[] nums, int val) {
@@ -115,7 +134,7 @@ class Solution:
115134
return left
116135
```
117136

118-
### Solution 2: Fast and Slow Pointers (more concise)
137+
### Solution 2: Fast and Slow Pointers (more concise and easier to code)
119138
```python
120139
class Solution:
121140
def removeElement(self, nums: List[int], val: int) -> int:
@@ -159,7 +178,7 @@ public:
159178
};
160179
```
161180

162-
### Solution 2: Fast and Slow Pointers (more concise)
181+
### Solution 2: Fast and Slow Pointers (more concise and easier to code)
163182
```c++
164183
class Solution {
165184
public:
@@ -205,7 +224,7 @@ var removeElement = function (nums, val) {
205224
};
206225
```
207226

208-
### Solution 2: Fast and Slow Pointers (more concise)
227+
### Solution 2: Fast and Slow Pointers (more concise and easier to code)
209228
```javascript
210229
var removeElement = function (nums, val) {
211230
let slowIndex = 0
@@ -255,7 +274,7 @@ public class Solution
255274
}
256275
```
257276

258-
### Solution 2: Fast and Slow Pointers (more concise)
277+
### Solution 2: Fast and Slow Pointers (more concise and easier to code)
259278
```c#
260279
public class Solution
261280
{
@@ -304,7 +323,7 @@ func removeElement(nums []int, val int) int {
304323
}
305324
```
306325

307-
### Solution 2: Fast and Slow Pointers (more concise)
326+
### Solution 2: Fast and Slow Pointers (more concise and easier to code)
308327
```go
309328
func removeElement(nums []int, val int) int {
310329
slowIndex := 0
@@ -347,7 +366,7 @@ def remove_element(nums, val)
347366
end
348367
```
349368

350-
### Solution 2: Fast and Slow Pointers (more concise)
369+
### Solution 2: Fast and Slow Pointers (more concise and easier to code)
351370
```ruby
352371
def remove_element(nums, val)
353372
slow_index = 0

solutions/1-1000/977-squares-of-a-sorted-array.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ var sortedSquares = function (nums) {
200200
## C#
201201
### Solution 1: using `sort()`
202202
```c#
203-
public class Solution {
204-
public int[] SortedSquares(int[] nums) {
205-
for (int i = 0; i < nums.Length; i++) {
203+
public class Solution
204+
{
205+
public int[] SortedSquares(int[] nums)
206+
{
207+
for (int i = 0; i < nums.Length; i++)
206208
nums[i] *= nums[i];
207-
}
208209

209210
Array.Sort(nums);
210211

@@ -215,8 +216,10 @@ public class Solution {
215216

216217
### Solution 2: not using `sort()`
217218
```c#
218-
public class Solution {
219-
public int[] SortedSquares(int[] nums) {
219+
public class Solution
220+
{
221+
public int[] SortedSquares(int[] nums)
222+
{
220223
var results = new int[nums.Length];
221224
int left = 0;
222225
int right = nums.Length - 1;

0 commit comments

Comments
 (0)