Skip to content

Commit 7256932

Browse files
committed
refactor(algorithms, heaps): move to heap package
1 parent 2db74fa commit 7256932

4 files changed

Lines changed: 23 additions & 23 deletions

File tree

algorithms/heap/longest_happy_string/README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ strings, return any one of them. If no such string can be formed, return an empt
2222

2323
## Examples
2424

25-
![Example 1](./images/examples/longest_happy_string_example_1.png)
26-
![Example 2](./images/examples/longest_happy_string_example_2.png)
27-
![Example 3](./images/examples/longest_happy_string_example_3.png)
25+
![Example 1](images/examples/longest_happy_string_example_1.png)
26+
![Example 2](images/examples/longest_happy_string_example_2.png)
27+
![Example 3](images/examples/longest_happy_string_example_3.png)
2828

2929

3030
## Solution
@@ -55,21 +55,21 @@ Note: In Python, strings are immutable, so we construct the result as a list and
5555

5656
Let’s look at the following illustration to get a better understanding of the solution:
5757

58-
![Longest Happy String Solution 1](./images/solutions/longest_happy_string_solution_1.png)
59-
![Longest Happy String Solution 2](./images/solutions/longest_happy_string_solution_2.png)
60-
![Longest Happy String Solution 3](./images/solutions/longest_happy_string_solution_3.png)
61-
![Longest Happy String Solution 4](./images/solutions/longest_happy_string_solution_4.png)
62-
![Longest Happy String Solution 5](./images/solutions/longest_happy_string_solution_5.png)
63-
![Longest Happy String Solution 6](./images/solutions/longest_happy_string_solution_6.png)
64-
![Longest Happy String Solution 7](./images/solutions/longest_happy_string_solution_7.png)
65-
![Longest Happy String Solution 8](./images/solutions/longest_happy_string_solution_8.png)
66-
![Longest Happy String Solution 9](./images/solutions/longest_happy_string_solution_9.png)
67-
![Longest Happy String Solution 10](./images/solutions/longest_happy_string_solution_10.png)
68-
![Longest Happy String Solution 11](./images/solutions/longest_happy_string_solution_11.png)
69-
![Longest Happy String Solution 12](./images/solutions/longest_happy_string_solution_12.png)
70-
![Longest Happy String Solution 13](./images/solutions/longest_happy_string_solution_13.png)
71-
![Longest Happy String Solution 14](./images/solutions/longest_happy_string_solution_14.png)
72-
![Longest Happy String Solution 15](./images/solutions/longest_happy_string_solution_15.png)
58+
![Longest Happy String Solution 1](images/solutions/longest_happy_string_solution_1.png)
59+
![Longest Happy String Solution 2](images/solutions/longest_happy_string_solution_2.png)
60+
![Longest Happy String Solution 3](images/solutions/longest_happy_string_solution_3.png)
61+
![Longest Happy String Solution 4](images/solutions/longest_happy_string_solution_4.png)
62+
![Longest Happy String Solution 5](images/solutions/longest_happy_string_solution_5.png)
63+
![Longest Happy String Solution 6](images/solutions/longest_happy_string_solution_6.png)
64+
![Longest Happy String Solution 7](images/solutions/longest_happy_string_solution_7.png)
65+
![Longest Happy String Solution 8](images/solutions/longest_happy_string_solution_8.png)
66+
![Longest Happy String Solution 9](images/solutions/longest_happy_string_solution_9.png)
67+
![Longest Happy String Solution 10](images/solutions/longest_happy_string_solution_10.png)
68+
![Longest Happy String Solution 11](images/solutions/longest_happy_string_solution_11.png)
69+
![Longest Happy String Solution 12](images/solutions/longest_happy_string_solution_12.png)
70+
![Longest Happy String Solution 13](images/solutions/longest_happy_string_solution_13.png)
71+
![Longest Happy String Solution 14](images/solutions/longest_happy_string_solution_14.png)
72+
![Longest Happy String Solution 15](images/solutions/longest_happy_string_solution_15.png)
7373

7474
### Time complexity
7575

algorithms/heap/longest_happy_string/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def longest_diverse_string(a: int, b: int, c: int) -> str:
2020
# Process the heap until it's empty or no valid character can be added
2121
while max_heap:
2222
# Pop the character with the highest remaining frequency
23-
count, character = heappop(max_heap)
24-
count = -count # Convert back to positive
23+
negative_count, character = heappop(max_heap)
24+
count = -negative_count # Convert back to positive
2525

2626
# Check if adding this character violates the "no three consecutive" rule
2727
if len(result) >= 2 and result[-1] == character and result[-2] == character:

algorithms/heap/longest_happy_string/test_longest_happy_string.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from parameterized import parameterized
3-
from pystrings.longest_happy_string import longest_diverse_string
3+
from algorithms.heap.longest_happy_string import longest_diverse_string
44

55

66
class LongestHappyStringTestCase(unittest.TestCase):

algorithms/heap/topklargest/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def k_largest(nums: List[int], k: int = 3) -> List[int]:
1515
# input validation to ensure we don't get unexpected results
1616
if not nums or k <= 0:
1717
return []
18-
18+
1919
# Adjust k if it exceeds the length of nums
2020
k = min(k, len(nums))
21-
21+
2222
# create a minimum heap with the first k elements
2323
min_heap = nums[:k]
2424
heapq.heapify(min_heap)

0 commit comments

Comments
 (0)