Hypercube's LeetCode Weekly Contest 52

  • Rank: 244 / 2615
  • Score: 14 / 22
  • Finish Time: 01:30:38

Repeated String Match

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.

For example, with A = “abcd” and B = “cdabcdab”.

Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times (“abcdabcd”).

Note:

The length of A and B will be between 1 and 10000.

Accepted in 00:44:06
class Solution:
    def repeatedStringMatch(self, A, B):
        """
        :type A: str
        :type B: str
        :rtype: int
        """
        n = (len(B) - 1) // len(A) + 1
        if B in A * n:
            return n
        if B in A * (n + 1):
            return n + 1
        return -1

Longest Univalue Path

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.

Note: The length of path between two nodes is represented by the number of edges between them.

Example 1:

Input:
    5
   / \
  4   5
 / \   \
1   1   5
Output: 2

Example 2:

Input:
    1
   / \
  4   5
 / \   \
4   4   5
Output: 2

Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

Accepted in 01:25:38 with 1 wrong submissions
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

def calc(n, v, le):
    if not n:
        return 0, 0
    if v == n.val:
        l, ll = calc(n.left, v, le+1)
        r, rr = calc(n.right, v, le+1)
        return 1 + max(l, r), max(l+r, ll, rr)
    l, ll = calc(n.left, n.val, le+1)
    r, rr = calc(n.right, n.val, le+1)
    return 0, max(l+r, ll, rr)

class Solution:
    def longestUnivaluePath(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        return calc(root, None, 0)[1]

Knight Probability in Chessboard

On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1).

A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.

  |   |   |   |   |   |
--+---+---+---+---+---+--
  |   | # <---> # |   |
--+---+---+ ^ +---+---+--
  | # |   | | |   | # |
--+ ^ +---+ | +---+ ^ +--
  | |<------@------>| |
--+ v +---+ | +---+ v +--
  | # |   | | |   | # |
--+---+---+ v +---+---+--
  |   | # <---> # |   |
--+---+---+---+---+---+--
  |   |   |   |   |   |

Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there.

The knight continues moving until it has made exactly K moves or has moved off the chessboard. Return the probability that the knight remains on the board after it has stopped moving.

Example:

Input: 3, 2, 0, 0
Output: 0.0625
Explanation: There are two moves (to (1,2), (2,1)) that will keep the knight on the board.
From each of those positions, there are also two moves that will keep the knight on the board.
The total probability the knight stays on the board is 0.0625.

Note:

  • N will be between 1 and 25.
  • K will be between 0 and 100.
  • The knight always initially starts on the board.
Accepted in 01:03:41
cache = {}

def calc(N, K, r, c):
    if r not in range(N):
        return 0
    if c not in range(N):
        return 0
    if not K:
        return 1
    r = min(r, N-1-r)
    c = min(c, N-1-c)
    if (N, K, r, c) in cache:
        return cache[N, K, r, c]
    p = 0
    p += calc(N, K-1, r-1, c-2)
    p += calc(N, K-1, r-2, c-1)
    p += calc(N, K-1, r-1, c+2)
    p += calc(N, K-1, r-2, c+1)
    p += calc(N, K-1, r+1, c-2)
    p += calc(N, K-1, r+2, c-1)
    p += calc(N, K-1, r+1, c+2)
    p += calc(N, K-1, r+2, c+1)
    cache[N, K, r, c] = p / 8
    return p / 8

class Solution:
    def knightProbability(self, N, K, r, c):
        """
        :type N: int
        :type K: int
        :type r: int
        :type c: int
        :rtype: float
        """
        return calc(N, K, r, c)

Maximum Sum of 3 Non-Overlapping Subarrays

In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum.

Each subarray will be of size k, and we want to maximize the sum of all 3*k entries.

Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.

Example:

Input: [1,2,1,2,6,7,5,1], 2
Output: [0, 3, 5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.

Note:

  • nums.length will be between 1 and 20000.
  • nums[i] will be between 1 and 65535.
  • k will be between 1 and floor(nums.length / 3).
Unsolved