XingXing Park

猩猩的乐园


  • Home

  • Archives

  • Tags

  • Job Info

  • Search

[Leetcode 1043] Partition Array for Maximum Sum

Posted on 2020-05-24 | In leetcode | Comments:

原题说明

Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray.

Return the largest sum of the given array after partitioning.

Example 1:

Input: A = [1,15,7,9,2,5,10], K = 3
Output: 84
Explanation: A becomes [15,15,15,9,10,10,10]

Note:

  1. 1 <= K <= A.length <= 500
  2. 0 <= A[i] <= 10^6

Read more »

[Leetcode 1041] Robot Bounded In Circle

Posted on 2020-05-24 | In leetcode | Comments:

原题说明

On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can receive one of three instructions:

  • “G”: go straight 1 unit;
  • “L”: turn 90 degrees to the left;
  • “R”: turn 90 degress to the right.

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

 

Example 1:

Input: “GGLLGG”
Output: true
Explanation:
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.

Example 2:

Input: “GG”
Output: false
Explanation:
The robot moves north indefinitely.

Example 3:

Input: “GL”
Output: true
Explanation:
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> …

 

Note:

  1. 1 <= instructions.length <= 100
  2. instructions[i] is in {‘G’, ‘L’, ‘R’}

Read more »

[Leetcode 1044] Longest Duplicate Substring

Posted on 2020-05-23 | In leetcode | Comments:

原题说明

Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times.  (The occurrences may overlap.)

Return any duplicated substring that has the longest possible length.  (If S does not have a duplicated substring, the answer is “”.)

 

Example 1:

Input: “banana”
Output: “ana”

Example 2:

Input: “abcd”
Output: “”

 

Note:

  1. 2 <= S.length <= 10^5
  2. S consists of lowercase English letters.

Read more »

[Leetcode 1042] Flower Planting With No Adjacent

Posted on 2020-05-23 | In leetcode | Comments:

原题说明

You have N gardens, labelled 1 to N.  In each garden, you want to plant one of 4 types of flowers.

paths[i] = [x, y] describes the existence of a bidirectional path from garden x to garden y.

Also, there is no garden that has more than 3 paths coming into or leaving it.

Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.

Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)-th garden.  The flower types are denoted 1, 2, 3, or 4.  It is guaranteed an answer exists.

 

Example 1:

Input: N = 3, paths = [[1,2],[2,3],[3,1]]
Output: [1,2,3]

Example 2:

Input: N = 4, paths = [[1,2],[3,4]]
Output: [1,2,1,2]

Example 3:

Input: N = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
Output: [1,2,3,4]

 

Note:

  • 1 <= N <= 10000
  • 0 <= paths.size <= 20000
  • No garden has 4 or more paths coming into or leaving it.
  • It is guaranteed an answer exists.

Read more »

[Leetcode 1039] Minimum Score Triangulation of Polygon

Posted on 2020-05-17 | In leetcode | Comments:

原题说明

Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], …, A[N-1] in clockwise order.

Suppose you triangulate the polygon into N-2 triangles.  For each triangle, the value of that triangle is the product of the labels of the vertices, and the total score of the triangulation is the sum of these values over all N-2 triangles in the triangulation.

Return the smallest possible total score that you can achieve with some triangulation of the polygon.

 

    Example 1:

    Input: [1,2,3]
    Output: 6
    Explanation: The polygon is already triangulated, and the score of the only triangle is 6.

    Example 2:

    Input: [3,7,4,5]
    Output: 144
    Explanation: There are two triangulations, with possible scores: 375 + 457 = 245, or 345 + 347 = 144. The minimum score is 144.

    Example 3:

    Input: [1,3,1,4,1,5]
    Output: 13
    Explanation: The minimum score triangulation has score 113 + 114 + 115 + 111 = 13.

     

    Note:

    1. 3 <= A.length <= 50
    2. 1 <= A[i] <= 100

    Read more »

    [Leetcode 1037] Valid Boomerang

    Posted on 2020-05-17 | In leetcode | Comments:

    原题说明

    A boomerang is a set of 3 points that are all distinct and not in a straight line.

    Given a list of three points in the plane, return whether these points are a boomerang.

     

    Example 1:

    Input: [[1,1],[2,3],[3,2]]
    Output: true

    Example 2:

    Input: [[1,1],[2,2],[3,3]]
    Output: false

     

    Note:

    1. points.length == 3
    2. points[i].length == 2
    3. 0 <= points[i][j] <= 100

    Read more »

    [Leetcode 1040] Moving Stones Until Consecutive II

    Posted on 2020-05-17 | In leetcode | Comments:

    原题说明

    On an infinite number line, the position of the i-th stone is given by stones[i]. Call a stone an endpoint stone if it has the smallest or largest position.

    Each turn, you pick up an endpoint stone and move it to an unoccupied position so that it is no longer an endpoint stone.

    In particular, if the stones are at say, stones = [1,2,5], you cannot move the endpoint stone at position 5, since moving it to any position (such as 0, or 3) will still keep that stone as an endpoint stone.

    The game ends when you cannot make any more moves, ie. the stones are in consecutive positions.

    When the game ends, what is the minimum and maximum number of moves that you could have made? Return the answer as an length 2 array: answer = [minimum_moves, maximum_moves]

    Example 1

    Input: [7,4,9]
    Output: [1,2]
    Explanation: We can move 4 -> 8 for one move to finish the game.
    Or, we can move 9 -> 5, 4 -> 6 for two moves to finish the game.

    Example 2

    Input: [6,5,4,3,10]
    Output: [2,3]
    Explanation: We can move 3 -> 8 then 10 -> 7 to finish the game.
    Or, we can move 3 -> 7, 4 -> 8, 5 -> 9 to finish the game.
    Notice we cannot move 10 -> 2 to finish the game, because that would be an illegal move.

    Example 3

    Input: [100,101,104,102,103]
    Output: [0,0]

    Note

    1. 3 <= stones.length <= 10^4
    2. 1 <= stones[i] <= 10^9
    3. stones[i] have distinct values.

    Read more »

    [Leetcode 1038] Binary Search Tree to Greater Sum Tree

    Posted on 2020-05-17 | In leetcode | Comments:

    原题说明

    Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val.

    As a reminder, a binary search tree is a tree that satisfies these constraints:

    The left subtree of a node contains only nodes with keys less than the node’s key.
    The right subtree of a node contains only nodes with keys greater than the node’s key.
    Both the left and right subtrees must also be binary search trees.

    Example 1:


    Input:
    [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
    Output:
    [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

    Constraints:

    The number of nodes in the tree is between 1 and 100.
    Each node will have value between 0 and 100.
    The given tree is a binary search tree.
    Note: This question is the same as 538: https://leetcode.com/problems/convert-bst-to-greater-tree/

    Read more »

    [Leetcode 1033] Moving Stones Until Consecutive

    Posted on 2019-08-06 | In leetcode | Comments:

    原题说明

    Three stones are on a number line at positions a, b, and c.

    Each turn, you pick up a stone at an endpoint (ie., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints.  Formally, let’s say the stones are currently at positions x, y, z with x < y < z.  You pick up the stone at either position x or position z, and move that stone to an integer position k, with x < k < z and k != y.

    The game ends when you cannot make any more moves, ie. the stones are in consecutive positions.

    When the game ends, what is the minimum and maximum number of moves that you could have made?  Return the answer as an length 2 array: answer = [minimum_moves, maximum_moves]

     

    Example 1:

    Input: a = 1, b = 2, c = 5
    Output: [1,2]
    Explanation: Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.

    Example 2:

    Input: a = 4, b = 3, c = 2
    Output: [0,0]
    Explanation: We cannot make any moves.

    Example 3:

    Input: a = 3, b = 5, c = 1
    Output: [1,2]
    Explanation: Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.

     

    Note:

    1. 1 <= a <= 100
    2. 1 <= b <= 100
    3. 1 <= c <= 100
    4. a != b, b != c, c != a

    Read more »

    [Leetcode 1036] Escape a Large Maze

    Posted on 2019-05-02 | In leetcode | Comments:

    原题说明

    In a 1 million by 1 million grid, the coordinates of each grid square are (x, y) with 0 <= x, y < 10^6.

    We start at the source square and want to reach the target square. Each move, we can walk to a 4-directionally adjacent square in the grid that isn’t in the given list of blocked squares.

    Return true if and only if it is possible to reach the target square through a sequence of moves.

    Example 1:

    Input: blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
    Output: false
    Explanation: The target square is inaccessible starting from the source square, because we can’t walk outside the grid.

    Example 2:

    Input: blocked = [], source = [0,0], target = [999999,999999]
    Output: true
    Explanation: Because there are no blocked cells, it’s possible to reach the target square.

    Note:

    1. 0 <= blocked.length <= 200
    2. blocked[i].length == 2
    3. 0 <= blocked[i][j] < 10^6
    4. source.length == target.length == 2
    5. 0 <= source[i][j], target[i][j] < 10^6
    6. source != target
    Read more »
    1…456…13

    猩猩的乐园

    技术面试问题详解
    123 posts
    2 categories
    69 tags
    RSS
    © 2018 – 2020 猩猩的乐园
    |