본문 바로가기

Algorithm

(2139)
(C++) - LeetCode (easy) 20. Valid Parentheses https://leetcode.com/problems/valid-parentheses/ Valid Parentheses - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 자료구조를 이용한 문제였습니다. 📕 풀이방법 📔 풀이과정 stack st를 선언해줍니다. 여러 경우를 생각해줍니다. 1. 여는 괄호 '(', '{', '[' 가 나오면 st에 push 해줍니다. 2. 닫는 괄호 ')', '}', ']'가 나온다면 두 경우가 있습니다. 2-1. stack에 원소가..
(C++) - LeetCode (easy) 14. Longest Common Prefix https://leetcode.com/problems/longest-common-prefix/ Longest Common Prefix - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 전수조사 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 strs를 문자열의 길이가 짧은 순으로 정렬해줍니다. 정답을 반환할 ans를 선언해줍니다. 📔 풀이과정 strs[0]이 가장 짧은 문자열이므로 이 size만큼이 최대 prefix입니다. 이에 대해 for loop를 수행합니다...
(C++) - LeetCode (easy) 976. Largest Perimeter Triangle https://leetcode.com/problems/largest-perimeter-triangle/ Largest Perimeter Triangle - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 간단 구현 문제였습니다. 📕 풀이방법 📔 풀이과정 10^4개가 nums의 최대 크기이므로 세 변을 결정하도록 모든 원소를 확인하는 brute force나 두 변을 고정하고 두 번째 큰 값의 lower_bound를 구하는 이분 탐색으로는 시간초과가 나게 됩니다. 따..
(C++) - LeetCode (easy) 13. Roman to Integer https://leetcode.com/problems/roman-to-integer/ Roman to Integer - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 간단 분기 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 각 symbol에 맞는 수를 반환하기 위한 map변수 symbolMap을 선언 후 초기화해줍니다. 📔 풀이과정 for loop를 수행하며 문자열 s를 확인해줍니다. 매 loop당 3 조건에 대해 고려해줍니다. 바로 다음 문자만 확인하면 ..
(C++) - 백준(BOJ) 3533 : Explicit Formula https://www.acmicpc.net/problem/3533 3533번: Explicit Formula Consider 10 Boolean variables x1, x2, x3, x4, x5, x6, x7, x8, x9, and x10. Consider all pairs and triplets of distinct variables among these ten. (There are 45 pairs and 120 triplets.) Count the number of pairs and triplets that contain at least one variab www.acmicpc.net 간단 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 x1 ~ x10까지 선언해줍니다. 📔 정답출력 공식의 결과..
(C++, Rust) - 백준(BOJ) 13236 : Collatz Conjecture https://www.acmicpc.net/problem/13236 13236번: Collatz Conjecture The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937 and is still an open problem in mathematics. The sequence of numbers involved is referred to as the hailstone sequence or hailstone numbers (b www.acmicpc.net 간단 구현 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 초기 숫자 n을 선언해 줍니다. 📔 풀이과정 조건에 따..
(C++) - 백준(BOJ) 25704 : 출석 이벤트 https://www.acmicpc.net/problem/25704 25704번: 출석 이벤트 쇼핑몰에서 30일간 출석 이벤트를 진행한다. 쇼핑몰의 사이트를 방문하면 1일 1회 출석 도장을 받을 수 있고, 출석 도장을 여러 개 모아서 할인 쿠폰으로 교환할 수 있다. 출석 도장의 개수에 따 www.acmicpc.net 간단 산수 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 출석 도장 개수 n, 가격 p, 정답 ans 선언 후 입력받습니다. 📔 풀이과정 조건에 따라 최솟값을 반환하는 함수 getSaledPrice를 수행합니다. 함수의 반환값을 ans에 저장합니다. 📔 정답출력 0 미만인 경우 정답은 0이므로 이를 고려해 정답을 출력합니다. 📕 Code 📔 C++ #include using namespace..
(C++) - 백준(BOJ) 25703 : 포인터 공부 https://www.acmicpc.net/problem/25703 25703번: 포인터 공부 용모는 오늘 객체지향프로그래밍1 시간에 포인터에 대해 배웠다. 포인터란, 프로그래밍 언어에서 다른 변수, 혹은 그 변수의 메모리 공간주소를 가리키는 변수를 의미한다. C/C++에서 포인터는 ( www.acmicpc.net 간단한 출력 문제였습니다. 📕 풀이방법 📔 입력 및 초기화 반복할 변수를 선언 후 입력받습니다. 📔 정답출력 형식에 맞춥니다. 📕 Code 📔 C++ #include using namespace std; int n; int main(){ cin >> n; cout