알고리즘

Programming/Algorithm

[Programmer Weekly Challenge #1] 부족한 금액 계산하기

문제 설명 새로 생긴 놀이기구는 인기가 매우 많아 줄이 끊이질 않습니다. 이 놀이기구의 원래 이용료는 price원인데, 놀이기구를 N 번 째 이용한다면 원래 이용료의 N배를 받기로 하였습니다. 즉, 처음 이용료가 100이었다면 2번째에는 200, 3번째에는 300으로 요금이 인상됩니다. 놀이기구를 count번 타게 되면 현재 자신이 가지고 있는 금액에서 얼마가 모자라는지를 return 하도록 solution 함수를 완성하세요. 단, 금액이 부족하지 않으면 0을 return 하세요. 제한사항 놀이기구의 이용료 price : 1 ≤ price ≤ 2,500, price는 자연수 처음 가지고 있던 금액 money : 1 ≤ money ≤ 1,000,000,000, money는 자연수 놀이기구의 이용 횟수 co..

Programming/Algorithm

[Queue문제] - ZigZagOrder

위와 같은 트리가 있을 때, [[1], [3, 2], [4, 5]] 의 결과를 출력하라 트리를 탐색하는데, 한번씩 방문 순서를 지그 재그 형태로 바꿔가면서 탐색을 해야 하는 문제이다. 일반적으로 트리를 preorder(전위순회)로 순회하지만, L, R 부분을 바꿔가면서 탐색하도록 해야 한다. 간단하게 boolean 타입 플래그를 두고, L와 R 노드를 방문하는 순서를 바꿔주면 된다. package inflearn.queue; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; class TreeNode { public int val; public TreeNode left;..

Programming/Algorithm

[Hackerrank] counting valleys - java

Hackkerank의 Interview prepare ket 중 Warm-up Challenges의 CountingValleys 문제 풀이 Java 코드 [정의] - 스텝은 해수면을 기준으로 시작 - 모든 걸음은 U, D로 기록됨 (U은 언덕을 올라갈 때, D는 내려갈 때) - 산의 정의: 해수면을 기준으로 위로 올라가고 다시 아래로 내려왔을 경우, 한 개의 산으로 판단. - 계곡의 정의: 해수면을 기준으로 아래로 내려가고 다시 위로 올라왔을 경우, 한 개의 계곡으로 판단. [문제] - 주어진 걸음 배열을 바탕으로, 지나간 계곡의 수를 구하여라. [전략] - 해수면을 기준으로 아래로 내려갔다가 올라온 경우만 카운팅 - Y 좌표계로 생각해서, 해수면(=기준점)을 좌표 0으로 정의 - U 일 땐 현재 좌표에..

Programming/Algorithm

[Hackerrank] JumpingOnClouds - java

Hackkerank의 Interview prepare ket 중 Warm-up Challenges의 JumpingOnClouds 문제 풀이 Java 코드 [제약] - 점프는 현재 현재 인덱스에서 1 또는 2 증가한 인덱스까지만 가능. - 점프 가능한 구름의 값은 0, 피해야 하는 구름의 값은 1. - 구름을 통과할 수 있는 최소 점프 수를 구하라. [전략] - 최소 점프 수 이므로, 뛸 수 있는 최대 거리(2)를 우선적으로 탐색 - 최소 점프가 2므로, 피해야 하는 구름은 연속으로 있을 수 없다. (ex: 0 1 1 ) public class JumpingOnTheCloud { // Complete the jumpingOnClouds function below. static int jumpingOnClo..

Programming/Algorithm

[Java-알고리즘] Two Sum

[Problem] Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. [Example1] Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. [Example2] Given nums = [3, 2, 4], target = 6, Because nums[1] + num..

Programming/Algorithm

[Java-알고리즘] RangeSumBinaraySearchTree

[Problem] Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. [Example1] Input: root = [10,5,15,3,7,null,18], L = 7, R = 15 Output: 32 [Example2] Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10 Output: 23 [Note] The number of nodes in the tree is at most 10..

Programming/Algorithm

[Java-알고리즘] toLowerCase Implemet

[Problem] Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. [Example1] Input: "Hello" Output: "hello" [Example2] Input: "here" Output: "here" [Example3] Input: "LOVELY" Output: "lovely" [Solution] import java.lang.StringBuilder; class Solution { public String toLowerCase(String str) { char[] characters = str.toCharArray(); int strLen = st..

Programming/Algorithm

[Java-알고리즘] Split a String in Balanced Strings

[Problem] Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced string s split it in the maximum amount of balanced strings. Return the maximum amount of splitted balanced strings. [Example1] Input: s = "RLRRLLRLRL" Output: 4 Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of 'L' and 'R'. [Example2] Input: s ..

JohnMark
'알고리즘' 태그의 글 목록