Practice (90/1000)

back to index  |  new

Find all solutions in positive integers to $3^n = x^k + y^k$ where $x$ and $y$ are co-prime and $k\ge 2$.


Suppose $a$ and $b$ are both positive real numbers such as $a-b$, $a^2-b^2$, $a^3-b^3$, $\cdots$, are all positive integers. Show that $a$ and $b$ must be positive integers.


Given a string s, find the length of the longest substring without repeating characters.

Example 1:

Input: s = "abcabcbb"

Output: 3

Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"

Output: 1

Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"

Output: 3

Explanation: The answer is "wke", with the length of 3.

Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.


A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. (Note that s consists only of printable ASCII characters.)

Example 1:

Input: s = "A man, a plan, a canal: Panama"

Output: true

Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"

Output: false

Explanation: "raceacar" is not a palindrome.

Example 3:

Input: s = " "

Output: true

Explanation: s is an empty string "" after removing non-alphanumeric characters.

Since an empty string reads the same forward and backward, it is a palindrome.


Given a string s (containing only lower case English words), return true if the s can be palindrome after deleting at most one character from it.

Example 1:

Input: s = "aba"

Output: true

Example 2:

Input: s = "abca"

Output: true

Explanation: You could delete the character 'c'.

Example 3:

Input: s = "abc"

Output: false


Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list. (Follow up: Could you do it in one pass?)

Example 1:

Input: head = [1,2,3,4,5], left = 2, right = 4

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

Reasoning: 2 - 4 is [2,3,4]. We want to reverse it in place [4,3,2]. We do not modify the linked list elsewhere, giving us the final [1,4,3,2,5]

Example 2:

Input: head = [5], left = 1, right = 1

Output: [5]

 


You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure as shown in the example below.

 

Given the head of the first level of the list, flatten the list so that all the nodes appear in a single-level, doubly linked list. Let curr be a node with a child list. The nodes in the child list should appear after curr and before curr.next in the flattened list.

 

Return the head of the flattened list. The nodes in the list must have all of their child pointers set to null.

 

Example 1:

Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]

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

 

Example 2:

Input: head = [1,2,null,3]

Output: [1,3,2]

 

 

Example 3:

Input: head = []

Output: []

Explanation: There could be empty list in the input.

 

Constraints:

The number of Nodes will not exceed 1000.

1 <= Node.val <= 105

How the multilevel linked list is represented in test cases:

 

We use the multilevel linked list from Example 1 above:

 

 1---2---3---4---5---6--NULL

         |

         7---8---9---10--NULL

             |

             11--12--NULL

The serialization of each level is as follows:

 

[1,2,3,4,5,6,null]

[7,8,9,10,null]

[11,12,null]

To serialize all levels together, we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:

 

[1,    2,      3, 4, 5, 6, null]

                 |

[null, null, 7,  8, 9, 10, null]

                      |

[            null, 11, 12, null]

Merging the serialization of each level and removing trailing nulls we obtain:

 

[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]


Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  • Open brackets must be closed by the same type of brackets.
  • Open brackets must be closed in the correct order.
  • Every close bracket has a corresponding open bracket of the same type.

Example 1:

Input: s = "()"

Output: true

Example 2:

Input: s = "()[]{}"

Output: true

Example 3:

Input: s = "(]"

Output: false


Given a string s of '(' , ')' and lowercase English characters. Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string. Formally, a parentheses string is valid if and only if:

  • It is the empty string, contains only lowercase characters, or
  • It can be written as AB (A concatenated with B), where A and B are valid strings, or
  • It can be written as (A), where A is a valid string.

Example 1:

Input: s = "lee(t(c)o)de)"

Output: "lee(t(c)o)de"

Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.

Example 2:

Input: s = "a)b(c)d"

Output: "ab(c)d"

Example 3:

Input: s = "))(("

Output: ""

Explanation: An empty string is also valid.


Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

Implement the MyQueue class:

  • void push(int x) Pushes element x to the back of the queue.
  • int pop() Removes the element from the front of the queue and returns it.
  • int peek() Returns the element at the front of the queue.
  • boolean empty() Returns true if the queue is empty, false otherwise.

Notes:

You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.

Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.

Example 1:

Input

["MyQueue", "push", "push", "peek", "pop", "empty"]

[[], [1], [2], [], [], []]

Output

[null, null, null, 1, 1, false]

Explanation

MyQueue myQueue = new MyQueue();

myQueue.push(1); // queue is: [1]

myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)

myQueue.peek(); // return 1

myQueue.pop(); // return 1, queue is [2]

myQueue.empty(); // return false

Follow-up: Can you implement the queue such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer.


Given an $m \times n$ 2D binary grid grid which represents a map of '$1$'s (land) and '$0$'s (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input: grid = [

  ["1","1","1","1","0"],

  ["1","1","0","1","0"],

  ["1","1","0","0","0"],

  ["0","0","0","0","0"]

]

Output: 1

 

Example 2:

Input: grid = [

  ["1","1","0","0","0"],

  ["1","1","0","0","0"],

  ["0","0","1","0","0"],

  ["0","0","0","1","1"]

]

Output: 3

 

 

Constraints:

m == grid.length

n == grid[i].length

1 <= m, n <= 300

grid[i][j] is '0' or '1'.


You are given an m x n grid where each cell can have one of three values:

  • $0$ representing an empty cell,
  • $1$ representing a fresh orange, or
  • $2$ representing a rotten orange.

Every minute, any fresh orange that is $4$-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return $-1$.

Example 1

Input: grid = $[[2,1,1],[1,1,0],[0,1,1]]$

Output: $4$

Example 2:

Input: grid = $[[2,1,1],[0,1,1],[1,0,1]]$

Output: $-1$

Explanation: The orange in the bottom left corner (row $2$, column $0$) is never rotten, because rotting only happens $4$-directionally.

Example 3:

Input: grid = $[[0,2]]$

Output: $0$

Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.

Constraints:

$m$ == grid.length

$n$ == grid[$i$].length

$1 \le m, n \le 10$

grid[$i$][$j$] is $0$, $1$, or $2$.


Solve $x^3-3x+1=0$.


Simplify: $\left(\frac{1-\sqrt{5}}{2}\right)^{12}$.


Solve $x^x = 2^{3x+192}$


Let $a, b, c \in\mathbf{R}^+$ and $\frac{a^2}{1+a^2}+\frac{b^2}{1+b^2}+\frac{c^2}{1+c^2}=1$, show that $$abc\le\frac{\sqrt{2}}{4}$$


Solve $\sqrt{x+5}=x^2-5$.


Solve $x^2 + x - 2= x\sqrt{2x^2-3}$.


Find all $a$ such the the equation $\frac{ax-2}{x-2}=3$ has no solution.