Skip to content

Commit 3a6ffe2

Browse files
committed
Added
1 parent 868a7ee commit 3a6ffe2

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

kth_largest_element_in_an_array/solution.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Note:
99
You may assume k is always valid, 1 <= k <= array's length
1010
"""
11+
1112
import heapq
1213

1314
class Solution(object):
@@ -25,6 +26,7 @@ def findKthLargest(self, nums, k):
2526
if i == k - 1:
2627
return e
2728

29+
2830
a1 = [3, 2, 1, 5, 6, 4]
2931
s = Solution()
3032
res = s.findKthLargest(a1, 2)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Find the kth largest element in an unsorted array. Note that it is the kth
3+
largest element in the sorted order, not the kth distinct element.
4+
5+
For example,
6+
Given [3,2,1,5,6,4] and k = 2, return 5.
7+
8+
Note: You may assume k is always valid, 1 <= k <= array's length.
9+
"""
10+
11+
12+
class Solution(object):
13+
def findKthLargest(self, nums, k):
14+
"""
15+
:type nums: List[int]
16+
:type k: int
17+
:rtype: int
18+
19+
Quickselect: O(n)
20+
"""
21+
left = 0
22+
right = len(nums) - 1
23+
while left <= right:
24+
pivot = self.partition(nums, left, right)
25+
# nums[pivot] is (pivot + 1)th largest, so
26+
# if pivot == k - 1, it is kth largest.
27+
if pivot == k - 1:
28+
return nums[pivot]
29+
elif pivot < k - 1:
30+
left = pivot + 1
31+
else:
32+
right = pivot - 1
33+
34+
def partition(self, nums, left, right):
35+
"""Partition the array so that larger elements are to the left"""
36+
pivot = right
37+
# i is from left to right - 1
38+
j = left
39+
for i in range(left, right):
40+
if nums[i] > nums[pivot]:
41+
nums[i], nums[j] = nums[j], nums[i]
42+
j += 1
43+
nums[j], nums[pivot] = nums[pivot], nums[j]
44+
return j
45+
46+
47+
s = Solution()
48+
a = [3, 2, 1, 5, 6, 4]
49+
50+
print s.findKthLargest(a, 2)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
3+
4+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
5+
6+
You are given a target value to search. If found in the array return its
7+
index, otherwise return -1.
8+
9+
You may assume no duplicate exists in the array.
10+
"""
11+
12+
class Solution(object):
13+
def search(self, nums, target):
14+
"""
15+
:type nums: List[int]
16+
:type target: int
17+
:rtype: int
18+
"""
19+
left = 0
20+
right = len(nums) - 1
21+
while left + 1 < right:
22+
mid = left + (right - left) / 2
23+
if target == nums[mid]:
24+
return mid
25+
# Right side is sorted
26+
elif nums[mid] < nums[right]:
27+
if nums[mid] <= target <= nums[right]:
28+
left = mid
29+
else:
30+
right = mid
31+
# Left side is sorted
32+
else:
33+
if nums[left] <= target <= nums[mid]:
34+
right = mid
35+
else:
36+
left = mid
37+
if nums[left] == target:
38+
return left
39+
elif nums[right] == target:
40+
return right
41+
return -1
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
3+
4+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
5+
6+
Follow up for "Search in Rotated Sorted Array":
7+
8+
What if duplicates are allowed?
9+
10+
Would this affect the run-time complexity? How and why?
11+
12+
Write a function to determine if a given target is in the array.
13+
"""
14+
15+
class Solution(object):
16+
def search(self, nums, target):
17+
"""
18+
:type nums: List[int]
19+
:type target: int
20+
:rtype: bool
21+
"""
22+
left = 0
23+
right = len(nums) - 1
24+
while left + 1 < right:
25+
mid = left + (right - left) / 2
26+
if target == nums[mid]:
27+
return True
28+
# Left part is sorted
29+
elif nums[mid] > nums[right]:
30+
if nums[left] <= target < nums[mid]:
31+
right = mid
32+
else:
33+
left = mid
34+
# Right part is sorted
35+
elif nums[mid] < nums[right]:
36+
if nums[mid] < target <= nums[right]:
37+
left = mid
38+
else:
39+
right = mid
40+
else:
41+
right -= 1
42+
if nums[left] == target or nums[right] == target:
43+
return True
44+
return False

0 commit comments

Comments
 (0)