Skip to content

Commit 22130fa

Browse files
committed
Add more DS
1 parent 4358ff9 commit 22130fa

File tree

1 file changed

+112
-2
lines changed

1 file changed

+112
-2
lines changed

Diff for: README.md

+112-2
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,6 @@ escaped_string = "He said, \"Hello, World!\"" # Use backslash to escape quotes
387387
# String Methods like upp
388388
upper_case = my_string.upper() # upper_case is "HELLO, WORLD!"
389389
lower_case = my_string.lower() # lower_case is "hello, world!"
390-
391-
392390
```
393391

394392
## Advanced Data Structures
@@ -419,6 +417,14 @@ sorted_list.add(2) # sorted_list is now [1, 2, 3, 4]
419417
sorted_list = SortedList([1, 2, 3, 4])
420418
sorted_list.remove(2) # sorted_list is now [1, 3, 4]
421419

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]
422428

423429
```
424430

@@ -652,6 +658,110 @@ class DoublyLinkedList:
652658

653659
### Dynamic Programming
654660

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+
def climb_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.
678+
- Template:
679+
680+
```python
681+
def knapsack_recursive(wt, val, W, n):
682+
if n == 0 or W == 0:
683+
return 0
684+
if wt[n-1] > W:
685+
return knapsack_recursive(wt, val, W, n-1)
686+
return max(val[n-1] + knapsack_recursive(wt, val, W-wt[n-1], n-1), knapsack_recursive(wt, val, W, n-1))
687+
```
688+
3. Minimum Path Sum
689+
690+
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+
def dp_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+
def solve_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+
def lcs_template(text1, text2):
722+
m, n = len(text1), len(text2)
723+
dp = [[0] * (n + 1) for _ in range(m + 1)]
724+
for i in range(1, m + 1):
725+
for j in range(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).
737+
- Template:
738+
```python
739+
def word_break_recursive(s, wordDict, start, memo):
740+
# Base case: reached the end of the string
741+
if start == len(s):
742+
return True
743+
744+
# Check if result is already computed
745+
if start in memo:
746+
return memo[start]
747+
748+
# Try to partition the string
749+
for end in range(start + 1, len(s) + 1):
750+
if s[start:end] in wordDict and word_break_recursive(s, wordDict, end, memo):
751+
memo[start] = True
752+
return True
753+
754+
# If no valid partition is found
755+
memo[start] = False
756+
return False
757+
758+
def word_break(s, wordDict):
759+
memo = {}
760+
return word_break_recursive(s, wordDict, 0, memo)
761+
```
762+
763+
764+
655765
### Graph Algorithms
656766

657767
## Interview Tips

0 commit comments

Comments
 (0)