This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def shiftingLetters(self, s: str, shifts: List[List[int]]) -> str: | |
n = len(s) | |
arr = [0] * (n + 1) | |
for shift in shifts: | |
start, end, direction = shift[0], shift[1], shift[2] | |
if direction == 1: | |
arr[start] += 1 | |
arr[end + 1] -= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation I: Naive Solution | |
- Enumerate the string and check all the substrings. Check the longest substring that have no repeating characters | |
- Update the result to the count of the longest substring with no repeating characters | |
abcabcbb | |
- From a, we check the substring starting from a | |
- From b, we check the substring starting from b | |
abcabcbb | |
abca --> has repeating characters. We do not need to check the next substring, which is abcab because we already have duplicates from the previous iteration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
[[1,3], [8, 10], [15, 18], [2,6]] | |
1-----3 | |
8------10 | |
15--------18 | |
2------6 | |
- Sort intervals based on the start times | |
[[1,3], [2,6], [8, 10], [15, 18]] | |
- Add the first interval to the res nested array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: Recursive DFS (Backtracking) Solution | |
* We can't reuse a character that we've visited before | |
So, let's do the backtracking brute-force as a human would do | |
word = "ABCCED" | |
We check if we have an A in the board since that's the first letter in the word | |
Yes, we do. Then we check all possible neighbors of A and check if we have a B | |
TC: O(rc * dfs) = O(rc * 4 ^ len(word)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
TC: O(n) | |
SC: O(n) | |
''' | |
from typing import List | |
class Solution: | |
def deleteAndEarn(self, nums: List[int]) -> int: | |
arrNums = [0] * (max(nums) + 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation I: DFS Recursion (Backtracking) | |
Input: [[1,2],[3],[3],[]] | |
Output: [[0,1,3],[0,2,3]] | |
f(0, 3) | |
/ \ | |
f(1,3) f(2,3) | |
| | | |
f(3,3) f(3,3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- First, talk about the case where you don't half the prices of non-adjacent prices | |
- Important inputs | |
n = number of nodes | |
edges.length = n - 1 | |
edgeA >= 0, edgeB <= n - 1 | |
tripA >= 0, tripB <= n - 1 | |
price.length = n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- We can have two data structures. An array and a hashmap | |
- With the array, we can easily choose a random number from it | |
- With the hashmap, we can achieve an insertion and removal of O(1) | |
hashmap --> key = value, value = index | |
array --> contains only the value | |
Insert function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- There is always guaranteed an integer right in front of an opening bracket | |
54[ab6[cd]] | |
stack = 5, 4, [, a, b, 6, [, c, d | |
5, 4, [, a, b, 6 | |
We pop any integer after what we've popped which is 6 --> 6 * cd | |
5, 4, [, a, b, 6*cd | |
54, ab, 6*cd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def twoCitySchedCost(self, costs: List[List[int]]) -> int: | |
costs = sorted(costs, key = lambda x: x[0] - x[1]) | |
res = 0 | |
n = len(costs) // 2 | |
for i in range(n): | |
res += costs[i][0] + costs[i + n][1] | |
NewerOlder