Skip to content

Commit 4e1ca1d

Browse files
authored
Added tasks 416-1143
1 parent 74b6ff7 commit 4e1ca1d

File tree

21 files changed

+584
-11
lines changed

21 files changed

+584
-11
lines changed

README.md

Lines changed: 34 additions & 11 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Level_2_Day_13_Dynamic_Programming
2+
# #Big_O_Time_O(n*sums)_Space_O(n*sums) #2024_06_07_Time_550_ms_(64.93%)_Space_16.6_MB_(92.89%)
3+
4+
class Solution:
5+
def canPartition(self, nums: List[int]) -> bool:
6+
sums = sum(nums)
7+
if sums % 2 == 1:
8+
return False
9+
sums //= 2
10+
dp = [False] * (sums + 1)
11+
dp[0] = True
12+
for num in nums:
13+
for i in range(sums, num - 1, -1):
14+
dp[i] = dp[i] or dp[i - num]
15+
return dp[sums]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
416\. Partition Equal Subset Sum
2+
3+
Medium
4+
5+
Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,5,11,5]
10+
11+
**Output:** true
12+
13+
**Explanation:** The array can be partitioned as [1, 5, 5] and [11].
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,2,3,5]
18+
19+
**Output:** false
20+
21+
**Explanation:** The array cannot be partitioned into equal sum subsets.
22+
23+
**Constraints:**
24+
25+
* `1 <= nums.length <= 200`
26+
* `1 <= nums[i] <= 100`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# #Medium #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree #Big_O_Time_O(n)_Space_O(n)
2+
# #2024_06_07_Time_264_ms_(30.25%)_Space_16.8_MB_(98.67%)
3+
4+
# Definition for a binary tree node.
5+
# class TreeNode:
6+
# def __init__(self, val=0, left=None, right=None):
7+
# self.val = val
8+
# self.left = left
9+
# self.right = right
10+
class Solution:
11+
def __init__(self):
12+
self.count = 0
13+
14+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> int:
15+
if root is None:
16+
return 0
17+
self.helper(root, targetSum, 0)
18+
self.pathSum(root.left, targetSum)
19+
self.pathSum(root.right, targetSum)
20+
return self.count
21+
22+
def helper(self, node: TreeNode, targetSum: int, currSum: int) -> None:
23+
if node is None:
24+
return
25+
currSum += node.val
26+
if targetSum == currSum:
27+
self.count += 1
28+
if node.left is not None:
29+
self.helper(node.left, targetSum, currSum)
30+
if node.right is not None:
31+
self.helper(node.right, targetSum, currSum)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
437\. Path Sum III
2+
3+
Medium
4+
5+
Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`.
6+
7+
The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)
12+
13+
**Input:** root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8
14+
15+
**Output:** 3
16+
17+
**Explanation:** The paths that sum to 8 are shown.
18+
19+
**Example 2:**
20+
21+
**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
22+
23+
**Output:** 3
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range `[0, 1000]`.
28+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
29+
* `-1000 <= targetSum <= 1000`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# #Medium #Top_100_Liked_Questions #String #Hash_Table #Sliding_Window
2+
# #Algorithm_II_Day_5_Sliding_Window #Programming_Skills_II_Day_12
3+
# #Level_1_Day_12_Sliding_Window/Two_Pointer #Big_O_Time_O(n+m)_Space_O(1)
4+
# #2024_06_07_Time_100_ms_(50.22%)_Space_17.6_MB_(97.85%)
5+
6+
from collections import defaultdict
7+
8+
class Solution:
9+
def findAnagrams(self, s: str, p: str) -> List[int]:
10+
map = [0] * 26
11+
for char in p:
12+
map[ord(char) - ord('a')] += 1
13+
14+
res = []
15+
i, j = 0, 0
16+
17+
while i < len(s):
18+
idx = ord(s[i]) - ord('a')
19+
# add the new character
20+
map[idx] -= 1
21+
# if the length is greater than windows length, pop the left character in the window
22+
if i >= len(p):
23+
map[ord(s[j]) - ord('a')] += 1
24+
j += 1
25+
26+
finish = True
27+
for k in range(26):
28+
# if it is not an anagram of string p
29+
if map[k] != 0:
30+
finish = False
31+
break
32+
33+
if i >= len(p) - 1 and finish:
34+
res.append(j)
35+
36+
i += 1
37+
38+
return res
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
438\. Find All Anagrams in a String
2+
3+
Medium
4+
5+
Given two strings `s` and `p`, return _an array of all the start indices of_ `p`_'s anagrams in_ `s`. You may return the answer in **any order**.
6+
7+
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "cbaebabacd", p = "abc"
12+
13+
**Output:** [0,6]
14+
15+
**Explanation:**
16+
17+
The substring with start index = 0 is "cba", which is an anagram of "abc".
18+
The substring with start index = 6 is "bac", which is an anagram of "abc".
19+
20+
**Example 2:**
21+
22+
**Input:** s = "abab", p = "ab"
23+
24+
**Output:** [0,1,2]
25+
26+
**Explanation:**
27+
28+
The substring with start index = 0 is "ab", which is an anagram of "ab".
29+
The substring with start index = 1 is "ba", which is an anagram of "ab".
30+
The substring with start index = 2 is "ab", which is an anagram of "ab".
31+
32+
**Constraints:**
33+
34+
* <code>1 <= s.length, p.length <= 3 * 10<sup>4</sup></code>
35+
* `s` and `p` consist of lowercase English letters.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# #Medium #Array #Dynamic_Programming #Backtracking #Big_O_Time_O(n*(sum+s))_Space_O(n*(sum+s))
2+
# #2024_06_07_Time_105_ms_(86.96%)_Space_16.7_MB_(80.93%)
3+
4+
class Solution:
5+
def findTargetSumWays(self, nums: List[int], target: int) -> int:
6+
total_sum = sum(nums)
7+
s = abs(target)
8+
9+
# Invalid s, just return 0
10+
if s > total_sum or (total_sum + s) % 2 != 0:
11+
return 0
12+
13+
target = (total_sum + s) // 2
14+
dp = [[0] * (len(nums) + 1) for _ in range(target + 1)]
15+
dp[0][0] = 1
16+
17+
# empty knapsack must be processed specially
18+
for i in range(len(nums)):
19+
if nums[i] == 0:
20+
dp[0][i + 1] = dp[0][i] * 2
21+
else:
22+
dp[0][i + 1] = dp[0][i]
23+
24+
for i in range(1, target + 1):
25+
for j in range(len(nums)):
26+
dp[i][j + 1] += dp[i][j]
27+
if nums[j] <= i:
28+
dp[i][j + 1] += dp[i - nums[j]][j]
29+
30+
return dp[target][len(nums)]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
494\. Target Sum
2+
3+
Medium
4+
5+
You are given an integer array `nums` and an integer `target`.
6+
7+
You want to build an **expression** out of nums by adding one of the symbols `'+'` and `'-'` before each integer in nums and then concatenate all the integers.
8+
9+
* For example, if `nums = [2, 1]`, you can add a `'+'` before `2` and a `'-'` before `1` and concatenate them to build the expression `"+2-1"`.
10+
11+
Return the number of different **expressions** that you can build, which evaluates to `target`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,1,1,1,1], target = 3
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
There are 5 ways to assign symbols to make the sum of nums be target 3.
22+
-1 + 1 + 1 + 1 + 1 = 3
23+
+1 - 1 + 1 + 1 + 1 = 3
24+
+1 + 1 - 1 + 1 + 1 = 3
25+
+1 + 1 + 1 - 1 + 1 = 3
26+
+1 + 1 + 1 + 1 - 1 = 3
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [1], target = 1
31+
32+
**Output:** 1
33+
34+
**Constraints:**
35+
36+
* `1 <= nums.length <= 20`
37+
* `0 <= nums[i] <= 1000`
38+
* `0 <= sum(nums[i]) <= 1000`
39+
* `-1000 <= target <= 1000`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# #Easy #Top_100_Liked_Questions #Depth_First_Search #Tree #Binary_Tree #Level_2_Day_7_Tree
2+
# #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n)
3+
# #2024_06_07_Time_51_ms_(36.84%)_Space_19.2_MB_(86.39%)
4+
5+
# Definition for a binary tree node.
6+
# class TreeNode:
7+
# def __init__(self, val=0, left=None, right=None):
8+
# self.val = val
9+
# self.left = left
10+
# self.right = right
11+
class Solution:
12+
def __init__(self):
13+
self.diameter = 0
14+
15+
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
16+
self.diameter = 0
17+
self.diameterOfBinaryTreeUtil(root)
18+
return self.diameter
19+
20+
def diameterOfBinaryTreeUtil(self, root: TreeNode) -> int:
21+
if root is None:
22+
return 0
23+
leftLength = 1 + self.diameterOfBinaryTreeUtil(root.left) if root.left else 0
24+
rightLength = 1 + self.diameterOfBinaryTreeUtil(root.right) if root.right else 0
25+
self.diameter = max(self.diameter, leftLength + rightLength)
26+
return max(leftLength, rightLength)

0 commit comments

Comments
 (0)