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
8 changes: 4 additions & 4 deletions src/main/java/g0101_0200/s0134_gas_station/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ There are `n` gas stations along a circular route, where the amount of gas at th

You have a car with an unlimited gas tank and it costs `cost[i]` of gas to travel from the <code>i<sup>th</sup></code> station to its next <code>(i + 1)<sup>th</sup></code> station. You begin the journey with an empty tank at one of the gas stations.

Given two integer arrays `gas` and `cost`, return _the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return_ `-1`. If there exists a solution, it is **guaranteed** to be **unique**
Given two integer arrays `gas` and `cost`, return _the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return_ `-1`. If there exists a solution, it is **guaranteed** to be **unique**.

**Example 1:**

Expand Down Expand Up @@ -41,7 +41,7 @@ Given two integer arrays `gas` and `cost`, return _the starting gas station's in

**Constraints:**

* `gas.length == n`
* `cost.length == n`
* `n == gas.length == cost.length`
* <code>1 <= n <= 10<sup>5</sup></code>
* <code>0 <= gas[i], cost[i] <= 10<sup>4</sup></code>
* <code>0 <= gas[i], cost[i] <= 10<sup>4</sup></code>
* The input is generated such that the answer is unique.
6 changes: 3 additions & 3 deletions src/main/java/g0101_0200/s0136_single_number/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ You must implement a solution with a linear runtime complexity and use only cons

**Input:** nums = [2,2,1]

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

**Example 2:**

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

**Output:** 4
**Output:** 4

**Example 3:**

**Input:** nums = [1]

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

**Constraints:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,10 @@ Your code will **only** be given the `head` of the original linked list.

**Output:** [[3,null],[3,0],[3,null]]

**Example 4:**

**Input:** head = []

**Output:** []

**Explanation:** The given linked list is empty (null pointer), so return null.

**Constraints:**

* `0 <= n <= 1000`
* `-10000 <= Node.val <= 10000`
* <code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code>
* `Node.random` is `null` or is pointing to some node in the linked list.

To solve the "Copy List with Random Pointer" problem in Java with a `Solution` class, we'll use a HashMap to maintain a mapping between the original nodes and their corresponding copied nodes. Below are the steps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

Medium

Evaluate the value of an arithmetic expression in [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).
You are given an array of strings `tokens` that represents an arithmetic expression in a [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).

Valid operators are `+`, `-`, `*`, and `/`. Each operand may be an integer or another expression.
Evaluate the expression. Return _an integer that represents the value of the expression_.

**Note** that division between two integers should truncate toward zero.
**Note** that:

It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
* The valid operators are `'+'`, `'-'`, `'*'`, and `'/'`.
* Each operand may be an integer or another expression.
* The division between two integers always **truncates toward zero**.
* There will not be any division by zero.
* The input represents a valid arithmetic expression in a reverse polish notation.
* The answer and all the intermediate calculations can be represented in a **32-bit** integer.

**Example 1:**

Expand Down
12 changes: 0 additions & 12 deletions src/main/java/g0101_0200/s0151_reverse_words_in_a_string/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,6 @@ Return _a string of the words in reverse order concatenated by a single space._

**Explanation:** You need to reduce multiple spaces between two words to a single space in the reversed string.

**Example 4:**

**Input:** s = " Bob Loves Alice "

**Output:** "Alice Loves Bob"

**Example 5:**

**Input:** s = "Alice does not even like bob"

**Output:** "bob like even not does Alice"

**Constraints:**

* <code>1 <= s.length <= 10<sup>4</sup></code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Medium

Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_.
Given an integer array `nums`, find a **non-empty subarrays** that has the largest product, and return _the product_.

It is **guaranteed** that the answer will fit in a **32-bit** integer.
The test cases are generated so that the answer will fit in a **32-bit** integer.

A **subarray** is a contiguous subsequence of the array.
**Note** that the product of an array with a single element is the value of that element.

**Example 1:**

Expand All @@ -28,4 +28,4 @@ A **subarray** is a contiguous subsequence of the array.

* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
* `-10 <= nums[i] <= 10`
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
* The product of any subarray of `nums` is **guaranteed** to fit in a **32-bit** integer.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time resul

Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_.

You must write an algorithm that runs in `O(log n) time.`
You must write an algorithm that runs in `O(log n) time`.

**Example 1:**

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/g0101_0200/s0155_min_stack/readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
155\. Min Stack

Easy
Medium

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

Expand All @@ -12,12 +12,11 @@ Implement the `MinStack` class:
* `int top()` gets the top element of the stack.
* `int getMin()` retrieves the minimum element in the stack.

**Example 1:**
You must implement a solution with `O(1)` time complexity for each function.

**Input**
**Example 1:**

["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
**Input** ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]]

**Output:** [null,null,null,null,-3,null,0,-2]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ The judge will then create the linked structure based on these inputs and pass t

**Output:** Intersected at '8'

**Explanation:** The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
**Explanation:** The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).

From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2<sup>nd</sup> node in A and 3<sup>rd</sup> node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3<sup>rd</sup> node in A and 4<sup>th</sup> node in B) point to the same location in memory.

**Example 2:**

Expand All @@ -58,11 +62,11 @@ The judge will then create the linked structure based on these inputs and pass t

* The number of nodes of `listA` is in the `m`.
* The number of nodes of `listB` is in the `n`.
* <code>0 <= m, n <= 3 * 10<sup>4</sup></code>
* <code>1 <= m, n <= 3 * 10<sup>4</sup></code>
* <code>1 <= Node.val <= 10<sup>5</sup></code>
* `0 <= skipA <= m`
* `0 <= skipB <= n`
* `intersectVal` is `0` if `listA` and `listB` do not intersect.
* `intersectVal == listA[skipA] == listB[skipB]` if `listA` and `listB` intersect.

**Follow up:** Could you write a solution that runs in `O(n)` time and use only `O(1)` memory?
**Follow up:** Could you write a solution that runs in `O(m + n)` time and use only `O(1)` memory?
4 changes: 2 additions & 2 deletions src/main/java/g0101_0200/s0162_find_peak_element/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Medium

A peak element is an element that is strictly greater than its neighbors.

Given an integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**.
Given a **0-indexed** integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**.

You may imagine that `nums[-1] = nums[n] = -∞`.
You may imagine that `nums[-1] = nums[n] = -∞`. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

You must write an algorithm that runs in `O(log n)` time.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
167\. Two Sum II - Input Array Is Sorted

Easy
Medium

Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 <= index<sub>1</sub> < index<sub>2</sub> <= numbers.length</code>.

Return _the indices of the two numbers,_ <code>index<sub>1</sub></code> _and_ <code>index<sub>2</sub></code>_, **added by one** as an integer array_ <code>[index<sub>1</sub>, index<sub>2</sub>]</code> _of length 2._

The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice.

Your solution must use only constant extra space.

**Example 1:**

**Input:** numbers = [2,7,11,15], target = 9
**Input:** numbers = [<ins>2</ins>,<ins>7</ins>,11,15], target = 9

**Output:** [1,2]

**Explanation:** The sum of 2 and 7 is 9. Therefore, index<sub>1</sub> = 1, index<sub>2</sub> = 2. We return [1, 2].

**Example 2:**

**Input:** numbers = [2,3,4], target = 6
**Input:** numbers = [<ins>2</ins>,3,<ins>4</ins>], target = 6

**Output:** [1,3]

**Explanation:** The sum of 2 and 4 is 6. Therefore index<sub>1</sub> = 1, index<sub>2</sub> = 3. We return [1, 3].

**Example 3:**

**Input:** numbers = [\-1,0], target = -1
**Input:** numbers = [<ins>\-1</ins>,<ins>0</ins>], target = -1

**Output:** [1,2]

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/g0101_0200/s0169_majority_element/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ The majority element is the element that appears more than `⌊n / 2⌋` times.

* `n == nums.length`
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>

**Follow-up:** Could you solve the problem in linear time and in `O(1)` space?