Skip to content

Commit 7ab04e5

Browse files
authored
Merge pull request #1593 from ivanpenaloza/march31
adding algo
2 parents a98ccb5 + 879e1af commit 7ab04e5

5 files changed

Lines changed: 189 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
17+
18+
19+
20+
21+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import random
2+
from typing import List
3+
4+
5+
class Solution:
6+
def findKthLargest(self, nums: List[int], k: int) -> int:
7+
target = len(nums) - k
8+
9+
def quickselect(left: int, right: int) -> int:
10+
pivot_index = random.randint(left, right)
11+
pivot = nums[pivot_index]
12+
13+
# 3-way partition (Dutch National Flag)
14+
low = left
15+
mid = left
16+
high = right
17+
18+
while mid <= high:
19+
if nums[mid] < pivot:
20+
nums[low], nums[mid] = nums[mid], nums[low]
21+
low += 1
22+
mid += 1
23+
elif nums[mid] > pivot:
24+
nums[mid], nums[high] = nums[high], nums[mid]
25+
high -= 1
26+
else:
27+
mid += 1
28+
29+
# All elements equal to pivot are in [low, high]
30+
if target < low:
31+
return quickselect(left, low - 1)
32+
elif target > high:
33+
return quickselect(high + 1, right)
34+
else:
35+
return nums[target]
36+
37+
return quickselect(0, len(nums) - 1)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function findKthLargest(nums: number[], k: number): number {
2+
const target = nums.length - k;
3+
4+
function quickselect(left: number, right: number): number {
5+
const pivotIndex = left + Math.floor(Math.random() * (right - left + 1));
6+
const pivot = nums[pivotIndex];
7+
8+
// 3-way partition (Dutch National Flag)
9+
let low = left;
10+
let mid = left;
11+
let high = right;
12+
13+
while (mid <= high) {
14+
if (nums[mid] < pivot) {
15+
[nums[low], nums[mid]] = [nums[mid], nums[low]];
16+
low++;
17+
mid++;
18+
} else if (nums[mid] > pivot) {
19+
[nums[mid], nums[high]] = [nums[high], nums[mid]];
20+
high--;
21+
} else {
22+
mid++;
23+
}
24+
}
25+
26+
// All elements equal to pivot are in [low, high]
27+
if (target < low) {
28+
return quickselect(left, low - 1);
29+
} else if (target > high) {
30+
return quickselect(high + 1, right);
31+
} else {
32+
return nums[target];
33+
}
34+
}
35+
36+
return quickselect(0, nums.length - 1);
37+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import unittest
2+
from typing import List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_116_kth_largest_element_in_array import Solution
5+
6+
7+
class KthLargestElementTestCase(unittest.TestCase):
8+
def setUp(self):
9+
self.solution = Solution()
10+
11+
def test_example_1(self):
12+
"""Test: nums = [3,2,1,5,6,4], k = 2, expected = 5"""
13+
nums = [3, 2, 1, 5, 6, 4]
14+
k = 2
15+
result = self.solution.findKthLargest(nums, k)
16+
self.assertEqual(result, 5)
17+
18+
def test_example_2(self):
19+
"""Test: nums = [3,2,3,1,2,4,5,5,6], k = 4, expected = 4"""
20+
nums = [3, 2, 3, 1, 2, 4, 5, 5, 6]
21+
k = 4
22+
result = self.solution.findKthLargest(nums, k)
23+
self.assertEqual(result, 4)
24+
25+
def test_single_element(self):
26+
"""Test: nums = [1], k = 1, expected = 1"""
27+
nums = [1]
28+
k = 1
29+
result = self.solution.findKthLargest(nums, k)
30+
self.assertEqual(result, 1)
31+
32+
def test_all_same_elements(self):
33+
"""Test: nums = [3,3,3,3], k = 2, expected = 3"""
34+
nums = [3, 3, 3, 3]
35+
k = 2
36+
result = self.solution.findKthLargest(nums, k)
37+
self.assertEqual(result, 3)
38+
39+
def test_k_equals_length(self):
40+
"""Test: nums = [7,6,5,4,3,2,1], k = 7, expected = 1"""
41+
nums = [7, 6, 5, 4, 3, 2, 1]
42+
k = 7
43+
result = self.solution.findKthLargest(nums, k)
44+
self.assertEqual(result, 1)
45+
46+
def test_k_equals_1(self):
47+
"""Test: nums = [2,1,4,3], k = 1, expected = 4"""
48+
nums = [2, 1, 4, 3]
49+
k = 1
50+
result = self.solution.findKthLargest(nums, k)
51+
self.assertEqual(result, 4)
52+
53+
def test_negative_numbers(self):
54+
"""Test: nums = [-1,-2,-3,-4], k = 2, expected = -2"""
55+
nums = [-1, -2, -3, -4]
56+
k = 2
57+
result = self.solution.findKthLargest(nums, k)
58+
self.assertEqual(result, -2)
59+
60+
def test_duplicates_with_target(self):
61+
"""Test: nums = [1,2,2,3,3,4], k = 3, expected = 3"""
62+
nums = [1, 2, 2, 3, 3, 4]
63+
k = 3
64+
result = self.solution.findKthLargest(nums, k)
65+
self.assertEqual(result, 3)
66+
67+
def test_two_elements(self):
68+
"""Test: nums = [2,1], k = 1, expected = 2"""
69+
nums = [2, 1]
70+
k = 1
71+
result = self.solution.findKthLargest(nums, k)
72+
self.assertEqual(result, 2)

0 commit comments

Comments
 (0)