Algorithm
Leetcode - Gas Station
Leetcode - Gas Station
2024.03.02Problem There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations. Given two integer arrays gas and cost, return the starting gas station's index if you ..
javascript - 괄호문자제거 (자료구조 stack and queue)
javascript - 괄호문자제거 (자료구조 stack and queue)
2021.08.14문제풀이 이문제는 스택과 큐를 이용해서 해결하는 문제다. 풀이에 사용된 방법은 닫는 괄호를 만나면 여는 괄호를 만날 때까지 pop을 시킨다. pop 되지 않은 문자열을 괄호 안에 없다고 판단하여 join 하여 return 한다. function solution(str) { const stack = []; for (x of str) { if (x === ")") { while (stack.pop() !== "("); } else stack.push(x); } return stack.join(""); } const str = "(A(BC)D)EF(G(H)(IJ)K)LM(N)"; console.log(solution(str)); Github https://github.com/JongyunHa/algorithm..
java - 두배열 합치기(Two pointer)
java - 두배열 합치기(Two pointer)
2021.07.27import java.util.ArrayList; import java.util.Scanner; public class CombineTwoArrays { public static ArrayList solution(int[] arr1, int[] arr2) { int p1 = 0, p2 = 0; ArrayList answer = new ArrayList(); while (p1 < arr1.length && p2 < arr2.length) { if (arr1[p1] < arr2[p2]) answer.add(arr1[p1++]); else answer.add(arr2[p2++]); } for (int i=p1; i
java - 격자판 최대합 (배열)
java - 격자판 최대합 (배열)
2021.07.25import java.util.Scanner; public class SumOfGrating { public static int solution(int n, int[][] grating) { int answer = Integer.MIN_VALUE; int sumRow, sumCol; for (int i = 0; i < n; i++) { sumRow=sumCol=0; for (int j = 0; j < n; j++) { sumRow += grating[i][j]; sumCol += grating[j][i]; } answer = Math.max(answer, sumRow); answer = Math.max(answer, sumCol); } int diagonalSum = 0, reverseDiagonalSu..