Algorithm & 자료구조

    151. Reverse Words in a String

    Reverse Words in a String - LeetCode Can you solve this real interview question? Reverse Words in a String - Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a strin leetcode.com 내 풀이 /* [문제] - 문자열 s가 주어졌을 때, 단어들의 순서를 뒤집어라 - 단어들은 공백을 기준으로 나뉘고, 공백을 기점으로 뒤집어야함 - s가 주어질 때 ..

    [Leetcode] 345. Reverse Vowels of a String

    Reverse Vowels of a String - LeetCode Can you solve this real interview question? Reverse Vowels of a String - Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than onc leetcode.com Given a string s, reverse only all the vowels in the string and return it. The vowels are ..

    [Leetcode] 605. Can Place Flowers

    Can Place Flowers - LeetCode Can you solve this real interview question? Can Place Flowers - You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots. Given an integer array flowerbed containing 0's and 1' leetcode.com You have a long flowerbed in which some of the plots are planted, and some are not. However, flower..

    [Leetcode] 1431. Kids With the Greatest Number of Candies

    Kids With the Greatest Number of Candies - LeetCodeCan you solve this real interview question? Kids With the Greatest Number of Candies - There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandieleetcode.comThere are n kids with candies. You are given an integer array candies, where ea..

    [Leetcode] 1071. Greatest Common Divisor of Strings

    Greatest Common Divisor of Strings - LeetCodeCan you solve this real interview question? Greatest Common Divisor of Strings - For two strings s and t, we say "t divides s" if and only if s = t + ... + t (i.e., t is concatenated with itself one or more times). Given two strings str1 and str2, return tleetcode.comFor two strings s and t, we say "t divides s" if and only if s = t + ... + t (i.e., t..

    [Leetcode] 1768.Merge Strings Alternately

    Merge Strings Alternately - LeetCodeCan you solve this real interview question? Merge Strings Alternately - You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional leleetcode.comYou are given two strings word1 and word2. Merge the strings by adding letters in alternating ..

    [알고리즘 JS] Sliding window - 중복이 없는 가장 긴 문자열의 길이 찾기

    주어진 문자열에서 중복이 없는 가장 긴 길이를 구하는 문제이다. 처음에는 부분 문자열을 매번 구하고 해당 부분 문자열에 현재 문자열이 존재하는지를 찾는 방법이 떠올랐다. 이 경우 시간 복잡도가 O(n^2) 이므로 상당히 비효율적이다. 이 때 슬라이딩 윈도우 기법을 사용하면 문자열 길이만큼 반복하여 O(n) 의 선형 시간 복잡도로 문제를 해결할 수 있다. [findLongestSubstring] Write a function called findLongestSubstring, which accepts a string and returns the length of the longest substring with all distinct characters. ex) findLongestSubstring(''),..

    242. Valid Anagram

    https://leetcode.com/problems/valid-anagram/description/ var isAnagram = function(s, t) { if(s.length !== t.length) return false; const sMap = {}; const tMap = {}; [...s].forEach(key => sMap[key] = (sMap[key] || 0) + 1 ); [...t].forEach(key => tMap[key] = (tMap[key] || 0) + 1 ); for(let sKey of Object.keys(sMap)) { if(!tMap[sKey]) { return false; } if(tMap[sKey] !== sMap[sKey]) { return false; }..

    [알고리즘JS] 피로도 (프로그래머스 lv.2)

    문제 https://school.programmers.co.kr/learn/courses/30/lessons/87946 풀이 DFS 를 이용한 완전탐색으로 접근하면 풀 수 있는 문제. 처음에는 이중 반복문으로 탐색하려고 했는데, 순열 문제임을 알았음에도 반복문을 써서 정답을 도출할 수 없었다. 이중 반복문을 사용하면, 0-1-2, 1-0-1, 2-0-1 처럼 정해진 순서밖에 돌지 못하기 때문에 DFS 로 접근해야 한다. function solution(k, dungeons) { let answer = []; const N = dungeons.length; const visited = new Array(N).fill(0); const DFS = (fatigue, count) => { answer.push(..

    [알고리즘JS] 연속 부분 수열 합의 개수 (프로그래머스 lv.2)

    [알고리즘JS] 연속 부분 수열 합의 개수 (프로그래머스 lv.2)

    문제 https://school.programmers.co.kr/learn/courses/30/lessons/131701 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 풀이 슬라이딩 윈도우와 Set 객체를 활용한 풀이 슬라이딩 윈도우는 아래와 같이 지정된 범위를 가지고 한 칸씩 움직이는 풀이 방법이다. 하나의 반복에서 다음 범위로 넘어가기 위해 맨 앞 요소를 빼고 맨 뒤 요소를 더한다. function solution(elements) { const set = new Set(); const N = elements.length; // 1 ~ n 길이까지의 ..