-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BOJ] 1062 가르침 #33
Open
Hellol77
wants to merge
4
commits into
hufslion10th:main
Choose a base branch
from
Hellol77:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+63
−0
Open
[BOJ] 1062 가르침 #33
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from itertools import combinations # 라이브러리 itertools 에서 combinations 를 import 해줍니다. | ||
import sys | ||
N,K = map(int,input().split()) | ||
# A=[] | ||
# for i in range(N): | ||
# x=input() | ||
# y=x[4:-4] | ||
# A.append(x) | ||
A=[sys.stdin.readline().rstrip()[4:-4] for _ in range(N)] # 시간초과가 나서 | ||
|
||
|
||
answer=0 | ||
already={'a','n','t','i','c'} # 처음에 무조건 배워야하는 알파벳 5가지 | ||
remain = set(chr(i) for i in range(97, 123)) - already # 처음 무조건 배워야하는 알파벳 5가지를 뺀 남은 알파벳 | ||
# remain = set(chr(i+97) for i in range(0,26)) - already 이것을 썼을때에는 시간초과가 났는데 위 코드로 바꾸니까 통과가 되네요. | ||
# chr() 함수는 정수를 인자로 받고 해당 정수에 해당하는 유니코드 문자를 반환하는 함수입니다. | ||
# ord() 함수는 문자를 인자로 받고 해당 문자에 해당하는 유니코드 정수를 반환합니다. | ||
# 97부터 'a' 입니다. ord('a')=97 chr(97)='a' 입니다. | ||
def a(A,learn): # 처음에는 함수코드를 사용 안하고 global코드를 이용했습니다. 하지만 시간초과로 함수코드로 옮겨봤습니다. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. global 변수랑 function 저장 방식 때문에 차이가 있다는건 알고 있었는데 그렇게 차이가 안 클줄 알았었는데 아닌가 보네요. 배워갑니다!! |
||
c=0 | ||
for word in A: | ||
flag=1 # flag를 잡아줍니다. | ||
for w in word: | ||
if learn[ord(w)]==0: | ||
# 만약 단어들중에 배우지 않는 알파벳이 있다면 읽지 못하는 단어이므로 c에 1을 늘리지 않도록 flag를 0으로 합니다. | ||
flag=0 | ||
break | ||
if flag==1: # 이 지점까지 flag가 1 일때 읽을수 있다는 것이므로 c를 1 늘려줍니다. | ||
c+=1 | ||
return c | ||
|
||
|
||
if K>=5: | ||
learn=[0]*123 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. learn 리스트에서 사용되는 범위는 learn[97]~learn[122] 까지 인가요? |
||
for i in already: | ||
learn[ord(i)]=1 # 무조건 배워야하는 알파벳 5가지를 배웁니다. | ||
for j in list(combinations(remain,K-5)): # 무조건 배워야하는 알파벳 5가지를 뺀 K-5개 를 사용해 단어를 읽어야 합니다. | ||
for k in j: | ||
learn[ord(k)]=1 # 알파벳을 배우는 과정 | ||
c=a(A,learn) | ||
|
||
if c>answer: | ||
answer=c | ||
for k in j : | ||
learn[ord(k)]=0 # learn 리스트를 처음 상태로 되돌려 놓습니다. | ||
print(answer) | ||
else: | ||
print(0) | ||
|
||
|
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,13 @@ | ||
H,W=map(int,input().split()) | ||
A=list(map(int,input().split())) | ||
ans=0 | ||
|
||
for i in range(1,W-1): # 양 끝은 물이 차오를수 없으므로 인덱스 범위는 1~W-2 | ||
l=max(A[:i]) # i에서 부터 왼쪽 끝까지의 블록중 가장 큰수 | ||
r=max(A[i:]) # i에서 부터 오른쪽 끝까지의 블록중 가장 큰수 | ||
|
||
k=min(l,r) # 둘중 작은 블록의 수 | ||
|
||
if A[i]<k: # i 지점에 쌓여있는 블록보다 k가 커야 물이 고일수 있습니다. | ||
ans+=k-A[i] | ||
print(ans) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
처음 알았습니다... 10^6 데이터 입력에 대해 sys.stdin.readline()이 input()에 비해 10배 넘게 빠르다는군요...
도움이 될까 해서 다른 함수들에 대해서도 속도 관련해 정리된 링크 첨부드립니다!
https://medium.com/@xkumiyu/8-speed-differences-you-need-to-know-in-python-db7266c3b177