본문 바로가기

0

(102)
Minimum Increment to Make Array Unique (in Python) Problem Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1.Return the least number of moves to make every value in A unique. Example 1:Input: [1,2,2] Output: 1 Explanation: After 1 move, the array could be [1, 2, 3]. Example 2:Input: [3,2,1,2,1,7] Output: 6 Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7]. It can be shown with 5 or less ..
Climbing Stairs (in Python) ProblemYou are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Example 1:Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example 2:Input: 3 Output: 3 Explanation: There are three ways to climb..
Add Binary (in Python) Problem Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a = "11", b = "1" Output: "100"Example 2:Input: a = "1010", b = "1011" Output: "10101" Solution 123456789101112131415161718192021222324class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ # ans = in..
Rotate Image (in Python) ProblemYou are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.Example 1:Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2..
Permutations (in Python) Problem Given a collection of distinct integers, return all possible permutations.Example:Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] Solution •종료 조건 : index가 현재 배열의 길이와 같을 때.•i번째 원소와 현재 선택한 원소(first)를 바꾸고•i+1번째 원소부터 다시 탐색을 시작한다.•i번째 원소를 다시 돌려 놓는다. (다음 탐색에 영향을 주지 않기 위해서.) 123456789101112131415161718192021222324class Solution: def permute(self, nums): """ :type..
Spiral Matrix (in Python) ProblemGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1:Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2:Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Solution 12345678910111213141516171819202122232425262728293031323334353637class Solution: def spiral..
Find First and Last Position of Element in Sorted Array (in Python) ProblemGiven an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found in the array, return [-1, -1]. Example 1:Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4]Example 2:Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] SolutionAppr..
Next Permutation (in Python) Problem Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place and use only constant extra memory. Here are some examples. Inputs are in the left-hand column and its corresponding out..