Skip to content

Commit 19576c9

Browse files
committed
Merge branch 'main' of https://github.com/python-geeks/Leetcode-scripts into allwells
2 parents a9708fc + b93ca38 commit 19576c9

File tree

8 files changed

+189
-0
lines changed

8 files changed

+189
-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+
```

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

704/REAMDE.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Binary Search
2+
3+
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
4+
5+
You must write an algorithm with O(log n) runtime complexity.
6+
7+
### Example 1:
8+
Input: nums = [-1,0,3,5,9,12], target = 9
9+
Output: 4
10+
11+
### Example 2:
12+
Input: nums = [-1,0,3,5,9,12], target = 2
13+
Output: -1
14+
15+
Question link : [Binary Search](https://leetcode.com/problems/binary-search/)
16+

704/binarySearch.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def search(self, arr, target: int) -> int:
3+
# simple Binary Search Algorithm
4+
5+
start = 0
6+
end = len(arr) - 1
7+
8+
while start <= end:
9+
mid = start + (end - start) // 2
10+
if arr[mid] == target:
11+
return mid
12+
elif target < arr[mid]:
13+
end = mid - 1
14+
else:
15+
start = mid + 1
16+
return -1

0 commit comments

Comments
 (0)