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 ..
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..