Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
sdj3261
algorithm-study
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm-study
/
binarySearch.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
42 lines (34 loc) · 1.19 KB
main
Breadcrumbs
algorithm-study
/
binarySearch.py
Copy path
Top
File metadata and controls
Code
Blame
42 lines (34 loc) · 1.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#N개의 집이 수직선 위에 있다.
# 이 집들 중 일부에 공유기를 설치하려 한다.
# 한 집에는 하나의 공유기만 설치할 수 있고,
# 가장 인접한 두 공유기 사이의 거리를 가능한 크게 하여 C개의 공유기를 설치하려 한다.
# 최대 인접 거리의 최댓값을 구하라.
n= int(input())
nums = list(map(int,input().split()))
find_num = int(input())
left = 0
right = len(nums)-1
ret = -1
# 배열이 정렬되어있는 경우 해당 구간의 중앙값과 비교해서 왼쪽/오른쪽 절반 중 하나만 보면 된다”는 규칙이다.
while left <= right:# 0과 6 index 기준으로 설계
mid = (left + right) // 2
if nums[mid] == find_num:
ret = mid
break
elif nums[mid] > find_num :
right = mid-1
else :
left = mid+1
print(left,right)
print(ret)
def binary_search(arr, fn, l, r) :
if l > r:
return -1 # 종료 조건
m = (left + right) // 2
if arr[m] == fn :
return m
elif arr[m] < fn:
return binary_search(arr, fn, m+1, r)
elif arr[m] > fn :
return binary_search(arr, fn, l, m-1)
print(binary_search(nums, find_num, 0, len(nums)-1))
You can’t perform that action at this time.