0921

def three_vowel_or_consonat(s: str): //3์ค ๋ฐ๋ณต์ ํ์ธํ๋ ํจ์
vowels = set('aeiou')
if len(s) < 3:
return True
v = c = 1
for i in range(1, len(s)):
cur_v = s[i] in vowels
prev_v = s[i-1] in vowels
if cur_v and prev_v:
v += 1; c = 1
elif not cur_v and not prev_v:
c += 1; v = 1
else:
v = c = 1
if v >= 3 or c >= 3:
return False
return True
def repeat(s: str): //์ด์ค ๋ฐ๋ณต์ ํ์ธํ๋ ํจ์(์ง์ ๋ ๋ชจ์ 'e', 'o'๋ ํ์ฉ)
allowed_vowel = 'e', 'o'
count = 1
prev = s[0]
for ch in s[1:]:
if ch == prev:
count += 1
if count >= 3:
return False
else:
if count == 2 and prev not in allowed_vowel:
return False
prev = ch
count = 1
if count == 2 and prev not in allowed_vowel:
return False
return True
def func(password):
vowels = set('aeiou')
status = True
status = status and any(ch in vowels for ch in password)
status = status and three_vowel_or_consonat(password)
status = status and repeat(password)
return status
while(True):
password = input()
if(password == 'end'):
break
comp = func(password)
print(f"<{password}> is{' not' if not comp else ''} acceptable.")
ํ์ด๊ณผ์ :
- ๊ธฐ๋ณธ ํจ์ func: ์ ๋ ฅ๋ฐ์ ๋น๋ฐ๋ฒํธ์ ํ์ฉ/๋นํ์ฉ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ํจ์๋ฅผ ๋ง๋ ๋ค. ํด๋น ํจ์์์ ์ง์ ์ ์ผ๋ก ํ๋จํ๋ ์กฐ๊ฑด์ ‘๋ชจ์์ด ํฌํจ๋์๋๊ฐ?’๋ง์ด๋ค
- ์ด์ค ๋ฐ๋ณต ํ์ธ ํจ์ repeat: ํ์ฉ๋๋ ๋ชจ์์ ๋ฏธ๋ฆฌ ์ค์ ํ ๋ค, ๋ฌธ์์ด์ ์ฒซ๋ฒ์งธ ๊ธ์(prev)๋ฅผ ์ง์ ํด ๋ค ๊ธ์์ ๋น๊ตํ๋ค. ์ดํ ์ํ์ด ๋๋๋ฉด prev๋ ๋ค์ ๊ธ์๋ก ์ฎ๊ธด๋ค. ๋ง์ฝ ์ค๊ฐ์ ์๋ชป๋๊ฒ ์์ผ๋ฉด True๋ฅผ returnํ๋ค.
- ์ผ์ค ๋ฐ๋ณต ํ์ธ ํจ์ three_vowel_and_consonat: ํ์ฌ ๊ธ์์ ์ด์ ๊ธ์์ ๋ชจ์ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ๋ ๋ณ์๋ฅผ ๋ง๋ ๋ค. ์ดํ ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ๋ณต๋ฌธ์ ํตํด ์ผ์ค ๋ฐ๋ณต์ด ์กด์ฌํ๋์ง ํ์ธํ๋ค.
๋ฐฐ์ด ์ :
3์ค ๋ฐ๋ณต ํจ์๋ฅผ ๋ง๋ค๋ฉฐ ๊ธฐ๋ณธ์ ์ธ ํ์ ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ๋ฒ์ ์๊ฒ ๋์๋ค.
์ํ ํ์๊ฐ ๊ฝค ๋ง์๋ฐ, ๋ฌธ์ ๋ฅผ ํ๋ฉฐ ์ฌ์ํ ์ค์๊ฐ ๋ง์๋ค(์ถ๋ ฅ๋ฌธ์ <>์ฒ๋ฆฌ ์ํจ, ์จ์ ์ ์ฐ์, return True์์ return ์ ์…)๋ฌธ์ ๋ฅผ ํ๋ฉฐ ์ฑ๊ธํ ์ฑ๊ฒฉ์ ๊ณ ์ณ์ผ ํ ๊ฒ ๊ฐ๋ค
์ฝ๋์์ ์ฌ์ํ๊ฒ ์ค์ผ ์ ์์ ๊ฒ ๊ฐ์ ๋ถ๋ถ์ด ๋ณด์ด๋๋ฐ(๋ชจ์ set ๋ฐ๋ณต ๋ฑ…) ๊ณ ๋ฏผํ๋ฉฐ ์ค์ด๋ ์ต๊ด์ ๋ค์ด๊ณ ์ถ๋ค
์ง๊ธ ์์ ๋ณด๋๊น status ์ด๊ธฐ ์ค์ True๋ก ๊ตณ์ด ์ํด๋ ๋ ๊ฒ ๊ฐ๋ค. ํจ์๋ค์์ ์ ๋ถ ๋ง์ง๋ง์ True ๋ฆฌํด์ด ์๊ธฐ์...
0922

def func(maxnum, num):
lst = [i+1 for i in range(maxnum)]
newlst = []
idx = 0
while lst:
idx = (idx + num - 1) % len(lst)
newlst.append(lst.pop(idx))
return newlst
maxnum, num = map(int, input().split())
resultLst = func(maxnum, num)
print(f"<{', '.join(map(str, resultLst))}>")
ํ์ด๊ณผ์ :
ํจ์์์ 1~maxnum์ ์ฐจ๋ก๋๋ก ๋ด์ ๊ธฐ์กด ๋ฆฌ์คํธ์ ์๋ก์ด ๋ต์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ ๋ค. lst๊ฐ ๊ณต๋ฐฑ์ด ์๋๋ผ๋ฉด, ๋ฐ๋ณต๋ฌธ ๋ด์์ ๋ต์ ๋ฆฌ์คํธ์ ๋ต์ ์ฑ์ฐ๋ ๊ฑธ ๋ฐ๋ณตํ๋ค
๋ฐฐ์ด ์ : ์์ ์ ์๋ํ๋ค๊ฐ ๋ชป ํผ ๋ฌธ์ ์๋๋ฐ, ๊ทธ๋๋ len(lst)≥num์ธ ์ผ์ด์ค์ ์๋ ์ผ์ด์ค๋ก ๊ตฌ๋ถํ๋๋ผ ๋ง์ด ๊ผฌ์์๋ค.
0923

#include <iostream>
using namespace std;
int main(void) {
int E, S, M;
cin >> E >> S >> M;
int year = 1;
while (true) {
int e = (year % 15 == 0) ? 15 : year % 15;
int s = (year % 28 == 0) ? 28 : year % 28;
int m = (year % 19 == 0) ? 19 : year % 19;
if (e == E && s == S && m == M) {
cout << year;
break;
}
year++;
}
return 0;
}
ํ์ด๊ณผ์ : year % 15๋ฅผ ํตํด ์ฝ์ ๊ฐ์๋ฅผ ๋ฐ์ง์ง ์๊ณ ๋จ์ํ ๊ฐ์์ง๋ง ๋น๊ตํ๋ค. ์คํจ์ year++์ ํตํด ์ ๋ต์ด ๋์ฌ ๋๊น์ง ๊ณ์ ๋ฐ๋ณตํ๋ค…
๋ฐฐ์ด ์ : ๋ ํจ์จ์ ์ธ ์๊ณ ๋ฆฌ์ฆ์ด ์กด์ฌํ ์๋ ์์ง๋ง, ํด๋น ์ฝ๋์ ์๊ฐ ๋ณต์ก๋๊ฐ O(152819)๋ก ์์ ํธ์ด๊ธฐ์ ๊ทธ๋ฅ ๋ ธ๊ฐ๋ค ๋ฐฉ์์ ์ ํํ๋ค. ์๋ง ๋ฌธ์ ์ ํฐ์ด๊ฐ ๋์์ผ๋ฉด ์๋ก์ด ๋ฐฉ์์ ์๊ฐํด์ผ ํ ๊ฒ ๊ฐ๋ค
0924

def binary_search(total, win):
nowZ = (win * 100) // total
low, high = 1, 10**9
result = -1
while low <= high:
mid = (low + high) // 2
newZ = ((win + mid) * 100) // (total + mid)
if newZ > nowZ:
result = mid
high = mid -1
else:
low = mid + 1
return result
total, win = map(int,input().split())
num = binary_search(total, win)
print(num)
ํ์ด๊ณผ์ : ์๋ฅผ ์ ๋ ฅ ๋ฐ์ ํ ์ด๋ถ ํ์ ํจ์๋ก ๋ณด๋ธ๋ค
๋ฌธ์ ์ ์ ํ ์กฐ๊ฑด๋๋ก low๋ 1, high๋ 10^9๋ก ์ก์ ๋ค, newZ๊ฐ nowZ๋ณด๋ค ์ปค์ง ๋๊น์ง ์ค์๊ฐ์ ์ฎ๊ฒจ๊ฐ๋ฉฐ ํ์ํ๋ค. ์ด๋ ์๊ฐ ๋ณต์ก๋๋ O(logN)์ด ๋๋ค
์ฐธ๊ณ ๋ธ๋ก๊ทธ: https://velog.io/@kimdukbae/์ด๋ถ-ํ์-์ด์ง-ํ์-Binary-Search
๋ฐฐ์ด ์ : ์ด๋ถ ํ์์ ์ ์ฉ์ํค๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ ๋ค.
์ฒ์์ nowZ ์ฐ์ฐ์ int((win / total) * 100)์ ์ฌ์ฉํ๋๋ฐ, ์์ ์ ์ถ๋ ฅ์๋ ๋ฌธ์ ๊ฐ ์์ด๋ ๋ฐฑ์ค ๋ด๋ถ์์ ํ๋ฆผ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ์ ๊ณ ์ณค๋ค.
0925

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int func(const vector <int>& arr){
if (arr.empty()) return 0;
int maxSum = arr[0];
int pMax = arr[0];
for(size_t i = 1;i < arr.size(); i++){
pMax = max(pMax + arr[i], arr[i]);
maxSum = max(maxSum, pMax);
}
return maxSum;
}
int main(void) {
int num(0), inputNum(0);
vector <int> arr;
cin >> num;
for(int i = 0; i < num; i++){
cin >> inputNum;
arr.push_back(inputNum);
}
cout << func(arr);
return 0;
}
ํ์ด๊ณผ์ : ๋จ์ํ๊ฒ ์ ๋ ฅ๋ฐ์ ๊ฐ์ ์ต๋ ๋ถ๋ถํฉ์ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ์ ์์ฑํ๋ฉด ๋๋ค
- pMax : ์ง์ ๊น์ง์ ๋ถ๋ถํฉ์ ํ์ฌ ์์๋ฅผ ์ด์ด๋ถ์ด๋ ๊ฒฝ์ฐ
- arr[i] : ์๋ก์ด ๋ถ๋ถํฉ์ ํ์ฌ ์์์์๋ถํฐ ์๋ก ์์ํ๋ ๊ฒฝ์ฐ
๋ฅผ ๋น๊ตํด ๊ณ์ ๊ฐฑ์ ํ๋ฉด ๋๋ค
์ฐธ๊ณ ๋ธ๋ก๊ทธ: https://velog.io/@foreknowledge/๋ฐฐ์ด์-์ต๋-๋ถ๋ถํฉ-์๊ณ ๋ฆฌ์ฆ-Maximum-subarray-problem
๋ฐฐ์ด ์ : ์ต๋ ๋ถ๋ถํฉ ์๊ณ ๋ฆฌ์ฆ์ ์ฒ์ ์ฌ์ฉํด๋ดค๋ค
0926

num = int(input())
stack = []
for i in range(num):
str = input()
truth = False
for j in range(len(str)):
if str[j] == '(':
stack.append('(')
elif str[j] == ')':
if len(stack) == 0:
stack.append(')')
break
if len(stack) != 0:
stack.pop()
if len(stack) == 0:
truth = True
stack = []
print('YES') if truth == True else print('NO')
ํ์ด๊ณผ์ : ์คํ์ ์ฑ์ง์ ์ฌ์ฉํ์ฌ ํ์๋ค. ‘(’ ์ ๋ ฅ์ด ๋ค์ด์ฌ ๋๋ง ์คํ์ ๊ฐ์ ์ถ๊ฐํ๋ค. ๋ง์ผ ‘)’ ๊ฐ์ด ๋ค์ด์จ๋ค๋ฉด, ์คํ์์ ๊ฐ์ ์ง์ด๋ค(๋จ, ์คํ์ด ๋น์ด์์ ๊ฒฝ์ฐ ์คํ์ ๊ฐ์ ์ถ๊ฐํ ๋ค ๋ฐ๋ณต๋ฌธ์ ํ์ถํ๋ค) ์ ๋ ฅ๊ฐ์ ํ์ํ๋ ๊ณผ์ ์ด ๋๋ฌ๋ค๋ฉด ์คํ์ ๋ช ๊ฐ์ ๊ฐ์ด ๋ค์ด์๋์ง ํ์ธํ๋ค. ์คํ์ ๊ฐ์ด 0์ด๋ผ๋ฉด ‘(’์ด ์ ๋ ฅ๋ ํ์==’)’์ด ์ ๋ ฅ๋ ํ์์ด๊ธฐ์ ์ด ๊ฒฝ์ฐ์๋ง YES๋ฅผ ์ถ๋ ฅํ๋ค. ์ดํ ์คํ์ ๋ค์ ๋น์ด๋ค
๋ฐฐ์ด ์ : ์คํ์ ์ ์ฐํ๊ฒ ์ด์ฉํ๋ ๋ฐฉ๋ฒ์ ์๊ฒ ๋์๋ค. ๋ง์ง๋ง์ ์คํ์ ๋ค์ ๋น์์ฃผ๋ ๊ฒ์ ์๊ฐํด๋ด์ง ๋ชปํด ์๊พธ ์๋ฑํ ๋ต๋ณ์ด ๋์๊ธฐ์, ์ด๋ฐ ์ฌ์ํ ์ ๋ค์ ์ ์ฒดํฌํด์ผ ํ ๊ฒ ๊ฐ๋ค
'๊ณต๋ถ > ์ฝ๋ฉ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์์ฃผ ์ ์ฉํ๊ณ ํธ๋ฆฌํ ์๋ ์์ผ ์ ๋ ฅ ํ๋ก๊ทธ๋จ (0) | 2025.12.08 |
|---|---|
| ์๊ณ ๋ฆฌ์ฆ ์คํฐ๋ 2์ฃผ์ฐจ ๊ฒฐ์ฐ (0) | 2025.10.05 |
| [C++] ๋ฒกํฐ๋ฅผ ์ง์ ์ ์ํด๋ณด์! (0) | 2025.08.20 |
| ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋์ Big-O ํ๊ธฐ๋ฒ (1) | 2025.08.18 |
| [C++] vector์ pair ์ฐ์ต (2) | 2025.08.17 |