You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+112-2
Original file line number
Diff line number
Diff line change
@@ -387,8 +387,6 @@ escaped_string = "He said, \"Hello, World!\"" # Use backslash to escape quotes
387
387
# String Methods like upp
388
388
upper_case = my_string.upper() # upper_case is "HELLO, WORLD!"
389
389
lower_case = my_string.lower() # lower_case is "hello, world!"
390
-
391
-
392
390
```
393
391
394
392
## Advanced Data Structures
@@ -419,6 +417,14 @@ sorted_list.add(2) # sorted_list is now [1, 2, 3, 4]
419
417
sorted_list = SortedList([1, 2, 3, 4])
420
418
sorted_list.remove(2) # sorted_list is now [1, 3, 4]
421
419
420
+
# find and remove multiple or list
421
+
sorted_list.discard(2)
422
+
sorted_list.index(3) # returns the index of 3 in the list (2 in this case)
423
+
# union, intersection, difference
424
+
425
+
union = sorted_list | sorted_list2 # union is [1, 2, 3, 4, 5]
426
+
intersection = sorted_list & sorted_list2 # intersection is [3]
427
+
difference = sorted_list - sorted_list2 # difference is [1, 2]
422
428
423
429
```
424
430
@@ -652,6 +658,110 @@ class DoublyLinkedList:
652
658
653
659
### Dynamic Programming
654
660
661
+
*Common patterns*
662
+
1. Fibonacci Pattern: Climbing Stairs
663
+
664
+
*Identification*:
665
+
If a problem asks for the number of ways to reach a target (like climbing stairs), or combinations involving two choices (1 or 2 steps), it often follows a Fibonacci pattern.
666
+
667
+
*Template*:
668
+
```python
669
+
defclimb_stairs(n):
670
+
if n <=2: # Base case
671
+
return n
672
+
return climb_stairs(n -1) + climb_stairs(n -2)
673
+
674
+
```
675
+
676
+
2. Knapsack Pattern: 0/1 Knapsack
677
+
- Identification: If a problem involves making decisions to maximize or minimize a value with given constraints (like capacity), it follows the Knapsack pattern.
Given an m x n grid filled with non-negative numbers, find a path from the top left to the bottom right which minimizes the sum of all numbers along its path. You can only move either down or right at any point in time.
691
+
```python
692
+
defdp_recursive(parameters, memo):
693
+
# Base case check
694
+
if base_condition:
695
+
return base_result
696
+
697
+
# Check memo dictionary for precomputed results
698
+
if parameters in memo:
699
+
return memo[parameters]
700
+
701
+
# Compute result recursively
702
+
result = recursive_logic(parameters)
703
+
704
+
# Store result in memo dictionary
705
+
memo[parameters] = result
706
+
707
+
return result
708
+
709
+
defsolve_problem():
710
+
# Initialize memo dictionary
711
+
memo = {}
712
+
# Call recursive function with initial parameters
713
+
return dp_recursive(initial_parameters, memo)
714
+
715
+
```
716
+
4. Given two strings text1 and text2, find the length of their longest common subsequence.
717
+
718
+
- Identification: If the problem involves comparing two sequences to find the longest or shortest subsequence or common pattern, it usually follows the LCS pattern.
719
+
- Template:
720
+
```python
721
+
deflcs_template(text1, text2):
722
+
m, n =len(text1), len(text2)
723
+
dp = [[0] * (n +1) for _ inrange(m +1)]
724
+
for i inrange(1, m +1):
725
+
for j inrange(1, n +1):
726
+
if text1[i -1] == text2[j -1]:
727
+
dp[i][j] = dp[i -1][j -1] +1
728
+
else:
729
+
dp[i][j] =max(dp[i -1][j], dp[i][j -1])
730
+
return dp[m][n]
731
+
732
+
```
733
+
734
+
5. Partitioning Problems Pattern: Word Break Problem
735
+
- Given a string s and a dictionary of words wordDict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
736
+
- Identification: When the problem requires partitioning a sequence into valid parts based on a set of rules (like dictionary words or palindromes).
0 commit comments