You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To solve the "Merge Intervals" problem in Java with the Solution class, follow these steps:
30
-
31
-
1. Define a method `merge` in the `Solution` class that takes an array of integer arrays `intervals` as input and returns an array of the non-overlapping intervals that cover all the intervals in the input.
32
-
2. Sort the intervals based on the start times.
33
-
3. Initialize an ArrayList to store the merged intervals.
34
-
4. Iterate through the sorted intervals:
35
-
- If the list of merged intervals is empty or the current interval's start time is greater than the end time of the last merged interval, add the current interval to the list of merged intervals.
36
-
- Otherwise, merge the current interval with the last merged interval by updating its end time if needed.
37
-
5. Convert the ArrayList of merged intervals into an array and return it as the result.
38
-
39
-
Here's the implementation of the `merge` method in Java:
40
-
41
-
```java
42
-
importjava.util.*;
43
-
44
-
classSolution {
45
-
publicint[][] merge(int[][] intervals) {
46
-
Arrays.sort(intervals, (a, b) ->Integer.compare(a[0], b[0]));
47
-
List<int[]> merged =newArrayList<>();
48
-
for (int[] interval : intervals) {
49
-
if (merged.isEmpty() || interval[0] > merged.get(merged.size() -1)[1]) {
This implementation efficiently merges overlapping intervals in the given array `intervals` using sorting and iteration, with a time complexity of O(n log n) due to sorting.
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0062_unique_paths/readme.md
+4-21Lines changed: 4 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,11 @@
2
2
3
3
Medium
4
4
5
-
A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
5
+
There is a robot on an `m x n` grid. The robot is initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.
6
6
7
-
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
7
+
Given the two integers `m` and `n`, return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
8
8
9
-
How many possible unique paths are there?
9
+
The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
10
10
11
11
**Example 1:**
12
12
@@ -22,24 +22,7 @@ How many possible unique paths are there?
22
22
23
23
**Output:** 3
24
24
25
-
**Explanation:**
26
-
27
-
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
28
-
1. Right -> Down -> Down
29
-
2. Down -> Down -> Right
30
-
3. Down -> Right -> Down
31
-
32
-
**Example 3:**
33
-
34
-
**Input:** m = 7, n = 3
35
-
36
-
**Output:** 28
37
-
38
-
**Example 4:**
39
-
40
-
**Input:** m = 3, n = 3
41
-
42
-
**Output:** 6
25
+
**Explanation:** From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 1. Right -> Down -> Down 2. Down -> Down -> Right 3. Down -> Right -> Down
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0063_unique_paths_ii/readme.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
3
3
Medium
4
4
5
-
A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
5
+
You are given an `m x n` integer array `grid`. There is a robot initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.
6
6
7
-
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
7
+
An obstacle and space are marked as `1`or `0` respectively in `grid`. A path that the robot takes cannot include **any** square that is an obstacle.
8
8
9
-
Now consider if some obstacles are added to the grids. How many unique paths would there be?
9
+
Return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
10
10
11
-
An obstacle and space is marked as `1` and `0` respectively in the grid.
11
+
The testcases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0066_plus_one/readme.md
+1-9Lines changed: 1 addition & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
Easy
4
4
5
-
You are given a **large integer** represented as an integer array `digits`, where each `digits[i]` is the `ith` digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading `0`'s.
5
+
You are given a **large integer** represented as an integer array `digits`, where each `digits[i]` is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading `0`'s.
6
6
7
7
Increment the large integer by one and return _the resulting array of digits_.
8
8
@@ -24,14 +24,6 @@ Increment the large integer by one and return _the resulting array of digits_.
24
24
25
25
**Example 3:**
26
26
27
-
**Input:** digits = [0]
28
-
29
-
**Output:**[1]
30
-
31
-
**Explanation:** The array represents the integer 0. Incrementing by one gives 0 + 1 = 1. Thus, the result should be [1].
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0068_text_justification/readme.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,12 +8,12 @@ You should pack your words in a greedy approach; that is, pack as many words as
8
8
9
9
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
10
10
11
-
For the last line of text, it should be left-justified and no extra space is inserted between words.
11
+
For the last line of text, it should be left-justified, and no extra space is inserted between words.
12
12
13
13
**Note:**
14
14
15
15
* A word is defined as a character sequence consisting of non-space characters only.
16
-
* Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
16
+
* Each word's length is guaranteed to be greater than `0` and not exceed `maxWidth`.
17
17
* The input array `words` contains at least one word.
18
18
19
19
**Example 1:**
@@ -28,7 +28,7 @@ For the last line of text, it should be left-justified and no extra space is ins
28
28
29
29
**Output:**[ "What must be", "acknowledgment ", "shall be " ]
30
30
31
-
**Explanation:** Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified becase it contains only one word.
31
+
**Explanation:** Note that the last line is "shall be " instead of "shall be", because the last line must be left-justified instead of fully-justified. Note that the second line is also left-justified because it contains only one word.
0 commit comments