Skip to content

Commit 6c19752

Browse files
committed
Updated broken links from the resources, and updated several solutions
1 parent 698ea30 commit 6c19752

6 files changed

+54
-38
lines changed

Diff for: Arrays/find_el_smaller_left_bigger_right.py

+36-25
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
11
'''
22
Find the element before which all the elements are smaller than it, and after which all are greater
33
4-
Given an array, find an element before which all elements are smaller than it, and after which all are greater than it.
5-
Return the index of the element if there is such an element, otherwise, return -1.
4+
Given an unsorted array of size N. Find the first element in array such that all of its left elements are smaller and all right elements to it are greater than it.
5+
Note: Left and right side elements can be equal to required element. And extreme elements cannot be required element.
66
77
Input: [5, 1, 4, 3, 6, 8, 10, 7, 9]
8-
Output: 4
8+
Output: 6
99
1010
=========================================
11-
Traverse the array starting from left and find all max elements till that element.
12-
Also traverse the array starting from right and find all min elements till that element.
13-
In the end only compare mins and maxs with the curent element.
11+
Traverse the array starting and check if the current element is smaller than the wanted one. If it's smaller then reset the result.
12+
In meantime keep track of the maximum value till the current position. This maximum value will be used for finding a new "middle" element.
13+
If the maximum value
1414
Time Complexity: O(N)
15-
Space Complexity: O(N)
15+
Space Complexity: O(1)
1616
'''
1717

1818

1919
############
2020
# Solution #
2121
############
2222

23-
import math
24-
2523
def find_element_smaller_left_bigger_right(arr):
2624
n = len(arr)
27-
left_maxs = [-math.inf]
28-
right_min = math.inf
29-
30-
# find all mins from the front
31-
for i in range(n - 1):
32-
left_maxs.append(max(left_maxs[-1], arr[i]))
25+
curr_max = arr[0]
26+
result = -1
3327

34-
for i in range(n - 1, -1, -1):
35-
# check if all left are smaller
36-
# and all right are bigger
37-
if (left_maxs[i] < arr[i]) and (right_min > arr[i]):
38-
return i
28+
for i in range(1, n):
29+
curr_el = arr[i]
3930

40-
# don't need a separate for loop for this as mins
41-
right_min = min(right_min, arr[i])
31+
if result == -1 and curr_el >= curr_max and i != n - 1:
32+
result = curr_el
33+
elif curr_el < result:
34+
result = -1
4235

43-
return -1
36+
if curr_el > curr_max:
37+
curr_max = curr_el
4438

39+
return result
4540

4641
###########
4742
# Testing #
4843
###########
4944

5045
# Test 1
51-
# Correct result => 4
46+
# Correct result => 6
5247
print(find_element_smaller_left_bigger_right([5, 1, 4, 3, 6, 8, 10, 7, 9]))
5348

5449
# Test 2
5550
# Correct result => -1
56-
print(find_element_smaller_left_bigger_right([5, 1, 4, 4]))
51+
print(find_element_smaller_left_bigger_right([5, 1, 4, 4]))
52+
53+
# Test 3
54+
# Correct result => 7
55+
print(find_element_smaller_left_bigger_right([5, 1, 4, 6, 4, 7, 14, 8, 19]))
56+
57+
# Test 4
58+
# Correct result => 5
59+
print(find_element_smaller_left_bigger_right([4, 2, 5, 7]))
60+
61+
# Test 5
62+
# Correct result => -1
63+
print(find_element_smaller_left_bigger_right([11, 9, 12]))
64+
65+
# Test 6
66+
# Correct result => 234
67+
print(find_element_smaller_left_bigger_right([177, 234, 236, 276, 519, 606, 697, 842, 911, 965, 1086, 1135, 1197, 1273, 1392, 1395, 1531, 1542, 1571, 1682, 2007, 2177, 2382, 2410, 2432, 2447, 2598, 2646, 2672, 2826, 2890, 2899, 2916, 2955, 3278, 3380, 3623, 3647, 3690, 4186, 4300, 4395, 4468, 4609, 4679, 4712, 4725, 4790, 4851, 4912, 4933, 4942, 5156, 5186, 5188, 5244, 5346, 5538, 5583, 5742, 5805, 5830, 6010, 6140, 6173, 6357, 6412, 6414, 6468, 6582, 6765, 7056, 7061, 7089, 7250, 7275, 7378, 7381, 7396, 7410, 7419, 7511, 7625, 7639, 7655, 7776, 7793, 8089, 8245, 8622, 8758, 8807, 8969, 9022, 9149, 9150, 9240, 9273, 9573, 9938]))

Diff for: Arrays/find_el_where_k_greater_or_equal.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
88
Input: [3,8,5,1,10,3,20,24], 2
99
Output: 11
10-
Output explanation: Only 20 and 24 are greater or smaller from 11 (11 is the smallest solution, also 12, 13...20 are solutions).
10+
Output explanation: Only 20 and 24 are equal or smaller from 11 (11 is the smallest solution, also 12, 13...20 are solutions).
1111
1212
=========================================
1313
Sort the array and check the Kth element from the end.
1414
Time Complexity: O(NLogN)
1515
Space Complexity: O(1)
16+
QuickSelect can be used (find the K+1th number + 1). https://en.wikipedia.org/wiki/Quickselect
17+
See kth_smallest.py, very similar solution.
18+
Time Complexity: O(N)
19+
Space Complexity: O(1)
1620
'''
1721

1822

@@ -46,4 +50,4 @@ def get_minimum_X(arr, k):
4650

4751
# Test 1
4852
# Correct result => 11
49-
print(get_minimum_X([3, 8, 5, 1, 10, 3, 20, 24], 2))
53+
print(get_minimum_X([3, 8, 5, 1, 10, 3, 20, 24], 2))

Diff for: Arrays/k_closest_points.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
Time Complexity: O(N) , O(N + N/2 + N/4 + N/8 + ... + 1 = 2*N = N)
1414
Space Complexity: O(K) , length of the output array
1515
Completely the same algorithm as the previous one, but without recursion. This solution is cleaner.
16+
This algorithm is called: QucikSelect - The quicksort pivoting logic but for searching kth smallest (not sorting the whole array) - O(n) complexity (n + n/2 + n/4 + ... + 1 = 2n)
17+
https://en.wikipedia.org/wiki/Quickselect
18+
Same solution as kth_smallest.py.
1619
Time Complexity: O(N)
1720
Space Complexity: O(K)
1821
'''
@@ -111,4 +114,4 @@ def find_k_closes(arr, pt, k):
111114
# Test 2
112115
# Correct result => [(1, 2), (2, 1)]
113116
print(find_k_closes_recursive([(0, 1), (2, 1), (3, 3), (1, 2)], (2, 2), 2))
114-
print(find_k_closes([(0, 1), (2, 1), (3, 3), (1, 2)], (2, 2), 2))
117+
print(find_k_closes([(0, 1), (2, 1), (3, 3), (1, 2)], (2, 2), 2))

Diff for: Arrays/kth_smallest.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
Time Complexity: O(N) , O(N + N/2 + N/4 + N/8 + ... + 1 = 2*N = N)
1919
Space Complexity: O(LogN) , because of the recursion stack
2020
Completely the same algorithm as the previous one, but without recursion. This solution is cleaner.
21+
This algorithm is called: QucikSelect - The quicksort pivoting logic but for searching kth smallest (not sorting the whole array) - O(n) complexity (n + n/2 + n/4 + ... + 1 = 2n)
22+
https://en.wikipedia.org/wiki/Quickselect
2123
Time Complexity: O(N)
2224
Space Complexity: O(1)
2325
'''
@@ -119,4 +121,4 @@ def find_kth_smallest(arr, k):
119121
# Correct result => 1 2 3 4 5
120122
arr = [5, 4, 3, 2, 1]
121123
print([find_kth_smallest_recursive(arr, i) for i in range(1, len(arr) + 1)])
122-
print([find_kth_smallest(arr, i) for i in range(1, len(arr) + 1)])
124+
print([find_kth_smallest(arr, i) for i in range(1, len(arr) + 1)])

Diff for: Arrays/top_k_frequent_elements.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
Time Complexity: O(U LogK) , U in this case is the number of unique elements (but all elements from the array could be unique, so because of that U can be equal to N)
1717
Space Complexity: O(N)
1818
Using pivoting, this solution is based on the quick sort algorithm (divide and conquer).
19-
Same pivoting solution as the nth_smallest.py.
19+
This algorithm is called: QucikSelect - The quicksort pivoting logic but for searching kth smallest (not sorting the whole array) - O(n) complexity (n + n/2 + n/4 + ... + 1 = 2n)
20+
https://en.wikipedia.org/wiki/Quickselect
21+
Same solution as kth_smallest.py.
2022
Time Complexity: O(U)
2123
Space Complexity: O(N)
2224
'''
@@ -161,4 +163,4 @@ def swap(arr, i, j):
161163
# Test 2
162164
# Correct result => [1]
163165
print(top_k_frequent_1([1], 1))
164-
print(top_k_frequent_2([1], 1))
166+
print(top_k_frequent_2([1], 1))

Diff for: README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ If the problems from [LeetCode](https://leetcode.com/) are not enough and you ne
174174
- [Topcoder](http://topcoder.com/)
175175
- [Project Euler](https://projecteuler.net/)
176176
- [SPOJ](http://www.spoj.com/)
177-
- [A2OJ](https://a2oj.com/)
178177
- [PEG](https://wcipeg.com/)
179178
- [Online Judge](https://onlinejudge.org/)
180179
- [E-Olymp](https://www.e-olymp.com/en/)
@@ -183,7 +182,6 @@ If the problems from [LeetCode](https://leetcode.com/) are not enough and you ne
183182
- [USA CO](http://www.usaco.org/)
184183
- [Rosetta Code](http://rosettacode.org/)
185184
- [AtCoder](https://atcoder.jp/)
186-
- [Russian Code Cup](https://www.russiancodecup.ru/en/)
187185
- [LintCode](http://www.lintcode.com/en/)
188186
- [Kattis](https://www.kattis.com/developers)
189187
- [CodeAbbey](http://codeabbey.com/)
@@ -196,11 +194,9 @@ If the problems from [LeetCode](https://leetcode.com/) are not enough and you ne
196194
- [Codewars](http://www.codewars.com/)
197195
- [Wolfram Challenges](https://challenges.wolfram.com/)
198196
- [Google's Coding Competitions](https://codingcompetitions.withgoogle.com/)
199-
- [Google Foobar](https://foobar.withgoogle.com/)
200197
- [Cyber-dojo](https://cyber-dojo.org/)
201198
- [CodingBat](http://codingbat.com/)
202199
- [CodeKata](http://codekata.com/)
203-
- [BinarySearch](https://binarysearch.io/)
204200
- [Daily Coding Problem](https://www.dailycodingproblem.com/)
205201
- [Daily Interview Pro](http://dailyinterviewpro.com/)
206202
- [AlgoDaily](https://algodaily.com/)
@@ -213,7 +209,6 @@ If the problems from [LeetCode](https://leetcode.com/) are not enough and you ne
213209
- [Brilliant](http://brilliant.org/)
214210
- [Codingame](https://www.codingame.com/)
215211
- [CheckiO](http://www.checkio.org/)
216-
- [FightCode](http://fightcodegame.com/)
217212
- [Kaggle](http://kaggle.com/)
218213
- [Rosalind](http://rosalind.info/problems/locations/)
219214
- [workat.tech](https://workat.tech/problem-solving/practice/)
@@ -230,13 +225,12 @@ If the problems from [LeetCode](https://leetcode.com/) are not enough and you ne
230225
- [Algorithm Visualizer](https://algorithm-visualizer.org/) - Interactive online platform that visualizes algorithms from code. This platform is an open-source project, [here](https://github.com/algorithm-visualizer/algorithm-visualizer) you can find the source code.
231226
5. Courses and tutorials (but not from universities like the [Courses](#Courses) section):
232227
- [Google - Intro to Data Structures and Algorithms](https://www.udacity.com/course/data-structures-and-algorithms-in-python--ud513) - Free course on Udacity offered by Google.
233-
- [Microsoft - Algorithms and Data Structures](https://www.edx.org/course/algorithms-and-data-structures) - Free course on edX offered by Microsoft.
234228
- [HackerEarth - Tutorials and Practice](https://www.hackerearth.com/practice/) - Practice problems and learn about many algorithms and data structures needed for competitive programming.
235229
- [KhanAcademy - Algorithms](https://www.khanacademy.org/computing/computer-science/algorithms) - Good explanations for some basic algorithms.
236230
- [Tutorialspoint - Data Structures and Algorithms](https://www.tutorialspoint.com/data_structures_algorithms/index.htm) - Another platform with good explanations, also Tutorialspoint has free tutorials for almost everything related to CS!
237231
- [Programiz - Data Structures and Algorithms](https://www.programiz.com/dsa) - One more platform which explains the data structures and algorithms in a simple and interesting way.
238232
- [Hackr.io - Data Structures and Algorithms Tutorials and Courses](https://hackr.io/tutorials/learn-data-structures-algorithms) - Big collection of tutorials and courses.
239-
- [Scaler - Data Structures Tutorial](https://www.scaler.com/topics/data-structures/) - Learn in-depth about the need & applications of data structures.
233+
- [Scaler - Data Structures Tutorial](https://www.scaler.com/topics/data-structures/) - Interesting and interactive explanations of some basic data structures.
240234
6. YouTube playlists with tutorials:
241235
- [Data Structures by mycodeschool](https://www.youtube.com/playlist?list=PL2_aWCzGMAwI3W_JlcBbtYTwiQSsOTa6P)
242236
- [Data Structures by HackerRank](https://www.youtube.com/playlist?list=PLI1t_8YX-Apv-UiRlnZwqqrRT8D1RhriX)

0 commit comments

Comments
 (0)