Skip to content

Commit 2648570

Browse files
authored
Merge branch 'python-geeks:main' into allwells
2 parents f4e4c68 + b93ca38 commit 2648570

File tree

12 files changed

+285
-0
lines changed

12 files changed

+285
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def search(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: int
7+
"""
8+
low, high = 0, len(nums) - 1
9+
while low <= high:
10+
mid = low + ((high - low) >> 1)
11+
if nums[mid] == target:
12+
return mid
13+
elif nums[low] > nums[mid]:
14+
if nums[mid] < target and nums[high] >= target:
15+
low = mid + 1
16+
else:
17+
high = mid - 1
18+
elif nums[low] <= nums[mid]:
19+
if nums[low] <= target and nums[mid] > target:
20+
high = mid - 1
21+
else:
22+
low = mid + 1
23+
return -1

0033/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
# **Search in Rotated Sorted Array**
3+
## **Problem Statement:**
4+
5+
There is an integer array nums sorted in ascending order (with distinct values).
6+
7+
Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
8+
9+
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
10+
11+
You must write an algorithm with O(log n) runtime complexity.
12+
13+
## **Example 1:**
14+
### **Input: nums=[4,5,6,7,0,1,2]**
15+
## **target:4**
16+
## **Output:4**
17+
18+
## **Example 2:**
19+
### **Input: nums=[4,5,6,7,0,1,2]**
20+
### **target: 3**
21+
### **Output:-1**
22+
23+
# **Solution:**
24+
To find the target element .in log(N) time.We have to use a Binary search function.But it only works on
25+
Sorted array. Because our list is rotated , it is not sorted anymore.
26+
So we have to make a slight change in binary search function.
27+
## **Changes in Code snippet**
28+
```angular2html
29+
if nums[mid] == target:
30+
return mid
31+
elif nums[low] > nums[mid]://Reassign the variables if the array is not sorted
32+
if nums[mid] < target and nums[high] >= target:
33+
low = mid + 1
34+
else:
35+
high = mid - 1
36+
elif nums[low] <= nums[mid]:
37+
if nums[low] <= target and nums[mid] > target:
38+
high = mid - 1
39+
else:
40+
low = mid + 1
41+
42+
```

0172/Readme.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 172. Factorial Trailing Zeroes
2+
3+
Given an integer n, return the number of trailing zeroes in n!.
4+
5+
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
6+
7+
### Examples:
8+
9+
Example 1:
10+
11+
Input: n = 3
12+
Output: 0
13+
Explanation: 3! = 6, no trailing zero.
14+
15+
16+
Example 2:
17+
18+
Input: n = 5
19+
Output: 1
20+
Explanation: 5! = 120, one trailing zero.
21+
22+
Example 3:
23+
24+
Input: n = 0
25+
Output: 0
26+
27+
28+
## Constraints:
29+
30+
0 <= n <= 104
31+
32+
#### Question link : [172_Factorial_Trailing_Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/)

0172/factorial-trailing-zeros.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Logic: Every trailing zero comes prom a power of 10 and every 10 must contain a 5
2+
so just count power of 5 gives us no. of trailing zeros."""
3+
4+
5+
class Solution:
6+
def trailingZeroes(self, n: int) -> int:
7+
a = 0
8+
while (n >= 5):
9+
n //= 5
10+
a += n
11+
return a

0204/Readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 204. Count Primes
2+
3+
Given an integer n, return the number of prime numbers that are strictly less than n.
4+
5+
### Examples:
6+
7+
Example 1:
8+
9+
Input: n = 10
10+
Output: 4
11+
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
12+
13+
14+
Example 2:
15+
16+
Input: n = 0
17+
Output: 0
18+
19+
Example 3:
20+
21+
Input: n = 1
22+
Output: 0
23+
24+
25+
## Constraints:
26+
27+
0 <= n <= 5 * 106
28+
29+
#### Question link : [204_count_primes](https://leetcode.com/problems/count-primes/)

0204/count-primes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# I have used sieve of erato algorithm to solve this problem
3+
4+
def seive(Max):
5+
primes = []
6+
isprime = [False] * (Max)
7+
maxN = Max
8+
if maxN < 2:
9+
return 0
10+
if maxN >= 2:
11+
isprime[0] = isprime[1] = True
12+
for i in range(2, maxN):
13+
if (isprime[i] is False):
14+
for j in range(i * i, maxN, i):
15+
isprime[j] = True
16+
for i in range(Max):
17+
if isprime[i] is False:
18+
primes.append(i)
19+
return len(primes)
20+
21+
22+
class Solution:
23+
def countPrimes(self, n: int) -> int:
24+
return seive(n)

1833/READme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 1833. Maximum Ice Cream Bars
2+
It is a sweltering summer day, and a boy wants to buy some ice cream bars.
3+
4+
At the store, there are n ice cream bars. You are given an array costs of length n, where costs[i] is the price of the ith ice cream bar in coins. The boy initially has coins coins to spend, and he wants to buy as many ice cream bars as possible.
5+
6+
Return the maximum number of ice cream bars the boy can buy with coins coins.
7+
8+
Note: The boy can buy the ice cream bars in any order.
9+
10+
11+
### Examples:
12+
13+
Example 1:
14+
15+
Input: costs = [1,3,2,4,1], coins = 7
16+
Output: 4
17+
Explanation: The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.
18+
Example 2:
19+
20+
Input: costs = [10,6,8,7,7,8], coins = 5
21+
Output: 0
22+
Explanation: The boy cannot afford any of the ice cream bars.
23+
Example 3:
24+
25+
Input: costs = [1,6,3,1,2,5], coins = 20
26+
Output: 6
27+
Explanation: The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.
28+
29+
30+
Question Link: https://leetcode.com/problems/maximum-ice-cream-bars/

1833/maxiumIceCreamBars.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maxIceCream(self, costs: list[int], coins: int) -> int:
3+
costs.sort()
4+
total = 0
5+
for c in costs:
6+
if coins < c:
7+
break
8+
coins -= c
9+
total += 1
10+
11+
return total

374/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Guess Number Higher or Lower
2+
3+
We are playing the Guess Game. The game is as follows:
4+
5+
I pick a number from 1 to n. You have to guess which number I picked.
6+
7+
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
8+
9+
You call a pre-defined API int guess(int num), which returns 3 possible results:
10+
11+
-1: The number I picked is lower than your guess (i.e. pick < num).
12+
1: The number I picked is higher than your guess (i.e. pick > num).
13+
0: The number I picked is equal to your guess (i.e. pick == num).
14+
Return the number that I picked.

374/guessNumberHigherOrLower.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# The guess API is already defined for you.
2+
# @param num, your guess
3+
# @return -1 if my number is lower,
4+
# 1 if my number is higher, otherwise return 0
5+
6+
# this function is just added to pass the flake8 code linter
7+
# delete this function definition while using it on leetcode
8+
def guess(num: int) -> int:
9+
pass
10+
11+
12+
class Solution:
13+
def guessNumber(self, n: int) -> int:
14+
# here start = 1 ans end = n
15+
# I supply mid as my pick and check with the original pick
16+
# if pick is not right, update start or end accodingly
17+
18+
start = 1
19+
end = n
20+
while start <= end:
21+
22+
mid = start + (end - start) // 2
23+
x = guess(mid)
24+
25+
if x == 0:
26+
# element is found
27+
return mid
28+
29+
elif x == -1:
30+
31+
# guessed element is greater than actual element
32+
# search in the left of mid
33+
end = mid - 1
34+
else:
35+
# guessed element is less than actual element
36+
# search in the right of mid
37+
start = mid + 1

0 commit comments

Comments
 (0)