Algorithm/LeetCode
[LeetCode] Shuffle the Array
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn]. Return the array in the form [x1,y1,x2,y2,...,xn,yn]. Time Complexity -> step 어떻게 step을 줄일 수 있을까? 직관적으로 떠오르는 것은 int[]을 두 개 만들어서 옮겨 담고 다시 합친것을 return하는것 O(N) + O(N) O(2N) => O(N) 변수를 하나 더 둬서 n번에 해결했다. Memory Complexity class Solution { public int[] shuffle(int[] nums, int n) { int[] result = new int[n*2]; int ..
[LeetCode] Running Sum of 1d Array
정수 배열 nums의 자리수까지의 모든 합의 값을 넣은 배열을 반환하라 Example 1: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. Example 2: Input: nums = [1,1,1,1,1] Output: [1,2,3,4,5] Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]. Example 3: Input: nums = [3,1,2,10,1] Output: [3,4,6,16,17] import java.util.*; class ..
[LeetCode] Check if Word Equals Summation of Two Words
한동안 멘탈이 안 좋았더니 몸도 안좋아졌다. 다시 주5문제 풀이를 시작하자. Easy~한 문제부터 즈려밟고 기획해 놓은 핫이슈 프로젝트를 7월말~8월초를 목표로 배포전 테스트완료까지 해봅시다 오늘의 문제! a부터 j까지의 알파벳을 각 0~9까지의 숫자로 대입하여 주어지는 세 개의 문자열 firstWord, secondWord를 더한 값이 targetWord라면 true 아니라면 false를 반환하는 문제이다 어떻게 접근할까 아스키 코드표를 이용하는 것이 좋겠다 -> 그냥 빼버리자 그렇다면 문자열을 하나씩 순회하며 값을 만들고 각 value를 더한 값이 targetWord인지 확인하자 import java.util.*; class Solution { public boolean isSumEqual(Strin..
[Quiz] Find-Eventual-Safe-States
2021.05.18 - [개발 공부/코딩 테스트] - [LeetCode] - Find Eventual Safe States 코테스터디에서 위 문제를 풀기위해 퀴즈를 내주셨다 Go로 적어주셨다 func eventualSafeNodes(graph [][]int) []int { result := []int{} for i, _ := range graph{ compare := 0 stack := []int{} check := i for len(graph[check]) != 0 || len(stack) > 0 { //len( list ) -> list의 갯수. compare += 1 stack = append(stack, graph[check]...) // graph[check]의 경우 리스트여서 리스트 + 리스트..
[LeetCode] - Find Eventual Safe States
cycle nodes와 safe nodes를 구분하여 safe nodes를 오름차순으로 반환하라 class Solution { public List eventualSafeNodes(int[][] graph) { /* n == graph.length 1 5 idx :3 items -> 0 idx :4 items -> 5 idx :5 items -> idx :6 items ->
[LeetCode] - Most Common Word
문자열 변수 paragraph와 문자열 배열 banned가 주어진다. paragraph에서 banned에 포함되지 않는 가장 자주 나오는 문자를 반환하시오. 답은 고유하며 한 단어를 보장한다. paragraph의 단어들은 대소문자의 구분하지 않으며 답은 소문자로 반환하시오. /* 생각해보자. paragraph는 space를 기준으로 단어를 나눈다. 그렇다면 일단 단어를 공백을 기준으로 나눠 단어의 배열을 만들자. 거기서 banned에 포함된 단어를 삭제 남은 단어들 중에서 가장 빈번하게 나오는 단어를 소문자로 리턴 1. 소문자로 2. 단어를 공백 기준으로 나눠 단어 배열만들기 3. banned 단어 제외시키기 4. 가장 빈번한 단어 고르기 5. 리턴 */ class Solution { public Str..