본문 바로가기

Algorithm

(20)
3. Longest Substring Without Repeating Characters Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the ans..
[Leetcode] 443. String Compression Given an array of characters chars, compress it using the following algorithm: Begin with an empty string s. For each group of consecutive repeating characters in chars: If the group's length is 1, append the character to s. Otherwise, append the character followed by the group's length. The compressed string s should not be returned separately, but instead be stored in the input character array..
[Leetcode] 1197. Minimum Knight Moves In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0]. A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction. Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists. Example 1: ..
1283. Find the Smallest Divisor Given a Threshold Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold. Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5)..
[Leetcode] 1396. Design Underground System Implement the class UndergroundSystem that supports three methods: checkIn(int id, string stationName, int t) A customer with id card equal to id, gets in the station stationName at time t. A customer can only be checked into one place at a time. checkOut(int id, string stationName, int t) A customer with id card equal to id, gets out from the station stationName at time t. getAverageTime(string..
[Leetcode] 381. Insert Delete GetRandom O(1) - Duplicates allowed Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if present. getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of s..
big O vs big Ω vs big Θ O (Big O)학계에서 big-O 는 시간의 상한을 나타낸다. 배열의 모든 값을 출력하는 알고리즘은 O(N) 으로 표현할 수도 있지만, 이 외에 N 보다 큰 big-O 시간으로 표현할 수도 있다. 예를 들어, O(N2),O(N3),O(2N)O(N^2), O(N^3), O(2^N)O(N2),O(N3),O(2N) 도 모두 옳은 표현이다. 다시 말해 실제 알고리즘의 수행시간은 적어도 이들 중 하나보다 빠르기만 하면 된다.Ω (Big Omega)학계에서 Ω 는 하한을 나타낸다. 해당 알고리즘은 Ω 수행 시간보다 빠를 수 없다.Θ (Big Theta)학계에서 Θ 는 O 와 Ω 둘다를 의미한다. 즉, 어떤 알고리즘의 수행시간이 O(N) 이면서 Ω(N) 이라면, 이 알고리즘의 수행 시간을 Θ(N) 으로 표현할 수..
[Leetcode] 532. K-diff Pairs in an Array Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array. A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true: 0
[Leetcode] 3. Longest Substring Without Repeating Characters Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the ans..
[Leetcode] 518. Coin Change 2 You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin. Example 1: Input: amount = 5, coins = [1, 2, 5] Output: 4 Explanation: there are four ways to make up the amount: 5=5 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 Example 2: Input: amount =..