๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/Leetcode

(19)
[Leetcode] Group Anagrams ํ•ด์„ค ์ฝ”๋“œ class Solution(object): def groupAnagrams(self, strs): d = {} for str in strs: key = ''.join(sorted(str)) if key not in d: d[key] = [str] else: d[key].append(str) return d.values() ๋ถ„์„ > ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ํ’€๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, value๊ฐ’์— ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ถ”๊ฐ€๊ฐ€ ์•ˆ๋˜์–ด์„œ ๊ณ„์† ํ’€์ง€ ๋ชปํ–ˆ๋‹ค. ์• ๋„ˆ๊ทธ๋žจ ๊ด€๊ณ„์— ์žˆ๋Š” ๋‹จ์–ด๋“ค์€ ์ •๋ ฌํ–ˆ์„ ๋•Œ ๊ฐ™๋‹ค๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ ํ•ด์‹œ๋งต์˜ key๊ฐ’์—๋Š” ์ด๋“ค์„ ์ •๋ ฌํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“  ๊ฒƒ์„ ๋„ฃ๊ณ , value๊ฐ’์—๋Š” ๊ฐ™์€ key๋ฅผ ๊ฐ€์ง„ ๋‹จ์–ด๋“ค์„ ๋ฐฐ์—ด๋กœ appendํ•˜๋ฉด ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํ•ด์‹œ๋งต์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์—ˆ๋‹ค. Group Anagrams - LeetCod..
[Leetcode] Last Moment Before All Ants Fall Out of a Plank (#1503) ๋ฌธ์ œ We have a wooden plank of the length n units. Some ants are walking on the plank, each ant moves with a speed of 1 unit per second. Some of the ants move to the left, the other move to the right. When two ants moving in two different directions meet at some point, they change their directions and continue moving again. Assume changing directions does not take any additional time. When an ant ..
[Leetcode] Validate Stack Sequences (#946) ๋ฌธ์ œ Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise. Example 1: Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] Output: true Explanation: We might do the following sequence: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() ->..
[Leetcode] Create Target Array in the Given Order (#1389) ๋ฌธ์ œ Given two arrays of integers nums and index. Your task is to create target array under the following rules: Initially target array is empty. From left to right read nums[i] and index[i], insert at index index[i] the value nums[i] in target array. Repeat the previous step until there are no elements to read in nums and index. Return the target array. It is guaranteed that the insertion operati..
[Leetcode] Clumsy Factorial (#1006) ๋ฌธ์ œ The factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We make a clumsy factorial using the integers in decreasing order by swapping out the multiply operations for a fixed rotation of operations with multiply '*', divide '/', add '+', and subtract '-' in this order. For exampl..
[Leetcode] Print Words Vertically (#1324) ๋ฌธ์ œ Given a string s. Return all the words vertically in the same order in which they appear in s. Words are returned as a list of strings, complete with spaces when is necessary. (Trailing spaces are not allowed). Each word would be put on only one column and that in one column there will be only one word. Example 1: Input: s = "HOW ARE YOU" Output: ["HAY","ORO","WEU"] Explanation: Each word is ..
[Leetcode] Reshape the Matrix (#566) ๋ฌธ์ œ In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data. You are given an m x n matrix mat and two integers r and c representing the number of rows and the number of columns of the wanted reshaped matrix. The reshaped matrix should be filled with all the elements of the original matrix in the sa..
[Leetcode] Complex Number Multiplication (#537) ๋ฌธ์ œ A complex number can be represented as a string on the form "real+imaginaryi" where: real is the real part and is an integer in the range [-100, 100]. imaginary is the imaginary part and is an integer in the range [-100, 100]. i2 == -1. Given two complex numbers num1 and num2 as strings, return a string of the complex number that represents their multiplications. Example 1: Input: num1 = "1+1..