Press "Enter" to skip to content

Posts tagged as “hard”

花花酱 LeetCode 927. Three Equal Parts

Problem

Given an arrayĀ AĀ ofĀ 0s andĀ 1s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value.

If it is possible, returnĀ anyĀ [i, j]Ā withĀ i+1 < j, such that:

  • A[0], A[1], ..., A[i]Ā is the first part;
  • A[i+1], A[i+2], ..., A[j-1]Ā is the second part, and
  • A[j], A[j+1], ..., A[A.length - 1]Ā is the third part.
  • All three parts have equal binary value.

If it is not possible, returnĀ [-1, -1].

Note that the entire part is used when considering what binary value it represents.Ā  For example,Ā [1,1,0]Ā representsĀ 6Ā in decimal,Ā notĀ 3.Ā  Also, leading zeros are allowed, soĀ [0,1,1]Ā andĀ [1,1]Ā represent the same value.

 

Example 1:

Input: [1,0,1,0,1]
Output: [0,3]

Example 2:

Input: [1,1,0,1,1]
Output: [-1,-1]

Note:

  1. 3 <= A.length <= 30000
  2. A[i] == 0Ā orĀ A[i] == 1

Solution:

each part should have the same number of 1 s.

Find the suffix (without leading os) of the last part which should have 1/3 of the total ones.

Time complexity: O(n^2) in theory but close to O(n) in practice

Space complexity: O(n)

C++

花花酱 LeetCode 924. Minimize Malware Spread

Problem

In a network of nodes, each nodeĀ iĀ is directly connected to another nodeĀ jĀ if and only ifĀ graph[i][j] = 1.

Some nodesĀ initialĀ are initially infected by malware.Ā  Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware.Ā  This spread of malware will continue until no more nodes can be infected in this manner.

SupposeĀ M(initial)Ā is the final number of nodes infected with malware in the entire network, after the spread of malware stops.

We willĀ remove one node from the initial list.Ā  Return the node that if removed, would minimizeĀ M(initial).Ā  If multiple nodes could be removed to minimizeĀ M(initial), return such a node with the smallest index.

Note that if a node was removed from theĀ initialĀ list of infected nodes, it may still be infected later as a result of the malware spread.

 

Example 1:

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0

Example 2:

Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
Output: 0

Example 3:

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
Output: 1

Note:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 0 <= initial[i] < graph.length

Solution: BFS

Time complexity: O(n^3)

Space complexity: O(n^2)

C++

 

花花酱 LeetCode 920. Number of Music Playlists

Problem

Your music player containsĀ NĀ different songs and she wants to listen toĀ LĀ (not necessarily different) songs during your trip. Ā YouĀ createĀ a playlist soĀ that:

  • Every song is played at least once
  • A song can only be played again only ifĀ KĀ other songs have been played

Return the number of possible playlists.Ā Ā As the answer can be very large, return it moduloĀ 10^9 + 7.

 

Example 1:

Input: N = 3, L = 3, K = 1
Output: 6
Explanation: There are 6 possible playlists. [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1].

Example 2:

Input: N = 2, L = 3, K = 0
Output: 6
Explanation: There are 6 possible playlists. [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], [1, 2, 2]

Example 3:

Input: N = 2, L = 3, K = 1
Output: 2
Explanation: There are 2 possible playlists. [1, 2, 1], [2, 1, 2]

Note:

  1. 0 <= K < N <= L <= 100

Solution: DP

dp[i][j] := # of playlists of length i using j different songs.

dp[i][j] = dp[i – 1][j – 1] * (N – (j – 1))Ā  +Ā  // Adding a new song. j – 1 used, choose any one from (N – (j – 1)) unused.
dp[i -1][j] * max(j – K, 0)Ā  Ā  Ā  Ā  Ā // Reuse an existing song.

Time complexity: O(LN)

Space complexity: O(LN) -> O(N)

C++/O(LN)

C++/O(N)

花花酱 LeetCode 32. Longest Valid Parentheses

Problem

Given a string containing just the charactersĀ '('Ā andĀ ')', find the length of the longest valid (well-formed) parentheses substring.

Example 1:

Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

Input: ")()())" Output: 4 Explanation: The longest valid parentheses substring is "()()"

Solution: Stack

Use a stack to track the index of all unmatched open parentheses.

Time complexity: O(n)

Space complexity: O(n)

C++

Python3

 

Related Problems

花花酱 LeetCode 25. Reverse Nodes in k-Group

Problem

Given a linked list, reverse the nodes of a linked listĀ kĀ at a time and return its modified list.

kĀ is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple ofĀ kĀ then left-out nodes in the end should remain as it is.

Example:

Given this linked list:Ā 1->2->3->4->5

ForĀ kĀ = 2, you should return:Ā 2->1->4->3->5

ForĀ kĀ = 3, you should return:Ā 3->2->1->4->5

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list’s nodes, only nodes itself may be changed.

Solution

Two passes.

First pass, get the length of the list.

Second pass, swap in groups.

Time complexity: O(n)

Space complexity: O(1)

C++

Related Problems