Practice (TheColoringMethod)

back to index  |  new

Solve $17^x-15^y=2$ in positive integers.


Given an string as input, find the length of the longest palindrome it contains. A palindrome is a word which stays the same if reading backwards. Examples include madam, Hannah. 


Determine whether a given integer can be expressed as the sum of two perfect squares.


(Coin Change Problem) Given a set of $N$ possible denominations of coins, find the number of different combinations to pay for a desired sum.


Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.


You are given an integer array height of length $n$. There are $n$ vertical lines drawn such that the two endpoints of the $i$th line are $(i, 0)$ and ($i$, height[$i$]). Find two lines that together with the $x$-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store.

Input: height = $[1,8,6,2,5,4,8,3,7]$

Output: $49$

Explanation: The above vertical lines are represented by array $[1,8,6,2,5,4,8,3,7]$. In this case, the max area of water (blue section) the container can contain is $49$.


A numeric array of length $N$ is given. We need to design a function that finds all positive numbers in the array that have their opposites in it as well. Describe approaches for solving optimal worst case and optimal average case performance, respectively.


Design an algorithm that finds the number of ways in which you can traverse $N$ meters by doing jumps of $1$, $2$, $3$, $4$, or $5$ meter lengths.


On a $M\times N$ board, some cells are occupied. Find the size of the largest square of unoccupied cells. 


Given $n$ non-negative integers representing an elevation map where the width of each bar is $1$, compute how much water it can trap after raining.

Example $1$:

Input: height = $[0,1,0,2,1,0,1,3,2,1,2,1]$

Output: $6$

Explanation: The above elevation map (black section) is represented by array $[0,1,0,2,1,0,1,3,2,1,2,1]$. In this case, $6$ units of rain water (blue section) are being trapped.

Example $2$:

Input: height = $[4,2,0,3,2,5]4

Output: $9$


Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. (Note that after backspacing an empty text, the text will continue empty.)

Example 1:

Input: s = "ab#c", t = "ad#c"

Output: true

Explanation: Both s and t become "ac".

Example 2:

Input: s = "ab##", t = "c#d#"

Output: true

Explanation: Both s and t become "".

Example 3:

Input: s = "a#c", t = "b"

Output: false

Explanation: s becomes "c" while t becomes "b".


You are given a string s consisting of lower case English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them. We repeatedly make duplicate removal on s until we no longer can. Return the final string after all such duplicate removals have been made.

Example:

Input : s = "abbaca"

Output :  "ca"

1. remove bb

2. remove aa


Let $T=\underbrace{333\cdots 3}_{3^{2024}}$. Find the largest power of $3$ that can divid $T$.


Lifting The Exponent (LTE) Let $v_p(n)$ be the largest power of a prime $p$ that divides a positive integer $n$, and $x$, $y$ be any two integers such that $p \not\mid x$ and $p \not\mid y$, then

  • When $p$ is odd
    • If $p\mid x-y$, then $v_p(x^n-y^n) = v_p(x-y)+v_p(n)$
    • If $n$ is odd and $p\mid x+y$, then $v_p(x^n+y^n) = v_p(x+y)+vp(n)$
    • If $n$ is even and $p\mid x+y$, then $v_p(x^n+y^n) = 0$
  • When $p=$
    • If $2\mid x-y$ and $n$ is even, then $v_2(x^n-y^n)=v_2(x-y)+v_2(x+y)+v_2(n)-1$
    • If $2\mid x-y$ and $n$ is odd, then $v_2(x^n-y^n)=v_2(x-y)$
  • For all $p$
    • if $gcd(n,p)=1$, and $p\mid x-y$, then $v_p(x^n-y^n)=v_p(x-y)$
    • If $bcd(n,p)=1$, $p\mid x+y$ and n odd, then $v_p(x^n+y^n) = v_p(x+y)$

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