Updating exercises

This commit is contained in:
Carlos
2025-07-14 10:09:13 -04:00
parent 57956bf728
commit 7f1381ae93
25 changed files with 634 additions and 13 deletions

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
# Edit at https://www.toptal.com/developers/gitignore?templates=node,java,python,c++
.cursor*
*src/notes*
*backup/
./src/notes/
*sync_leetcode.sh
### C++ ###

View File

@@ -0,0 +1,23 @@
// @leet start
/**
* @param {string[]} words
* @return {number}
*/
var longestStrChain = function (words) {
let cache = {};
words.sort((a, b) => a.length - b.length);
let max = 0;
for (let word of words) {
let longest = 0;
for (let i = 0; i < word.length; i++) {
let subWord = word.slice(0, i) + word.slice(i + 1);
longest = Math.max(longest, (cache[subWord] || 0) + 1);
}
cache[word] = longest;
max = Math.max(max, longest);
}
return max;
};
// @leet end

View File

@@ -50,4 +50,3 @@ class Solution:
# @leet end

View File

@@ -0,0 +1,72 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
self.max_dep = 0
self.sum_max = 0
def dfs(node: TreeNode, depth: int):
if not node:
return
if not node.left and not node.right:
if depth > self.max_dep:
self.max_dep = depth
self.sum_max = node.val
elif depth == self.max_dep:
self.sum_max += node.val
else:
dfs(node.left, depth + 1)
dfs(node.right, depth + 1)
dfs(root, 0)
return self.sum_max
# @leet end

View File

@@ -44,10 +44,9 @@ class Solution:
for i in range(len(ratings) - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
arr[i] = max(arr[i], arr[i + 1] + 1)
arr[i] = max(arr[i + 1] + 1, arr[i])
return sum(arr)
# @leet end

View File

@@ -0,0 +1,49 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
class Solution:
def reverseWords(self, s: str) -> str:
s = s.split(" ")
a = [""] * len(s)
j = len(a) - 1
for i in range(len(s)):
if s[i] != "":
a[j] = s[i]
j -= 1
return " ".join(a).strip()
# @leet end

View File

@@ -43,6 +43,5 @@ class Solution:
nums.sort()
return nums[len(nums)//2]
# @leet end

View File

@@ -0,0 +1,56 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
class Solution:
def maximumDifference(self, nums: List[int]) -> int:
max_val = -1
for i in range(len(nums)):
j = len(nums) - 1
while j > i:
if nums[i] < nums[j]:
diff = nums[j] - nums[i]
max_val = max(diff, max_val)
j -= 1
return max_val
# @leet end

View File

@@ -44,6 +44,7 @@ class Solution:
pos1 = word_indices[word1]
pos2 = word_indices[word2]
# Two-pointer traversal
i, j = 0, 0
min_distance = float("inf")
@@ -52,6 +53,7 @@ class Solution:
index2 = pos2[j]
min_distance = min(min_distance, abs(index1 - index2))
# Move the pointer pointing to the smaller index
if index1 < index2:
i += 1
else:
@@ -59,6 +61,5 @@ class Solution:
return min_distance
# @leet end

View File

@@ -54,6 +54,7 @@ class WordDistance:
index2 = pos2[j]
min_distance = min(min_distance, abs(index1 - index2))
# Move the pointer pointing to the smaller index
if index1 < index2:
i += 1
else:

View File

@@ -67,7 +67,5 @@ class Solution:
j += 1
return min_distance
# @leet end

View File

@@ -52,4 +52,3 @@ class Solution:
# @leet end

View File

@@ -0,0 +1,50 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
k = 0
for i in range(len(nums)):
if nums[i] != nums[k]:
k += 1
nums[k] = nums[i]
return k + 1
# @leet end

View File

@@ -0,0 +1,47 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
k = 0
for i in range(len(nums)):
if nums[i] != val:
nums[k] = nums[i]
k += 1
return k
# @leet end

View File

@@ -0,0 +1,31 @@
// @leet start
/**
* Encodes a list of strings to a single string.
*
* @param {string[]} strs
* @return {string}
*/
var key = '$#@C$(){(($[]%^[$()${#${(}'
var encode = function(strs) {
if (strs.length === 0) return [];
const encoded = strs.join(key);
return encoded;
};
/**
* Decodes a single string to a list of strings.
*
* @param {string} s
* @return {string[]}
*/
var decode = function(str) {
if (Array.isArray(str)) return str;
let decoded = str.split(key);
return decoded;
};
/**
* Your functions will be called as such:
* decode(encode(strs));
*/
// @leet end

View File

@@ -50,4 +50,3 @@ class Solution:
# @leet end;

View File

@@ -57,6 +57,4 @@ class Solution:
return True
# @leet end

View File

@@ -0,0 +1,30 @@
// @leet start
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
const setOfNumbers = {};
for (const num of nums) {
if (!setOfNumbers[num]) {
setOfNumbers[num] = 1;
} else {
setOfNumbers[num] = setOfNumbers[num] + 1;
}
}
const values = Object.values(setOfNumbers);
const keys = Object.keys(setOfNumbers);
let newArray = [];
for (let i = 0; i < keys.length; i++) {
newArray.push([keys[i], values[i]]);
}
newArray = newArray.sort((a, b) => {
return b[1] - a[1];
});
newArray = newArray.slice(0, k);
newArray = newArray.map((a) => Number(a[0]));
return newArray;
};
// @leet end

View File

@@ -0,0 +1,59 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
class Solution:
def generateTag(self, caption: str) -> str:
words = caption.strip().split(" ")
result = []
for i in range(len(words)):
clean = re.sub(r"[^a-zA-Z]", "", words[i])
if not clean:
continue
if i == 0:
result.append(clean.lower())
else:
result.append(clean[0].upper() + clean[1:].lower())
return ("#" + "".join(result))[:100]
# @leet end

View File

@@ -0,0 +1,74 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
class Solution:
def specialTriplets(self, nums: List[int]) -> int:
j = 0
count_left = {}
count_right = {}
total = 0
for i in nums:
if i not in count_right:
count_right[i] = 1
else:
count_right[i] += 1
while j < len(nums) - 1:
count_right[nums[j]] -= 1
target = nums[j] * 2
left_count = count_left.get(target, 0)
right_count = count_right.get(target, 0)
total += left_count * right_count
# Update count_left with current value
if nums[j] not in count_left:
count_left[nums[j]] = 1
else:
count_left[nums[j]] += 1
j += 1
return total % (10**9 + 7)
# @leet end

View File

@@ -0,0 +1,18 @@
// @leet start
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
if (strs.length < 2) return [strs];
const map = {};
for (const str of strs) {
const key = str.split("").sort().join("");
if (!map[key]) {
map[key] = [];
}
map[key].push(str);
}
return Object.values(map);
};
// @leet end

View File

@@ -0,0 +1,44 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
class Solution:
def lengthOfLastWord(self, s: str) -> int:
s = s.strip()
s = s.split(" ")
return len(s[-1])
# @leet end

View File

@@ -0,0 +1,19 @@
// @leet start
/**
* @param {number} n
* @return {number}
*/
const fibonacci = (element, cache = []) => {
if (element === 0) return 0;
if (element === 1) return 1;
if (cache[element]) return cache[element];
cache[element] = fibonacci(element - 2, cache) + fibonacci(element - 1, cache);
return cache[element];
}
var climbStairs = function(n) {
const array = fibonacci(n + 1)
return array
};
// @leet end

View File

@@ -73,7 +73,5 @@ class Solution:
isValid = bin_search(0, len(nums) - 1)
return isValid != -1
# @leet end

View File

@@ -0,0 +1,57 @@
# @leet imports start
from string import *
from re import *
from datetime import *
from collections import *
from heapq import *
from bisect import *
from copy import *
from math import *
from random import *
from statistics import *
from itertools import *
from functools import *
from operator import *
from io import *
from sys import *
from json import *
from builtins import *
import string
import re
import datetime
import collections
import heapq
import bisect
import copy
import math
import random
import statistics
import itertools
import functools
import operator
import io
import sys
import json
from typing import *
# @leet imports end
# @leet start
class Solution:
def anagramMappings(self, nums1: List[int], nums2: List[int]) -> List[int]:
numsList = {}
j = 0
for i in nums2:
if i not in numsList:
numsList[i] = j
j += 1
arr = []
for i in nums1:
arr.append(numsList[i])
return arr
# @leet end