Skip to content

Commit 0274055

Browse files
committed
Add new solutions, e.g. sliding window
1 parent 1babe00 commit 0274055

File tree

11 files changed

+371
-45
lines changed

11 files changed

+371
-45
lines changed

README.md

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,46 @@ Leetcode practice in Python
77
|-----|------| ---------------------------------------------------------------- | -------- |------------------- |-----------------------------------------------------------|
88
| 1 | 1 | [Two Sum] | [Easy] | | [Two Sum Solution] |
99
| 2 | 3 | [Longest Substring Without Repeating Characters] | [Medium] | | [Longest Substring Without Repeating Characters Solution] |
10-
| 3 | 20 | [Valid Parentheses] | [Easy] | | [Valid Parentheses Solution] |
11-
| 4 | 21 | [Merge Two Sorted Lists] | [Easy] | | [Merge Two Sorted Lists Solution] |
12-
| 5 | 26 | [Remove Duplicates from Sorted Array] | [Easy] | | [Remove Duplicates from Sorted Array Solution] |
13-
| 6 | 49 | [Group Anagrams] | [Medium] | | [Group Anagrams Solution] |
14-
| 7 | 70 | [Climbing Stairs] | [Easy] | | [Climbing Stairs Solution] |
15-
| 8 | 98 | [Validate Binary Search Tree] | [Medium] | | [Validate Binary Search Tree Solution] |
16-
| 9 | 100 | [Same Tree] | [Easy] | | [Same Tree Solution] |
17-
| 10 | 102 | [Binary Tree Level Order Traversal] | [Medium] | | [Binary Tree Level Order Traversal Solution] |
18-
| 11 | 104 | [Maximum Depth of Binary Tree] | [Easy] | | [Maximum Depth of Binary Tree Solution] |
19-
| 12 | 122 | [Best Time to Buy and Sell Stock II] | [Easy] | | [Best Time to Buy and Sell Stock II Solution] |
20-
| 13 | 141 | [Linked List Cycle] | [Easy] | | [Linked List Cycle Solution] |
21-
| 14 | 146 | [LRU Cache] | [Medium] | | [LRU Cache Solution] |
22-
| 15 | 155 | [Min Stack] | [Medium] | | [Min Stack Solution] |
23-
| 16 | 189 | [Rotate Array] | [Medium] | | [Rotate Array Solution] |
24-
| 17 | 198 | [House Robber] | [Medium] | | [House Robber Solution] |
25-
| 18 | 200 | [Number of Islands] | [Medium] | | [Number of Islands Solution] |
26-
| 19 | 206 | [Reverse Linked List] | [Easy] | | [Reverse Linked List Solution] |
27-
| 20 | 207 | [Course Schedule] | [Medium] | | [Course Schedule Solution] |
28-
| 21 | 213 | [House Robber II] | [Medium] | | [House Robber II Solution] |
29-
| 22 | 226 | [Invert Binary Tree] | [Easy] | | [Invert Binary Tree Solution] |
30-
| 23 | 230 | [Kth Smallest Element in a BST] | [Medium] | | [Kth Smallest Element in a BST Solution] |
31-
| 24 | 236 | [Lowest Common Ancestor of a Binary Tree] | [Medium] | | [Lowest Common Ancestor of a Binary Tree Solution] |
32-
| 25 | 238 | [Product of Array Except Self] | [Medium] | | [Product of Array Except Self Solution] |
33-
| 26 | 242 | [Valid Anagram] | [Easy] | | [Valid Anagram Solution] |
34-
| 27 | 509 | [Fibonacci Number] | [Easy] | | [Fibonacci Number Solution] |
35-
| 28 | 543 | [Diameter of Binary Tree] | [Easy] | | [Diameter of Binary Tree Solution] |
36-
| 29 | 560 | [Subarray Sum Equals K] | [Medium] | | [Subarray Sum Equals K Solution] |
37-
| 30 | 740 | [Delete and Earn] | [Medium] | | [Delete and Earn Solution] |
38-
| 31 | 746 | [Min Cost Climbing Stairs] | [Easy] | | [Min Cost Climbing Stairs Solution] |
39-
| 32 | 1137 | [Nth Tribonacci Number] | [Easy] | | [Nth Tribonacci Number Solution] |
40-
| 33 | 1473 | [Final Price With a Special Discount in a Shop] | [Easy] | | [Final Price With a Special Discount in a Shop Solution] |
41-
| 34 | 1060 | [Missing Element in Sorted Array] | [Medium] | | [Missing Element in Sorted Array Solution] |
10+
| 3 | 15 | [3 Sum] | [Medium] | | [3 Sum Solution] |
11+
| 4 | 20 | [Valid Parentheses] | [Easy] | | [Valid Parentheses Solution] |
12+
| 5 | 21 | [Merge Two Sorted Lists] | [Easy] | | [Merge Two Sorted Lists Solution] |
13+
| 6 | 26 | [Remove Duplicates from Sorted Array] | [Easy] | | [Remove Duplicates from Sorted Array Solution] |
14+
| 7 | 49 | [Group Anagrams] | [Medium] | | [Group Anagrams Solution] |
15+
| 8 | 70 | [Climbing Stairs] | [Easy] | | [Climbing Stairs Solution] |
16+
| 9 | 98 | [Validate Binary Search Tree] | [Medium] | | [Validate Binary Search Tree Solution] |
17+
| 10 | 100 | [Same Tree] | [Easy] | | [Same Tree Solution] |
18+
| 11 | 102 | [Binary Tree Level Order Traversal] | [Medium] | | [Binary Tree Level Order Traversal Solution] |
19+
| 12 | 104 | [Maximum Depth of Binary Tree] | [Easy] | | [Maximum Depth of Binary Tree Solution] |
20+
| 13 | 122 | [Best Time to Buy and Sell Stock II] | [Easy] | | [Best Time to Buy and Sell Stock II Solution] |
21+
| 14 | 141 | [Linked List Cycle] | [Easy] | | [Linked List Cycle Solution] |
22+
| 15 | 146 | [LRU Cache] | [Medium] | | [LRU Cache Solution] |
23+
| 16 | 155 | [Min Stack] | [Medium] | | [Min Stack Solution] |
24+
| 17 | 159 | [Longest Substring with At Most Two Distinct Characters] | [Medium] | | [Longest Substring with At Most Two Distinct Characters Solution] |
25+
| 18 | 189 | [Rotate Array] | [Medium] | | [Rotate Array Solution] |
26+
| 19 | 198 | [House Robber] | [Medium] | | [House Robber Solution] |
27+
| 20 | 200 | [Number of Islands] | [Medium] | | [Number of Islands Solution] |
28+
| 21 | 206 | [Reverse Linked List] | [Easy] | | [Reverse Linked List Solution] |
29+
| 22 | 207 | [Course Schedule] | [Medium] | | [Course Schedule Solution] |
30+
| 23 | 209 | [Minimum Size Subarray Sum] | [Medium] | | [Minimum Size Subarray Sum Solution] |
31+
| 24 | 213 | [House Robber II] | [Medium] | | [House Robber II Solution] |
32+
| 25 | 226 | [Invert Binary Tree] | [Easy] | | [Invert Binary Tree Solution] |
33+
| 26 | 230 | [Kth Smallest Element in a BST] | [Medium] | | [Kth Smallest Element in a BST Solution] |
34+
| 27 | 236 | [Lowest Common Ancestor of a Binary Tree] | [Medium] | | [Lowest Common Ancestor of a Binary Tree Solution] |
35+
| 28 | 238 | [Product of Array Except Self] | [Medium] | | [Product of Array Except Self Solution] |
36+
| 29 | 242 | [Valid Anagram] | [Easy] | | [Valid Anagram Solution] |
37+
| 30 | 438 | [Find All Anagrams in a String] | [Medium] | | [Find All Anagrams in a String Solution] |
38+
| 31 | 487 | [Max Consecutive Ones II] | [Medium] | | [Max Consecutive Ones II Solution] |
39+
| 32 | 509 | [Fibonacci Number] | [Easy] | | [Fibonacci Number Solution] |
40+
| 33 | 543 | [Diameter of Binary Tree] | [Easy] | | [Diameter of Binary Tree Solution] |
41+
| 34 | 560 | [Subarray Sum Equals K] | [Medium] | | [Subarray Sum Equals K Solution] |
42+
| 35 | 567 | [Permutation in String] | [Medium] | | [Permutation in String Solution] |
43+
| 36 | 643 | [Maximum Average Subarray I] | [Easy] | | [Maximum Average Subarray I Solution] |
44+
| 37 | 740 | [Delete and Earn] | [Medium] | | [Delete and Earn Solution] |
45+
| 38 | 746 | [Min Cost Climbing Stairs] | [Easy] | | [Min Cost Climbing Stairs Solution] |
46+
| 39 | 1137 | [Nth Tribonacci Number] | [Easy] | | [Nth Tribonacci Number Solution] |
47+
| 40 | 1473 | [Final Price With a Special Discount in a Shop] | [Easy] | | [Final Price With a Special Discount in a Shop Solution] |
48+
| 41 | 1695 | [Maximum Erasure Value] | [Medium] | | [Maximum Erasure Value Solution] |
49+
| 42 | 1060 | [Missing Element in Sorted Array] | [Medium] | | [Missing Element in Sorted Array Solution] |
4250

4351
[Remove Duplicates from Sorted Array]: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
4452
[Remove Duplicates from Sorted Array Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/easy/remove_duplicates_from_sorted_array/solution.py
@@ -108,6 +116,22 @@ Leetcode practice in Python
108116
[Nth Tribonacci Number Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/easy/nth_tribonacci_number/solution.py
109117
[Final Price With a Special Discount in a Shop]: https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/
110118
[Final Price With a Special Discount in a Shop Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/easy/final_price_with_a_special_discount_in_a_shop/solution.py
119+
[Maximum Erasure Value]: https://leetcode.com/problems/maximum-erasure-value/
120+
[Maximum Erasure Value Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/medium/maximum_erasure_value/solution.py
121+
[3 Sum]: https://leetcode.com/problems/3sum/
122+
[3 Sum Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/medium/3_sum/solution.py
123+
[Longest Substring with At Most Two Distinct Characters]: https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/
124+
[Longest Substring with At Most Two Distinct Characters Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/medium/longest_substring_with_at_most_two_distinct_characters/solution.py
125+
[Minimum Size Subarray Sum]: https://leetcode.com/problems/minimum-size-subarray-sum/
126+
[Minimum Size Subarray Sum Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/medium/minimum_size_subarray_sum/solution.py
127+
[Find All Anagrams in a String]: https://leetcode.com/problems/find-all-anagrams-in-a-string/
128+
[Find All Anagrams in a String Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/medium/find_all_anagrams_in_a_string/solution.py
129+
[Max Consecutive Ones II]: https://leetcode.com/problems/max-consecutive-ones-ii/
130+
[Max Consecutive Ones II Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/medium/max_consecutive_onesII/solution.py
131+
[Permutation in String]: https://leetcode.com/problems/permutation-in-string/
132+
[Permutation in String Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/medium/premutation_in_string/solution.py
133+
[Maximum Average Subarray I]: https://leetcode.com/problems/maximum-average-subarray-i/
134+
[Maximum Average Subarray I Solution]: https://github.com/robinali34/leetcode_python/blob/main/src/easy/maximum_average_subarrayI/solution.py
111135

112136
[Easy]: https://github.com/robinali34/leetcode_python/tree/main/src/easy
113137
[Medium]: https://github.com/robinali34/leetcode_python/tree/main/src/medium

src/easy/maximum_average_subarrayI/__init__.py

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#! /usr/bin/python3
2+
# -*- coding:utf8 -*-
3+
4+
# Copyright 2025 Robina Li. BSD 3-Clause License All Rights Reserved.
5+
# @file : solution.py
6+
# @desc : Solution for Leetcode
7+
# Reference : https://leetcode.com/problems/maximum-average-subarray-i/
8+
9+
from typing import List
10+
from functools import cache
11+
import math
12+
13+
class Solution(object):
14+
def findMaxAverage(self, nums: List[int], k: int) -> float:
15+
sum_, max_avg = 0, -math.inf
16+
17+
start = 0
18+
for end in range(len(nums)):
19+
sum_ += nums[end]
20+
if end - start + 1 == k:
21+
max_avg = max(max_avg, sum_/k)
22+
if end >= k - 1:
23+
sum_ -= nums[start]
24+
start += 1
25+
return max_avg
26+
27+
if __name__ == '__main__':
28+
solution = Solution()
29+
30+
rtn = solution.findMaxAverage([1,12,-5,-6,50,3], 4)
31+
print(12.75000 == rtn)
32+
rtn = solution.findMaxAverage([5], 1)
33+
print(5.00000 == rtn)

src/medium/3_sum/solution.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#! /usr/bin/python3
2+
# -*- coding:utf8 -*-
3+
4+
# Copyright 2025 Robina Li. BSD 3-Clause License All Rights Reserved.
5+
# @file : solution.py
6+
# @desc : Solution for Leetcode
7+
# Reference : https://leetcode.com/problems/3sum/
8+
9+
from functools import cache
10+
from typing import List
11+
import math
12+
13+
class Solution(object):
14+
def threeSum(self, nums: List[int]) -> List[List[int]]:
15+
nums.sort()
16+
ans = []
17+
for i in range(len(nums)):
18+
if i > 0 and nums[i] == nums[i - 1]:
19+
continue
20+
j, k = i + 1, len(nums) - 1
21+
while j < k:
22+
total = nums[i] + nums[j] + nums[k]
23+
if total > 0:
24+
k -= 1
25+
elif total < 0:
26+
j+= 1
27+
else:
28+
ans.append([nums[i], nums[j], nums[k]])
29+
while j < k and nums[j] == nums[j + 1]:
30+
j += 1
31+
while j < k and nums[k] == nums[k - 1]:
32+
k -= 1
33+
j += 1
34+
k -= 1
35+
return ans
36+
37+
if __name__ == '__main__':
38+
solution = Solution()
39+
rtn = solution.threeSum([-1,0,1,2,-1,-4])
40+
print([[-1,-1,2],[-1,0,1]] == rtn)
41+
rtn = solution.threeSum([0,1,1])
42+
print([] == rtn)
43+
rtn = solution.threeSum([0,0,0])
44+
print([[0,0,0]] == rtn)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#! /usr/bin/python3
2+
# -*- coding:utf8 -*-
3+
4+
# Copyright 2025 Robina Li. BSD 3-Clause License All Rights Reserved.
5+
# @file : solution.py
6+
# @desc : Solution for Leetcode
7+
# Reference : https://leetcode.com/problems/find-all-anagrams-in-a-string/
8+
9+
from functools import cache
10+
from typing import List
11+
12+
class Solution(object):
13+
def findAnagrams(self, s: str, p: str) -> List[int]:
14+
res, hashmap = [], {}
15+
hashmap_p = {}
16+
for char in p:
17+
hashmap_p[char] = hashmap_p.get(char, 0) + 1
18+
start = 0
19+
for end in range(len(s)):
20+
hashmap[s[end]] = hashmap.get(s[end], 0) + 1
21+
if hashmap == hashmap_p:
22+
res.append(start)
23+
if end - start >= len(p) - 1:
24+
hashmap[s[start]] -= 1
25+
if hashmap[s[start]] == 0:
26+
del hashmap[s[start]]
27+
start += 1
28+
return res
29+
30+
if __name__ == '__main__':
31+
solution = Solution()
32+
rtn = solution.findAnagrams("cbaebabacd", "abc")
33+
print([0,6] == rtn)
34+
rtn = solution.findAnagrams("abab", "ab")
35+
print([0,1,2] == rtn)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#! /usr/bin/python3
2+
# -*- coding:utf8 -*-
3+
4+
# Copyright 2025 Robina Li. BSD 3-Clause License All Rights Reserved.
5+
# @file : solution.py
6+
# @desc : Solution for Leetcode
7+
# Reference : https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
8+
9+
from functools import cache
10+
from typing import List
11+
import math
12+
13+
class Solution(object):
14+
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
15+
max_len, hashmap = 0, {}
16+
start = 0
17+
for end in range(len(s)):
18+
hashmap[s[end]] = hashmap.get(s[end], 0) + 1
19+
if len(hashmap) <= 2:
20+
max_len = max(max_len, end-start+1)
21+
while len(hashmap) > 2:
22+
hashmap[s[start]] -= 1
23+
if hashmap[s[start]] == 0:
24+
del hashmap[s[start]]
25+
start += 1
26+
return max_len
27+
28+
if __name__ == '__main__':
29+
solution = Solution()
30+
rtn = solution.lengthOfLongestSubstringTwoDistinct("eceba")
31+
print(3 == rtn)
32+
rtn = solution.lengthOfLongestSubstringTwoDistinct("ccaabbb")
33+
print(5 == rtn)

0 commit comments

Comments
 (0)