Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:

answer = dict()

for k, v in enumerate(nums):

if v in answer:
return [answer[v], k]
else:
answer[target - v] = k

return []





Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import re

class Solution:
def isPalindrome(self, s: str) -> bool:

# To lowercase
s = s.lower()

# Remove non-alphanumeric characters
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)

# Determine if s is palindrome or not
len_s = len(s)

for i in range(len_s//2):

if s[i] != s[len_s - 1 - i]:
return False

return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import random
from typing import List


class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
target = len(nums) - k

def quickselect(left: int, right: int) -> int:
pivot_index = random.randint(left, right)
pivot = nums[pivot_index]

# 3-way partition (Dutch National Flag)
low = left
mid = left
high = right

while mid <= high:
if nums[mid] < pivot:
nums[low], nums[mid] = nums[mid], nums[low]
low += 1
mid += 1
elif nums[mid] > pivot:
nums[mid], nums[high] = nums[high], nums[mid]
high -= 1
else:
mid += 1

# All elements equal to pivot are in [low, high]
if target < low:
return quickselect(left, low - 1)
elif target > high:
return quickselect(high + 1, right)
else:
return nums[target]

return quickselect(0, len(nums) - 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function findKthLargest(nums: number[], k: number): number {
const target = nums.length - k;

function quickselect(left: number, right: number): number {
const pivotIndex = left + Math.floor(Math.random() * (right - left + 1));
const pivot = nums[pivotIndex];

// 3-way partition (Dutch National Flag)
let low = left;
let mid = left;
let high = right;

while (mid <= high) {
if (nums[mid] < pivot) {
[nums[low], nums[mid]] = [nums[mid], nums[low]];
low++;
mid++;
} else if (nums[mid] > pivot) {
[nums[mid], nums[high]] = [nums[high], nums[mid]];
high--;
} else {
mid++;
}
}

// All elements equal to pivot are in [low, high]
if (target < low) {
return quickselect(left, low - 1);
} else if (target > high) {
return quickselect(high + 1, right);
} else {
return nums[target];
}
}

return quickselect(0, nums.length - 1);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import unittest
from typing import List
from src.my_project.interviews.top_150_questions_round_22\
.ex_116_kth_largest_element_in_array import Solution


class KthLargestElementTestCase(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_example_1(self):
"""Test: nums = [3,2,1,5,6,4], k = 2, expected = 5"""
nums = [3, 2, 1, 5, 6, 4]
k = 2
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, 5)

def test_example_2(self):
"""Test: nums = [3,2,3,1,2,4,5,5,6], k = 4, expected = 4"""
nums = [3, 2, 3, 1, 2, 4, 5, 5, 6]
k = 4
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, 4)

def test_single_element(self):
"""Test: nums = [1], k = 1, expected = 1"""
nums = [1]
k = 1
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, 1)

def test_all_same_elements(self):
"""Test: nums = [3,3,3,3], k = 2, expected = 3"""
nums = [3, 3, 3, 3]
k = 2
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, 3)

def test_k_equals_length(self):
"""Test: nums = [7,6,5,4,3,2,1], k = 7, expected = 1"""
nums = [7, 6, 5, 4, 3, 2, 1]
k = 7
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, 1)

def test_k_equals_1(self):
"""Test: nums = [2,1,4,3], k = 1, expected = 4"""
nums = [2, 1, 4, 3]
k = 1
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, 4)

def test_negative_numbers(self):
"""Test: nums = [-1,-2,-3,-4], k = 2, expected = -2"""
nums = [-1, -2, -3, -4]
k = 2
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, -2)

def test_duplicates_with_target(self):
"""Test: nums = [1,2,2,3,3,4], k = 3, expected = 3"""
nums = [1, 2, 2, 3, 3, 4]
k = 3
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, 3)

def test_two_elements(self):
"""Test: nums = [2,1], k = 1, expected = 2"""
nums = [2, 1]
k = 1
result = self.solution.findKthLargest(nums, k)
self.assertEqual(result, 2)
Loading