본문 바로가기

00/algorithm

(55)
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..
Swap Nodes in Pairs (in Python) ProblemGiven a linked list, swap every two adjacent nodes and return its head. Example:Given 1->2->3->4, you should return the list as 2->1->4->3.Note:Your algorithm should use only constant extra space.You may not modify the values in the list's nodes, only nodes itself may be changed. Solution •current 노드를 저장해뒀다가 다음 라운드에서 next 변경해줘야함. 123456789101112131415161718192021222324252627class Solution..
Generate Parentheses (in Python) Problem GIven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n=3, a solution set is:[ "((()))", "(()())", "(())()", "()(())", "()()()" ] Solution Approach 1 : Brute Force Complexity Analysis•Time Complexity : \(O(2^{2n}*n)\). For each of \(O(2^{2n})\) sequences, we need to create and validate the sequence, which takes \(O(n)\) ..
Valid Parentheses (in Python) Problem Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets.2. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1:Input: "()" Output: true Example 2:Input: "()[]{}" Output: true E..
Remove Node From End of List (in Python) Problem Given a linked list, remove the n-th node from the end of list and return its head. Note:Given n will always be valid. Example:Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Solution Approach 1 : Two pass algorithmRemove the (L-n+1)th node from the beginning in the list, where L is the list length. •while문을 이용..
Letter Combinations of a Phone Number (in Python) Problem Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Example:Input: "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].Note :Although the above answer is in lexicographical orde..
3Sum (in Python) ProblemGiven an array nums of n integers, are there elements a, b, c in nums such that a+b+c=0? Find all unique triplets in the array which gives the sum of zero.Note :The solution set must not contain duplicate triplets.Example:Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Solution •배열 정렬.•i를 기준으로 left boundary와 right boundary를 설정.1. 세 수의 합이 0보다 작을 경우 ..