-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import math | ||
|
||
class DichotomousSearchRight(): # 向右边找 | ||
def __init__(self): | ||
pass | ||
|
||
# 查找k在有序数组nums 中得位置。 nums是升序得 | ||
#return index, flag flag是代表是否有和k匹配得数得bool。这里没有考虑数组中有很多相同的元素 | ||
def search(self, nums, k): | ||
l = 0 | ||
r = len(nums)-1# 取右边 | ||
|
||
while l<r: | ||
mid =math.ceil((l + r) / 2) | ||
|
||
if nums[mid]> k: # 向左边找 | ||
r = mid - 1 | ||
elif nums[mid] <k: # 向右边找 | ||
l = mid +1 | ||
elif nums[mid] == k: | ||
l = mid | ||
|
||
|
||
|
||
if nums[l] == k: | ||
return l, True | ||
elif nums[l] >k: | ||
return l, False | ||
elif nums[l] < k: | ||
return l+1, False | ||
|
||
|
||
class DichotomousSearchLeft(): # 向右边找 | ||
def __init__(self): | ||
pass | ||
|
||
|
||
# 查找k在有序数组nums 中得位置。 nums是升序得 | ||
#return index, flag flag是代表是否有和k匹配得数得bool。这里没有考虑数组中有很多相同的元素 | ||
def search(self, nums, k): | ||
l = 0 | ||
r = len(nums)-1# 取右边 | ||
|
||
while l<r: | ||
# print(l, r) | ||
|
||
mid =math.floor((l + r) / 2) | ||
|
||
if nums[mid]> k: # 向左边找 | ||
r = mid - 1 | ||
elif nums[mid] <k: # 向右边找 | ||
l = mid +1 | ||
elif nums[mid] == k: | ||
r = mid | ||
|
||
if nums[l] == k: | ||
return l, True | ||
elif nums[l] >k: | ||
return l, False | ||
elif nums[l] < k: | ||
return l+1, False | ||
|
||
|
||
|
||
d = DichotomousSearchRight() | ||
|
||
print('向右边查找',d.search( | ||
[1,9, 9,9,9,11,11,11,16], | ||
9 | ||
) | ||
) | ||
|
||
|
||
|
||
d = DichotomousSearchLeft() | ||
|
||
print('向左边查找',d.search( | ||
[1,9, 9,9,9,11,11,11,16], | ||
8 | ||
|
||
) | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
Mod = 10**9+7 | ||
|
||
class DichotomousSearch(): | ||
def __init__(self): | ||
pass | ||
|
||
|
||
# 查找k在有序数组nums 中得位置。 nums是升序得 | ||
#return index, flag flag是代表是否有和k匹配得数得bool。 这里 | ||
def search(self, nums, k,r): | ||
l = 0 | ||
r = r | ||
|
||
while l<r: | ||
mid = (l + r) // 2 | ||
|
||
if nums[mid]> k: # 向左边找 | ||
r = mid - 1 | ||
elif nums[mid] <k: # 向右边找 | ||
l = mid +1 | ||
elif nums[mid] == k: | ||
l = mid +1 | ||
|
||
|
||
if nums[l] == k: | ||
return l, True | ||
else: | ||
return l, False | ||
d= DichotomousSearch() | ||
|
||
def find(index,nums, tar): | ||
# print(tar) | ||
n, f = d.search(nums, tar,r=index) | ||
# print(n, f) | ||
if f: | ||
while nums[n] <= tar and n <= index: | ||
n+=1 | ||
if n> index: | ||
n = index+1 | ||
|
||
return n | ||
else: | ||
while nums[n] <= tar and n <= index: | ||
n+=1 | ||
if n> index: | ||
n = index+1 | ||
return n | ||
|
||
|
||
|
||
|
||
class Solution(object): | ||
def purchasePlans(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: int | ||
""" | ||
nums = sorted(nums) | ||
length = len(nums) | ||
ans = 0 | ||
for i in range(1, length): | ||
index =find(i-1, nums, target- nums[i]) | ||
# print(index) | ||
ans+= index | ||
|
||
print(ans) | ||
return ans % Mod | ||
|
||
|
||
Solution.purchasePlans(None,nums = [1,1,1,1,1,1], target = 6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def orchestraLayout(self, num: int, xPos: int, yPos: int) -> int: | ||
layer = min([xPos, num - xPos - 1, yPos, num - yPos - 1]) # 位于第几圈 | ||
v = (num*layer*4 - layer*layer*4) % 9 # 前几圈有多少个元素 | ||
|
||
start, end = layer, num - layer | ||
|
||
if xPos == start: | ||
return (v + yPos - start) % 9 + 1 | ||
|
||
if yPos == end - 1: | ||
return (v + end - start - 1 + xPos - start) % 9 + 1 | ||
|
||
if xPos == end - 1: | ||
return (v + (end - start)*2 - 2 + end - yPos - 1) % 9 + 1 | ||
|
||
if yPos == start: | ||
return (v + (end - start)*3 - 3 + end - xPos - 1) % 9 + 1 | ||
|
||
return 0 | ||
|