From 0da853c74aaabf9c34f13d48765832b05bb5399b Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Thu, 30 Oct 2025 19:46:47 +0700 Subject: [PATCH 1/2] update readme 50-72 --- .../java/g0001_0100/s0050_powx_n/readme.md | 2 + .../s0053_maximum_subarray/readme.md | 16 ++--- .../s0056_merge_intervals/readme.md | 45 ++++---------- .../s0057_insert_interval/readme.md | 22 +------ .../s0058_length_of_last_word/readme.md | 4 +- .../g0001_0100/s0062_unique_paths/readme.md | 25 ++------ .../s0063_unique_paths_ii/readme.md | 8 +-- .../s0064_minimum_path_sum/readme.md | 2 +- .../java/g0001_0100/s0066_plus_one/readme.md | 10 +--- .../s0068_text_justification/readme.md | 6 +- .../java/g0001_0100/s0069_sqrtx/readme.md | 14 +++-- .../g0001_0100/s0071_simplify_path/readme.md | 59 +++++++++++++------ .../g0001_0100/s0072_edit_distance/readme.md | 2 +- 13 files changed, 90 insertions(+), 125 deletions(-) diff --git a/src/main/java/g0001_0100/s0050_powx_n/readme.md b/src/main/java/g0001_0100/s0050_powx_n/readme.md index 3e93a2f3a..426d83a07 100644 --- a/src/main/java/g0001_0100/s0050_powx_n/readme.md +++ b/src/main/java/g0001_0100/s0050_powx_n/readme.md @@ -28,4 +28,6 @@ Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which c * `-100.0 < x < 100.0` * -231 <= n <= 231-1 +* `n` is an integer. +* Either `x` is not zero or `n > 0`. * -104 <= xn <= 104 \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0053_maximum_subarray/readme.md b/src/main/java/g0001_0100/s0053_maximum_subarray/readme.md index aaeed21c0..f85358539 100644 --- a/src/main/java/g0001_0100/s0053_maximum_subarray/readme.md +++ b/src/main/java/g0001_0100/s0053_maximum_subarray/readme.md @@ -1,10 +1,8 @@ 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:** @@ -12,19 +10,23 @@ A **subarray** is a **contiguous** part of an array. **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:** diff --git a/src/main/java/g0001_0100/s0056_merge_intervals/readme.md b/src/main/java/g0001_0100/s0056_merge_intervals/readme.md index 8c9be4e77..981bfd912 100644 --- a/src/main/java/g0001_0100/s0056_merge_intervals/readme.md +++ b/src/main/java/g0001_0100/s0056_merge_intervals/readme.md @@ -10,7 +10,7 @@ Given an array of `intervals` where intervals[i] = [starti, 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:** @@ -20,41 +20,16 @@ Given an array of `intervals` where intervals[i] = [starti, 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:** * 1 <= intervals.length <= 104 * `intervals[i].length == 2` -* 0 <= starti <= endi <= 104 - -To solve the "Merge Intervals" problem in Java with the Solution class, follow these steps: - -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. -2. Sort the intervals based on the start times. -3. Initialize an ArrayList to store the merged intervals. -4. Iterate through the sorted intervals: - - 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. - - Otherwise, merge the current interval with the last merged interval by updating its end time if needed. -5. Convert the ArrayList of merged intervals into an array and return it as the result. - -Here's the implementation of the `merge` method in Java: - -```java -import java.util.*; - -class Solution { - public int[][] merge(int[][] intervals) { - Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); - List merged = new ArrayList<>(); - for (int[] interval : intervals) { - if (merged.isEmpty() || interval[0] > merged.get(merged.size() - 1)[1]) { - merged.add(interval); - } else { - merged.get(merged.size() - 1)[1] = Math.max(merged.get(merged.size() - 1)[1], interval[1]); - } - } - return merged.toArray(new int[merged.size()][]); - } -} -``` - -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. \ No newline at end of file +* 0 <= starti <= endi <= 104 \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0057_insert_interval/readme.md b/src/main/java/g0001_0100/s0057_insert_interval/readme.md index 1b94c0138..a179bde23 100644 --- a/src/main/java/g0001_0100/s0057_insert_interval/readme.md +++ b/src/main/java/g0001_0100/s0057_insert_interval/readme.md @@ -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] @@ -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:** diff --git a/src/main/java/g0001_0100/s0058_length_of_last_word/readme.md b/src/main/java/g0001_0100/s0058_length_of_last_word/readme.md index 96b92534e..578da4752 100644 --- a/src/main/java/g0001_0100/s0058_length_of_last_word/readme.md +++ b/src/main/java/g0001_0100/s0058_length_of_last_word/readme.md @@ -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:** diff --git a/src/main/java/g0001_0100/s0062_unique_paths/readme.md b/src/main/java/g0001_0100/s0062_unique_paths/readme.md index 31699d919..ec2431fa9 100644 --- a/src/main/java/g0001_0100/s0062_unique_paths/readme.md +++ b/src/main/java/g0001_0100/s0062_unique_paths/readme.md @@ -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 2 * 109. **Example 1:** @@ -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:** diff --git a/src/main/java/g0001_0100/s0063_unique_paths_ii/readme.md b/src/main/java/g0001_0100/s0063_unique_paths_ii/readme.md index 2d511b1af..f4351a18f 100644 --- a/src/main/java/g0001_0100/s0063_unique_paths_ii/readme.md +++ b/src/main/java/g0001_0100/s0063_unique_paths_ii/readme.md @@ -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 2 * 109. **Example 1:** diff --git a/src/main/java/g0001_0100/s0064_minimum_path_sum/readme.md b/src/main/java/g0001_0100/s0064_minimum_path_sum/readme.md index 4882401bd..2a49e04ab 100644 --- a/src/main/java/g0001_0100/s0064_minimum_path_sum/readme.md +++ b/src/main/java/g0001_0100/s0064_minimum_path_sum/readme.md @@ -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: diff --git a/src/main/java/g0001_0100/s0066_plus_one/readme.md b/src/main/java/g0001_0100/s0066_plus_one/readme.md index c2e467dcd..9ad61754e 100644 --- a/src/main/java/g0001_0100/s0066_plus_one/readme.md +++ b/src/main/java/g0001_0100/s0066_plus_one/readme.md @@ -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 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. Increment the large integer by one and return _the resulting array of digits_. @@ -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] diff --git a/src/main/java/g0001_0100/s0068_text_justification/readme.md b/src/main/java/g0001_0100/s0068_text_justification/readme.md index 78e9dd708..8340865ce 100644 --- a/src/main/java/g0001_0100/s0068_text_justification/readme.md +++ b/src/main/java/g0001_0100/s0068_text_justification/readme.md @@ -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:** @@ -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:** diff --git a/src/main/java/g0001_0100/s0069_sqrtx/readme.md b/src/main/java/g0001_0100/s0069_sqrtx/readme.md index 005177f31..4c91b62ca 100644 --- a/src/main/java/g0001_0100/s0069_sqrtx/readme.md +++ b/src/main/java/g0001_0100/s0069_sqrtx/readme.md @@ -2,17 +2,19 @@ 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 x ** 0.5 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:** @@ -20,8 +22,8 @@ Since the return type is an integer, the decimal digits are **truncated**, and o **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:** -* 0 <= x <= 231 - 1 +* 0 <= x <= 231 - 1 \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0071_simplify_path/readme.md b/src/main/java/g0001_0100/s0071_simplify_path/readme.md index af8fa8b02..6f56e3d62 100644 --- a/src/main/java/g0001_0100/s0071_simplify_path/readme.md +++ b/src/main/java/g0001_0100/s0071_simplify_path/readme.md @@ -2,18 +2,23 @@ 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:** @@ -21,29 +26,49 @@ Return _the simplified **canonical path**_. **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:** diff --git a/src/main/java/g0001_0100/s0072_edit_distance/readme.md b/src/main/java/g0001_0100/s0072_edit_distance/readme.md index 166565d35..b7ca3feb5 100644 --- a/src/main/java/g0001_0100/s0072_edit_distance/readme.md +++ b/src/main/java/g0001_0100/s0072_edit_distance/readme.md @@ -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`_. From 7d5b484478d4d703c97e5daa897ba20d83ca8470 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 30 Oct 2025 14:58:44 +0200 Subject: [PATCH 2/2] Update readme.md --- .../s0056_merge_intervals/readme.md | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/g0001_0100/s0056_merge_intervals/readme.md b/src/main/java/g0001_0100/s0056_merge_intervals/readme.md index 981bfd912..64ab839c6 100644 --- a/src/main/java/g0001_0100/s0056_merge_intervals/readme.md +++ b/src/main/java/g0001_0100/s0056_merge_intervals/readme.md @@ -32,4 +32,37 @@ Given an array of `intervals` where intervals[i] = [starti, end * 1 <= intervals.length <= 104 * `intervals[i].length == 2` -* 0 <= starti <= endi <= 104 \ No newline at end of file +* 0 <= starti <= endi <= 104 + +To solve the "Merge Intervals" problem in Java with the Solution class, follow these steps: + +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. +2. Sort the intervals based on the start times. +3. Initialize an ArrayList to store the merged intervals. +4. Iterate through the sorted intervals: + - 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. + - Otherwise, merge the current interval with the last merged interval by updating its end time if needed. +5. Convert the ArrayList of merged intervals into an array and return it as the result. + +Here's the implementation of the `merge` method in Java: + +```java +import java.util.*; + +class Solution { + public int[][] merge(int[][] intervals) { + Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); + List merged = new ArrayList<>(); + for (int[] interval : intervals) { + if (merged.isEmpty() || interval[0] > merged.get(merged.size() - 1)[1]) { + merged.add(interval); + } else { + merged.get(merged.size() - 1)[1] = Math.max(merged.get(merged.size() - 1)[1], interval[1]); + } + } + return merged.toArray(new int[merged.size()][]); + } +} +``` + +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.