Skip to content

Commit 8df7ec4

Browse files
committed
Add sliding window to hash table
1 parent 25e19c0 commit 8df7ec4

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-6
lines changed

src/code/javascript/array/sliding_window.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const fn = (arr) => {
2+
let window = 0
23
let ans = 0
3-
let curr = 0
44
let left = 0
55

66
for (let right = 0; right < arr.length; right++) {
7-
// TODO: add arr[right] to curr
7+
// TODO: add arr[right] to window
88

99
while (WINDOW_CONDITION_BROKEN) {
10-
// TODO: remove arr[left] from curr
10+
// TODO: remove arr[left] from window
1111
left++
1212
}
1313

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function fn(arr) {
2+
let window = new Set()
3+
let ans = 0
4+
let left = 0
5+
6+
for (let right = 0; right < arr.length; right++) {
7+
// TODO: add arr[right] to window
8+
9+
while (WINDOW_CONDITION_BROKEN) {
10+
// TODO: remove arr[left] from window
11+
left += 1
12+
}
13+
14+
// TODO: update ans
15+
}
16+
17+
return ans
18+
}

src/code/python/array/sliding_window.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
def fn(arr):
22
n = len(arr)
3+
window = 0
34
left = 0
4-
curr = 0
55
ans = 0
66

77
for right in range(n):
8-
# TODO: add arr[right] to curr
8+
# TODO: add arr[right] to window
99

1010
while WINDOW_CONDITION_BROKEN:
11-
# TODO: remove arr[left] from curr
11+
# TODO: remove arr[left] from window
1212
left += 1
1313

1414
# TODO: update ans
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def fn(arr):
2+
window = set()
3+
ans = 0
4+
left = 0
5+
6+
for right, ELEMENT in enumerate(arr):
7+
# TODO: add arr[right] to window
8+
9+
while WINDOW_CONDITION_BROKEN:
10+
# TODO: remove arr[left] from window
11+
left += 1
12+
13+
# TODO: update ans
14+
15+
return ans

src/components/Sidebar/SidebarLinks.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default function SidebarLinks() {
1919
</Accordion>
2020
<Accordion title="Hash Map">
2121
<LinkWithTooltip href="#hashmap-find-number-of-subarrays" description="find number of subarrays that fit an exact criteria" />
22+
<LinkWithTooltip href="#hashmap-sliding-window" description="sliding window" />
2223
</Accordion>
2324
<Accordion title="Linked List">
2425
<LinkWithTooltip href="#linkedlist-fast-and-slow-pointer" description="fast and slow pointer" />

src/sections/HashMap/index.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import styles from '@sections/section.module.sass'
33
import Tabs from '@components/Tabs'
44

55
import FindNumberOfSubarraysPY from '@code/python/hash_map/find_number_of_subarrays.py?raw'
6+
import SlidingWindowPY from '@code/python/hash_map/sliding_window.py?raw'
7+
8+
69
import FindNumberOfSubarraysJS from '@code/javascript/hash_map/find_number_of_subarrays.js?raw'
10+
import SlidingWindowJS from '@code/javascript/hash_map/sliding_window.js?raw'
711

812

913
export default function HashMap() {
@@ -16,6 +20,12 @@ export default function HashMap() {
1620
<Tabs.Tab code={FindNumberOfSubarraysJS} language="javascript" />
1721
</Tabs>
1822
</section>
23+
<section id="hashmap-sliding-window">
24+
<Tabs title="sliding window">
25+
<Tabs.Tab code={SlidingWindowPY} language="python" />
26+
<Tabs.Tab code={SlidingWindowJS} language="javascript" />
27+
</Tabs>
28+
</section>
1929
</div>
2030
)
2131
}

0 commit comments

Comments
 (0)