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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/g0001_0100/s0051_n_queens/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
51\. N-Queens

Hard

The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.

Given an integer `n`, return _all distinct solutions to the **n-queens puzzle**_. You may return the answer in **any order**.

Each solution contains a distinct board configuration of the n-queens' placement, where `'Q'` and `'.'` both indicate a queen and an empty space, respectively.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)

**Input:** n = 4

**Output:** \[\[".Q..","...Q","Q...","..Q."\],\["..Q.","Q...","...Q",".Q.."\]\]

**Explanation:** There exist two distinct solutions to the 4-queens puzzle as shown above

**Example 2:**

**Input:** n = 1

**Output:** \[\["Q"\]\]

**Constraints:**

* `1 <= n <= 9`
27 changes: 27 additions & 0 deletions src/main/java/g0001_0100/s0052_n_queens_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
52\. N-Queens II

Hard

The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.

Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)

**Input:** n = 4

**Output:** 2

**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown.

**Example 2:**

**Input:** n = 1

**Output:** 1

**Constraints:**

* `1 <= n <= 9`
34 changes: 34 additions & 0 deletions src/main/java/g0001_0100/s0053_maximum_subarray/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
53\. Maximum Subarray

Easy

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.

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

**Example 2:**

**Input:** nums = \[1\]

**Output:** 1

**Example 3:**

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

**Output:** 23

**Constraints:**

* `1 <= nums.length <= 105`
* `-104 <= nums[i] <= 104`

**Follow up:** If you have figured out the `O(n)` solution, try coding another solution using the **divide and conquer** approach, which is more subtle.
28 changes: 28 additions & 0 deletions src/main/java/g0001_0100/s0054_spiral_matrix/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
54\. Spiral Matrix

Medium

Given an `m x n` `matrix`, return _all elements of the_ `matrix` _in spiral order_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg)

**Input:** matrix = \[\[1,2,3\],\[4,5,6\],\[7,8,9\]\]

**Output:** \[1,2,3,6,9,8,7,4,5\]

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg)

**Input:** matrix = \[\[1,2,3,4\],\[5,6,7,8\],\[9,10,11,12\]\]

**Output:** \[1,2,3,4,8,12,11,10,9,5,6,7\]

**Constraints:**

* `m == matrix.length`
* `n == matrix[i].length`
* `1 <= m, n <= 10`
* `-100 <= matrix[i][j] <= 100`
28 changes: 28 additions & 0 deletions src/main/java/g0001_0100/s0055_jump_game/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
55\. Jump Game

Medium

You are given an integer array `nums`. You are initially positioned at the array's **first index**, and each element in the array represents your maximum jump length at that position.

Return `true` _if you can reach the last index, or_ `false` _otherwise_.

**Example 1:**

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

**Output:** true

**Explanation:** Jump 1 step from index 0 to 1, then 3 steps to the last index.

**Example 2:**

**Input:** nums = \[3,2,1,0,4\]

**Output:** false

**Explanation:** You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

**Constraints:**

* `1 <= nums.length <= 104`
* `0 <= nums[i] <= 105`
27 changes: 27 additions & 0 deletions src/main/java/g0001_0100/s0056_merge_intervals/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
56\. Merge Intervals

Medium

Given an array of `intervals` where `intervals[i] = [starti, endi]`, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.

**Example 1:**

**Input:** intervals = \[\[1,3\],\[2,6\],\[8,10\],\[15,18\]\]

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

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

**Example 2:**

**Input:** intervals = \[\[1,4\],\[4,5\]\]

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

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

**Constraints:**

* `1 <= intervals.length <= 104`
* `intervals[i].length == 2`
* `0 <= starti <= endi <= 104`
50 changes: 50 additions & 0 deletions src/main/java/g0001_0100/s0057_insert_interval/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
57\. Insert Interval

Medium

You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [starti, endi]` represent the start and the end of the `ith` interval and `intervals` is sorted in ascending order by `starti`. You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval.

Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by `starti` and `intervals` still does not have any overlapping intervals (merge overlapping intervals if necessary).

Return `intervals` _after the insertion_.

**Example 1:**

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

**Output:** \[\[1,5\],\[6,9\]\]

**Example 2:**

**Input:** intervals = \[\[1,2\],\[3,5\],\[6,7\],\[8,10\],\[12,16\]\], newInterval = \[4,8\]

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

**Constraints:**

* `0 <= intervals.length <= 104`
* `intervals[i].length == 2`
* `0 <= starti <= endi <= 105`
* `intervals` is sorted by `starti` in **ascending** order.
* `newInterval.length == 2`
* `0 <= start <= end <= 105`
37 changes: 37 additions & 0 deletions src/main/java/g0001_0100/s0058_length_of_last_word/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
58\. Length of Last Word

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

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

**Example 1:**

**Input:** s = "Hello World"

**Output:** 5

**Explanation:** The last word is "World" with length 5.

**Example 2:**

**Input:** s = " fly me to the moon "

**Output:** 4

**Explanation:** The last word is "moon" with length 4.

**Example 3:**

**Input:** s = "luffy is still joyboy"

**Output:** 6

**Explanation:** The last word is "joyboy" with length 6.

**Constraints:**

* `1 <= s.length <= 104`
* `s` consists of only English letters and spaces `' '`.
* There will be at least one word in `s`.
23 changes: 23 additions & 0 deletions src/main/java/g0001_0100/s0059_spiral_matrix_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
59\. Spiral Matrix II

Medium

Given a positive integer `n`, generate an `n x n` `matrix` filled with elements from `1` to `n2` in spiral order.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg)

**Input:** n = 3

**Output:** \[\[1,2,3\],\[8,9,4\],\[7,6,5\]\]

**Example 2:**

**Input:** n = 1

**Output:** \[\[1\]\]

**Constraints:**

* `1 <= n <= 20`
39 changes: 39 additions & 0 deletions src/main/java/g0001_0100/s0060_permutation_sequence/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
60\. Permutation Sequence

Hard

The set `[1, 2, 3, ..., n]` contains a total of `n!` unique permutations.

By listing and labeling all of the permutations in order, we get the following sequence for `n = 3`:

1. `"123"`
2. `"132"`
3. `"213"`
4. `"231"`
5. `"312"`
6. `"321"`

Given `n` and `k`, return the `kth` permutation sequence.

**Example 1:**

**Input:** n = 3, k = 3

**Output:** "213"

**Example 2:**

**Input:** n = 4, k = 9

**Output:** "2314"

**Example 3:**

**Input:** n = 3, k = 1

**Output:** "123"

**Constraints:**

* `1 <= n <= 9`
* `1 <= k <= n!`