Leetcode 5 Flashcards

1
Q

Merge Sorted Arrays

A

In place relies on three pointers

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Decode String

A

Use two stacks, one for number and one for string.

Time: O(maxK*n), K is max value of k and n is length of the string. We traverse a string of size n and iterate k times to decode each pattern of form k[string]

Space: O(m+n), where m is the number of letters(a-z) and n is the number of digits(0-9). Worst case, stringStack = m, countStack = n.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Pow(x, n)

A

Brute force is just multiply x for n times. time O(n), space O(1)

However, if we have x^n, x^(2n) is just x^n * x^n. Recursion -> Time: O(logn), space O(logn) b/c for each computation we need to store the result of x^n/2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Validate Palindrome

A

Two points starting at beginning and end, check if both are lower case, if so then check if they’re the same. If not, return false

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

First Bad Version

A

Use binary search. If isBadVersion(mid) = false, we know all versions preceding and including mid are all good.

Time: O(logn), Space: O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly