Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/g0001_0100/s0050_powx_n/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which c

* `-100.0 < x < 100.0`
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code>
* `n` is an integer.
* Either `x` is not zero or `n > 0`.
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code>
16 changes: 9 additions & 7 deletions src/main/java/g0001_0100/s0053_maximum_subarray/readme.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
53\. Maximum Subarray

Easy
Medium

Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return _its sum_.

A **subarray** is a **contiguous** part of an array.
Given an integer array `nums`, find the **non-empty subarrays** with the largest sum, and return _its sum_.

**Example 1:**

**Input:** nums = [-2,1,-3,4,-1,2,1,-5,4]

**Output:** 6

**Explanation:** [4,-1,2,1] has the largest sum = 6.
**Explanation:** The subarray [4,-1,2,1] has the largest sum 6.

**Example 2:**

**Input:** nums = [1]

**Output:** 1
**Output:** 1

**Explanation:** The subarray [1] has the largest sum 1.

**Example 3:**

**Input:** nums = [5,4,-1,7,8]

**Output:** 23
**Output:** 23

**Explanation:** The subarray [5,4,-1,7,8] has the largest sum 23.

**Constraints:**

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/g0001_0100/s0056_merge_intervals/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end

**Output:** [[1,6],[8,10],[15,18]]

**Explanation:** Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
**Explanation:** Since intervals [1,3] and [2,6] overlap, merge them into [1,6].

**Example 2:**

Expand All @@ -20,6 +20,14 @@ Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end

**Explanation:** Intervals [1,4] and [4,5] are considered overlapping.

**Example 3:**

**Input:** intervals = [[4,7],[1,4]]

**Output:** [[1,7]]

**Explanation:** Intervals [1,4] and [4,7] are considered overlapping.

**Constraints:**

* <code>1 <= intervals.length <= 10<sup>4</sup></code>
Expand Down Expand Up @@ -57,4 +65,4 @@ class Solution {
}
```

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.
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.
22 changes: 3 additions & 19 deletions src/main/java/g0001_0100/s0057_insert_interval/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Insert `newInterval` into `intervals` such that `intervals` is still sorted in a

Return `intervals` _after the insertion_.

**Note** that you don't need to modify `intervals` in-place. You can make a new array and return it.

**Example 1:**

**Input:** intervals = [[1,3],[6,9]], newInterval = [2,5]
Expand All @@ -20,25 +22,7 @@ Return `intervals` _after the insertion_.

**Output:** [[1,2],[3,10],[12,16]]

**Explanation:** Because the new interval `[4,8]` overlaps with `[3,5],[6,7],[8,10]`.

**Example 3:**

**Input:** intervals = [], newInterval = [5,7]

**Output:** [[5,7]]

**Example 4:**

**Input:** intervals = [[1,5]], newInterval = [2,3]

**Output:** [[1,5]]

**Example 5:**

**Input:** intervals = [[1,5]], newInterval = [2,7]

**Output:** [[1,7]]
**Explanation:** Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].

**Constraints:**

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/g0001_0100/s0058_length_of_last_word/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Easy

Given a string `s` consisting of some words separated by some number of spaces, return _the length of the **last** word in the string._
Given a string `s` consisting of words and spaces, return _the length of the **last** word in the string._

A **word** is a maximal substring consisting of non-space characters only.
A **word** is a maximal **substring** consisting of non-space characters only.

**Example 1:**

Expand Down
25 changes: 4 additions & 21 deletions src/main/java/g0001_0100/s0062_unique_paths/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Medium

A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
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.

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).
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_.

How many possible unique paths are there?
The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.

**Example 1:**

Expand All @@ -22,24 +22,7 @@ How many possible unique paths are there?

**Output:** 3

**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

**Example 3:**

**Input:** m = 7, n = 3

**Output:** 28

**Example 4:**

**Input:** m = 3, n = 3

**Output:** 6
**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

**Constraints:**

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/g0001_0100/s0063_unique_paths_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

Medium

A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
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.

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).
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.

Now consider if some obstacles are added to the grids. How many unique paths would there be?
Return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.

An obstacle and space is marked as `1` and `0` respectively in the grid.
The testcases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.

**Example 1:**

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0064_minimum_path_sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Given a `m x n` `grid` filled with non-negative numbers, find a path from top le
* `m == grid.length`
* `n == grid[i].length`
* `1 <= m, n <= 200`
* `0 <= grid[i][j] <= 100`
* `0 <= grid[i][j] <= 200`

To solve the "Minimum Path Sum" problem in Java with the Solution class, follow these steps:

Expand Down
10 changes: 1 addition & 9 deletions src/main/java/g0001_0100/s0066_plus_one/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Easy

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.
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.

Increment the large integer by one and return _the resulting array of digits_.

Expand All @@ -24,14 +24,6 @@ Increment the large integer by one and return _the resulting array of digits_.

**Example 3:**

**Input:** digits = [0]

**Output:** [1]

**Explanation:** The array represents the integer 0. Incrementing by one gives 0 + 1 = 1. Thus, the result should be [1].

**Example 4:**

**Input:** digits = [9]

**Output:** [1,0]
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/g0001_0100/s0068_text_justification/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ You should pack your words in a greedy approach; that is, pack as many words as

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.

For the last line of text, it should be left-justified and no extra space is inserted between words.
For the last line of text, it should be left-justified, and no extra space is inserted between words.

**Note:**

* A word is defined as a character sequence consisting of non-space characters only.
* Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
* Each word's length is guaranteed to be greater than `0` and not exceed `maxWidth`.
* The input array `words` contains at least one word.

**Example 1:**
Expand All @@ -28,7 +28,7 @@ For the last line of text, it should be left-justified and no extra space is ins

**Output:** [ "What must be", "acknowledgment ", "shall be " ]

**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.
**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.

**Example 3:**

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/g0001_0100/s0069_sqrtx/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@

Easy

Given a non-negative integer `x`, compute and return _the square root of_ `x`.
Given a non-negative integer `x`, return _the square root of_ `x` _rounded down to the nearest integer_. The returned integer should be **non-negative** as well.

Since the return type is an integer, the decimal digits are **truncated**, and only **the integer part** of the result is returned.
You **must not use** any built-in exponent function or operator.

**Note:** You are not allowed to use any built-in exponent function or operator, such as `pow(x, 0.5)` or `x ** 0.5`.
* For example, do not use `pow(x, 0.5)` in c++ or <code>x ** 0.5</code> in python.

**Example 1:**

**Input:** x = 4

**Output:** 2
**Output:** 2

**Explanation:** The square root of 4 is 2, so we return 2.

**Example 2:**

**Input:** x = 8

**Output:** 2

**Explanation:** The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
**Explanation:** The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.

**Constraints:**

* <code>0 <= x <= 2<sup>31</sup> - 1</code>
* <code>0 <= x <= 2<sup>31</sup> - 1</code>
59 changes: 42 additions & 17 deletions src/main/java/g0001_0100/s0071_simplify_path/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,73 @@

Medium

Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**.
You are given an _absolute_ path for a Unix-style file system, which always begins with a slash `'/'`. Your task is to transform this absolute path into its **simplified canonical path**.

In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level, and any multiple consecutive slashes (i.e. `'//'`) are treated as a single slash `'/'`. For this problem, any other format of periods such as `'...'` are treated as file/directory names.
The _rules_ of a Unix-style file system are as follows:

The **canonical path** should have the following format:
* A single period `'.'` represents the current directory.
* A double period `'..'` represents the previous/parent directory.
* Multiple consecutive slashes such as `'//'` and `'///'` are treated as a single slash `'/'`.
* Any sequence of periods that does **not match** the rules above should be treated as a **valid directory or** **file** **name**. For example, `'...'` and `'....'` are valid directory or file names.

* The path starts with a single slash `'/'`.
* Any two directories are separated by a single slash `'/'`.
* The path does not end with a trailing `'/'`.
* The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period `'.'` or double period `'..'`)
The simplified canonical path should follow these _rules_:

Return _the simplified **canonical path**_.
* The path must start with a single slash `'/'`.
* Directories within the path must be separated by exactly one slash `'/'`.
* The path must not end with a slash `'/'`, unless it is the root directory.
* The path must not have any single or double periods (`'.'` and `'..'`) used to denote current or parent directories.

Return the **simplified canonical path**.

**Example 1:**

**Input:** path = "/home/"

**Output:** "/home"

**Explanation:** Note that there is no trailing slash after the last directory name.
**Explanation:**

The trailing slash should be removed.

**Example 2:**

**Input:** path = "/../"
**Input:** path = "/home//foo/"

**Output:** "/"
**Output:** "/home/foo"

**Explanation:**

**Explanation:** Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
Multiple consecutive slashes are replaced by a single one.

**Example 3:**

**Input:** path = "/home//foo/"
**Input:** path = "/home/user/Documents/../Pictures"

**Output:** "/home/foo"
**Output:** "/home/user/Pictures"

**Explanation:**

**Explanation:** In the canonical path, multiple consecutive slashes are replaced by a single one.
A double period `".."` refers to the directory up a level (the parent directory).

**Example 4:**

**Input:** path = "/a/./b/../../c/"
**Input:** path = "/../"

**Output:** "/"

**Explanation:**

Going one level up from the root directory is not possible.

**Example 5:**

**Input:** path = "/.../a/../b/c/../d/./"

**Output:** "/.../b/d"

**Explanation:**

**Output:** "/c"
`"..."` is a valid name for a directory in this problem.

**Constraints:**

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0001_0100/s0072_edit_distance/readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
72\. Edit Distance

Hard
Medium

Given two strings `word1` and `word2`, return _the minimum number of operations required to convert `word1` to `word2`_.

Expand Down