Press "Enter" to skip to content

Posts published in “Leetcode”

花花酱 LeetCode Weekly Contest 131 (1021, 1022, 1023, 1024)

LeetCode 1021. Remove Outermost Parentheses

Solution: Track # of opened parentheses

Let n denote the # of opened parentheses after current char, keep ‘(‘ if n > 1 and keep ‘)’ if n > 0

Time complexity: O(n)
Space complexity: O(1)

C++

LeetCode 1022. Sum of Root To Leaf Binary Numbers

Solution: Recursion + Math

Keep tracking the number from root to current node.

Time complexity: O(n)
Space complexity: O(n)

C++

LeetCode 1023. Camelcase Matching

Solution: String…

Time complexity: O(n)
Space complexity: O(n)

C++

LeetCode 1024. Video Stitching

Solution 1: DP

Time complexity: O(nT^2)
Space complexity: O(T^2)

C++

C++/V2

花花酱 LeetCode Weekly Contest 130 (1017, 1018, 1019, 1020)

1017. Convert to Base -2

Solution: Math / Simulation

Time complexity: O(logn)
Space complexity: O(logn)

C++

base K

1018. Binary Prefix Divisible By 5

Solution: Math

Time complexity: O(n)
Space complexity: O(1)

C++

1019. Next Greater Node In Linked List

Solution: Reverse + Monotonic Stack

Process in reverse order and keep a monotonically increasing stack, pop all the elements that are smaller than the current one, then the top of the stack (if exists) will be the next greater node.

Time complexity: O(n)
Space complexity: O(n)

C++

1020. Number of Enclaves

Solution: DFS / Connected Components

Time complexity: O(mn)
Space complexity: O(mn)

C++

花花酱 LeetCode Weekly Contest 129 (1020, 1021, 1022, 1023)

1020. Partition Array Into Three Parts With Equal Sum

Return true if there is a 2/3*sum prefix sum after a 1/3 prefix sum

Time complexity: O(n)
Space complexity: O(1)

Python

1021. Best Sightseeing Pair

Greedy, only keep the best A[i] + i so far and pair with A[j] – j, i < j

Time complexity: O(n)
Space complexity: O(1)

C++

1022. Smallest Integer Divisible by K

Math

Time complexity: O(K)
Space complexity: O(1)

C++

1023. Binary String With Substrings Representing 1 To N

Brute Force, try all possible substrings and convert them into decimals.

Time complexity: O(|S|*log(N)^2)

Python