목록알고리즘 (547)
N
387. First Unique Character in a String Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. Example 1: Input: s = "leetcode" Output: 0 Example 2: Input: s = "loveleetcode" Output: 2 Example 3: Input: s = "aabb" Output: -1 Constraints: 1
15. 3Sum Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets. Example 1: Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Example 2: Input: nums = [] Output: [] Example 3: Input: nums = [0] Output: [] Constraints: 0
383. Ransom Note Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise. Each letter in magazine can only be used once in ransomNote. Example 1: Input: ransomNote = "a", magazine = "b" Output: false Example 2: Input: ransomNote = "aa", magazine = "ab" Output: false Example 3: Input: ransomNote = "aa", magazine = "aab" Output: true..
374. Guess Number Higher or Lower We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess. You call a pre-defined API int guess(int num), which returns 3 possible results: -1: The number I picked is lower than your guess (..
https://programmers.co.kr/learn/courses/30/lessons/49189?language=javascript 코딩테스트 연습 - 가장 먼 노드 6 [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] 3 programmers.co.kr 큐와 BFS를 이용한 문제 풀이. 우선 그래프를 그리기 위해 n + 1의 크기만큼 2차원 배열 graph를 선언. 노드의 방문 여부를 알기 위한 visited 배열을 선언. for문. 주어진 edge는 무방향 그래프이기 때문에 두 노드에 해당하는 번호를 각각 넣어줘야 한다. 큐 class를 만들어 bfs를 위한 큐를 만든다. 문제가 1부터 가장 먼 노드를 찾아야 하기 때문에 큐에 1을 넣어 시작(..
https://programmers.co.kr/learn/courses/30/lessons/42579 코딩테스트 연습 - 베스트앨범 스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다. 속한 노래가 programmers.co.kr 해시를 이용한 문제 풀이. gSum 객체는 각 장르의 총 플레이 횟수를 담는다. gIndex 객체는 각 장르의 곡마다 플레이 횟수와 인덱스를 담는다. 우선 각 장르 별 총 플레이 횟수와 인덱스를 알아야 한다. for문을 통해 genres와 plays의 원소에 접근. 만약 gSum 객체에서 genre를 key로 가지고 있지 않다면(undefined) 0으로 초기..
367. Valid Perfect Square Given a positive integer num, write a function which returns True if num is a perfect square else False. Follow up: Do not use any built-in library function such as sqrt. Example 1: Input: num = 16 Output: true Example 2: Input: num = 14 Output: false Constraints: 1
350. Intersection of Two Arrays II Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order. Example 1: Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2] Example 2: Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [4,9] Explanation: [9,4] is al..