๊ณต๋ถ€/์ฝ”๋”ฉ

[์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋””] 1์ฃผ์ฐจ ๊ฒฐ์‚ฐ

sourceoftax 2025. 9. 27. 21:17

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.")

ํ’€์ด๊ณผ์ • :

  1. ๊ธฐ๋ณธ ํ•จ์ˆ˜ func: ์ž…๋ ฅ๋ฐ›์€ ๋น„๋ฐ€๋ฒˆํ˜ธ์˜ ํ—ˆ์šฉ/๋น„ํ—ˆ์šฉ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค. ํ•ด๋‹น ํ•จ์ˆ˜์—์„œ ์ง์ ‘์ ์œผ๋กœ ํŒ๋‹จํ•˜๋Š” ์กฐ๊ฑด์€ ‘๋ชจ์Œ์ด ํฌํ•จ๋˜์—ˆ๋Š”๊ฐ€?’๋งŒ์ด๋‹ค
  2. ์ด์ค‘ ๋ฐ˜๋ณต ํ™•์ธ ํ•จ์ˆ˜ repeat: ํ—ˆ์šฉ๋˜๋Š” ๋ชจ์Œ์„ ๋ฏธ๋ฆฌ ์„ค์ •ํ•œ ๋’ค, ๋ฌธ์ž์—ด์˜ ์ฒซ๋ฒˆ์งธ ๊ธ€์ž(prev)๋ฅผ ์ง€์ •ํ•ด ๋’ค ๊ธ€์ž์™€ ๋น„๊ตํ•œ๋‹ค. ์ดํ›„ ์‹œํ–‰์ด ๋๋‚˜๋ฉด prev๋Š” ๋‹ค์Œ ๊ธ€์ž๋กœ ์˜ฎ๊ธด๋‹ค. ๋งŒ์•ฝ ์ค‘๊ฐ„์— ์ž˜๋ชป๋œ๊ฒŒ ์—†์œผ๋ฉด True๋ฅผ returnํ•œ๋‹ค.
  3. ์‚ผ์ค‘ ๋ฐ˜๋ณต ํ™•์ธ ํ•จ์ˆ˜ 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;
}

ํ’€์ด๊ณผ์ • : ๋‹จ์ˆœํ•˜๊ฒŒ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์˜ ์ตœ๋Œ€ ๋ถ€๋ถ„ํ•ฉ์„ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค

  1. pMax : ์ง์ „๊นŒ์ง€์˜ ๋ถ€๋ถ„ํ•ฉ์— ํ˜„์žฌ ์›์†Œ๋ฅผ ์ด์–ด๋ถ™์ด๋Š” ๊ฒฝ์šฐ
  2. 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๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ดํ›„ ์Šคํƒ์„ ๋‹ค์‹œ ๋น„์šด๋‹ค

๋ฐฐ์šด ์ : ์Šคํƒ์„ ์œ ์—ฐํ•˜๊ฒŒ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๋งˆ์ง€๋ง‰์— ์Šคํƒ์„ ๋‹ค์‹œ ๋น„์›Œ์ฃผ๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•ด๋‚ด์ง€ ๋ชปํ•ด ์ž๊พธ ์—‰๋šฑํ•œ ๋‹ต๋ณ€์ด ๋‚˜์™”๊ธฐ์—, ์ด๋Ÿฐ ์‚ฌ์†Œํ•œ ์ ๋“ค์„ ์ž˜ ์ฒดํฌํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค