https://school.programmers.co.kr/learn/courses/30/lessons/12973?language=javascript코드function solution(s) { const stack = []; for (const ch of s) { if (stack.length > 0 && ch === stack[stack.length - 1]) { stack.pop(); } else { stack.push(ch); } console.log(stack); } return stack.length === 0 ? 1 : 0;} 처음에는 배열의 마지막 요소에 접근할 때 파이썬처럼 stack[-1]처럼 했다가 틀렸다....찾아보니 자바스크립트는 길이-..
문제풀이/자료구조
https://school.programmers.co.kr/learn/courses/30/lessons/12909?language=javascript코드function solution(s) { let answer = true; const stack = []; for (const ch of s) { if (ch === "(") { stack.push("("); } else { if (stack.length > 0) { stack.pop(); } else { answer = false; break; } } } if (stack.length > 0) { answer = false; } return ans..
https://www.acmicpc.net/problem/11652코드from collections import defaultdictn = int(input())nums_dict = defaultdict(int)for _ in range(n): nums_dict[int(input())] += 1sorted_nums = sorted(nums_dict.items(), key=lambda x: (-x[1], x[0]))print(sorted_nums[0][0]) 처음에는 가장 많이 나온 수가 여러 개면 작은 수를 출력한다는 조건을 제대로 고려 안해서 틀렸는데, 정렬의 key 속성을 이용해서 처리했다.
https://www.acmicpc.net/problem/13335코드const fs = require("fs");const filePath = process.platform === "linux" ? "dev/stdin" : "./input.txt";const [n, w, l, ...arr] = fs .readFileSync(filePath) .toString() .trim() .split(/\s/) .filter((item) => item != "") .map((item) => +item);class TruckNode { constructor(weight) { this.next = null; this.weight = weight; this.dist = 0; }}class ..
https://www.acmicpc.net/problem/1374코드from heapq import heappop, heappushimport sysinput = sys.stdin.readlinen = int(input())lectures = []for _ in range(n): _, start, end = map(int, input().split()) lectures.append((start, end))lectures.sort()needs = []for i in range(n): start, end = lectures[i] if needs and needs[0] 이 문제는 heap 자료구조를 잘 알고 사용하는 것이 중요한 문제였다.1. 강의 정보 정렬 1-1. 각 강의의 시작 ..