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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Hard

You are given an integer array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer `k`.

Find the maximum profit you can achieve. You may complete at most `k` transactions.
Find the maximum profit you can achieve. You may complete at most `k` transactions: i.e. you may buy at most `k` times and sell at most `k` times.

**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Expand All @@ -26,6 +26,6 @@ Find the maximum profit you can achieve. You may complete at most `k` transactio

**Constraints:**

* `0 <= k <= 100`
* `0 <= prices.length <= 1000`
* `1 <= k <= 100`
* `1 <= prices.length <= 1000`
* `0 <= prices[i] <= 1000`
2 changes: 1 addition & 1 deletion src/main/java/g0101_0200/s0189_rotate_array/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Medium

Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.
Given an integer array `nums`, rotate the array to the right by `k` steps, where `k` is non-negative.

**Example 1:**

Expand Down
44 changes: 32 additions & 12 deletions src/main/java/g0101_0200/s0190_reverse_bits/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,51 @@

Easy

Reverse bits of a given 32 bits unsigned integer.
Reverse bits of a given 32 bits signed integer.

**Note:**
**Example 1:**

* Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
* In Java, the compiler represents the signed integers using [2's complement notation](https://en.wikipedia.org/wiki/Two%27s_complement). Therefore, in **Example 2** above, the input represents the signed integer `-3` and the output represents the signed integer `-1073741825`.
**Input:** n = 43261596

**Example 1:**
**Output:** 964176192

**Explanation:**

Integer

Binary

43261596

**Input:** n = 00000010100101000001111010011100
00000010100101000001111010011100

**Output:** 964176192 (00111001011110000010100101000000)
964176192

**Explanation:** The input binary string **00000010100101000001111010011100** represents the unsigned integer 43261596, so return 964176192 which its binary representation is **00111001011110000010100101000000**.
00111001011110000010100101000000

**Example 2:**

**Input:** n = 11111111111111111111111111111101
**Input:** n = 2147483644

**Output:** 1073741822

**Explanation:**

Integer

Binary

2147483644

01111111111111111111111111111100

**Output:** 3221225471 (10111111111111111111111111111111)
1073741822

**Explanation:** The input binary string **11111111111111111111111111111101** represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is **10111111111111111111111111111111**.
00111111111111111111111111111110

**Constraints:**

* The input must be a **binary string** of length `32`
* <code>0 <= n <= 2<sup>31</sup> - 2</code>
* `n` is even.

**Follow up:** If this function is called many times, how would you optimize it?
29 changes: 15 additions & 14 deletions src/main/java/g0101_0200/s0191_number_of_1_bits/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,40 @@

Easy

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).

**Note:**

* Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
* In Java, the compiler represents the signed integers using [2's complement notation](https://en.wikipedia.org/wiki/Two%27s_complement). Therefore, in **Example 3**, the input represents the signed integer. `-3`.
Given a positive integer `n`, write a function that returns the number of set bits in its binary representation (also known as the [Hamming weight](http://en.wikipedia.org/wiki/Hamming_weight)).

**Example 1:**

**Input:** n = 00000000000000000000000000001011
**Input:** n = 11

**Output:** 3

**Explanation:** The input binary string **00000000000000000000000000001011** has a total of three '1' bits.
**Explanation:**

The input binary string **1011** has a total of three set bits.

**Example 2:**

**Input:** n = 00000000000000000000000010000000
**Input:** n = 128

**Output:** 1

**Explanation:** The input binary string **00000000000000000000000010000000** has a total of one '1' bit.
**Explanation:**

The input binary string **10000000** has a total of one set bit.

**Example 3:**

**Input:** n = 11111111111111111111111111111101
**Input:** n = 2147483645

**Output:** 30

**Output:** 31
**Explanation:**

**Explanation:** The input binary string **11111111111111111111111111111101** has a total of thirty one '1' bits.
The input binary string **1111111111111111111111111111101** has a total of thirty set bits.

**Constraints:**

* The input must be a **binary string** of length `32`.
* <code>1 <= n <= 2<sup>31</sup> - 1</code>

**Follow up:** If this function is called many times, how would you optimize it?
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ Given the `root` of a binary tree, imagine yourself standing on the **right side

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/14/tree.jpg)

**Input:** root = [1,2,3,null,5,null,4]

**Output:** [1,3,4]
**Output:** [1,3,4]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/11/24/tmpd5jn43fs-1.png)

**Example 2:**

**Input:** root = [1,null,3]
**Input:** root = [1,2,3,4,null,null,null,5]

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

**Output:** [1,3]
**Explanation:**

![](https://assets.leetcode.com/uploads/2024/11/24/tmpkpe40xeh-1.png)

**Example 3:**

**Input:** root = [1,null,3]

**Output:** [1,3]

**Example 4:**

**Input:** root = []

**Output:** []
**Output:** []

**Constraints:**

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/g0201_0300/s0205_isomorphic_strings/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@ All occurrences of a character must be replaced with another character while pre

**Input:** s = "egg", t = "add"

**Output:** true
**Output:** true

**Explanation:**

The strings `s` and `t` can be made identical by:

* Mapping `'e'` to `'a'`.
* Mapping `'g'` to `'d'`.

**Example 2:**

**Input:** s = "foo", t = "bar"

**Output:** false
**Output:** false

**Explanation:**

The strings `s` and `t` can not be made identical as `'o'` needs to be mapped to both `'a'` and `'r'`.

**Example 3:**

**Input:** s = "paper", t = "title"

**Output:** true
**Output:** true

**Constraints:**

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0201_0300/s0207_course_schedule/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Return `true` if you can finish all courses. Otherwise, return `false`.

**Constraints:**

* <code>1 <= numCourses <= 10<sup>5</sup></code>
* `1 <= numCourses <= 2000`
* `0 <= prerequisites.length <= 5000`
* `prerequisites[i].length == 2`
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Medium

Given an array of positive integers `nums` and a positive integer `target`, return the minimal length of a **contiguous subarray** <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> of which the sum is greater than or equal to `target`. If there is no such subarray, return `0` instead.
Given an array of positive integers `nums` and a positive integer `target`, return _the **minimal length** of a_ **non-empty subarrays** _whose sum is greater than or equal to_ `target`. If there is no such subarray, return `0` instead.

**Example 1:**

Expand All @@ -28,6 +28,6 @@ Given an array of positive integers `nums` and a positive integer `target`, retu

* <code>1 <= target <= 10<sup>9</sup></code>
* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>4</sup></code>

**Follow up:** If you have figured out the `O(n)` solution, try coding another solution of which the time complexity is `O(n log(n))`.
10 changes: 2 additions & 8 deletions src/main/java/g0201_0300/s0210_course_schedule_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@ Return _the ordering of courses you should take to finish all courses_. If there

**Output:** [0,1]

**Explanation:**

There are a total of 2 courses to take. To take course 1 you should have finished course 0.
So the correct course order is [0,1].
**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].

**Example 2:**

**Input:** numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]

**Output:** [0,2,1,3]

**Explanation:**

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
**Explanation:** There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].

**Example 3:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Implement the `WordDictionary` class:

**Constraints:**

* `1 <= word.length <= 500`
* `word` in `addWord` consists lower-case English letters.
* `word` in `search` consist of `'.'` or lower-case English letters.
* At most `50000` calls will be made to `addWord` and `search`.
* `1 <= word.length <= 25`
* `word` in `addWord` consists of lowercase English letters.
* `word` in `search` consist of `'.'` or lowercase English letters.
* There will be at most `2` dots in `word` for `search` queries.
* At most <code>10<sup>4</sup></code> calls will be made to `addWord` and `search`.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Given an integer array `nums` and an integer `k`, return _the_ <code>k<sup>th</s

Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.

Can you solve it without sorting?

**Example 1:**

**Input:** nums = [3,2,1,5,6,4], k = 2
Expand All @@ -20,5 +22,5 @@ Note that it is the <code>k<sup>th</sup></code> largest element in the sorted or

**Constraints:**

* <code>1 <= k <= nums.length <= 10<sup>4</sup></code>
* <code>1 <= k <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>