본문 바로가기

전체 글

(113)
ZigZag Conversion (in Python) Problem The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this :P A H N A P L S I I G Y I RAnd then read line by line : "PAHNAPLSIIGYIR" Solution Approach 1 : Sort by Row IntuitionBy iterating through the string from left to right, we can easily determine which row in the Zig-Zag pattern that a character belongs to. Algorithm1. We can use min(numRows, len(..
Longest Palindromic Substring (in Python) Problem Given a string s, find the longest palindromic substring in s. you may assume that the maximum length of s is 1000. Solution Make sure you understand what a palindrome means. A palindrome is a string which reads the same in both directions. For example, S="aba" is a palindrome, S="abc" is not. Approach 1 : Brute Force The obvious brute force solution is to pick all possible starting and ..
Median of Two Sorted Arrays (in Python) Problem There are two sorted arrays num1 and num2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log(m+n)). You may assume nums1 and nums2 cannot be both empty. Solution Approach 1 : Sort and find My first approach was just put them together and find median. It costs same time with the second approach on Leetcode. But it will wa..
What does python's sort use? The answer is Timsort. Definition•A hybrid stable sorting algorithm, derived from merge sort and insertion sort.•Designed to perform well on many kinds of real-world data.•It was implemented by Tim Peters in 2002 for use in the Python programming language.•The algorithm finds subsequences of the data that are already ordered, and uses that knowledge to sort the remainder more efficiently.•This i..
Longest Substring Without Repeating Characters (in Python) Problem Given a string, find the length of the longest substring without repeating characters. Solution Approach 1 : Brute Force IntuitionCheck all the substring one by one to see if it has no duplicate character. Algorithm def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ if len(s) == 0 : return 0 result = 1 for i in range(len(s)) : for j in range(i+1, len(s)) : if s[j] in..
Add two numbers (in Python) Problem You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Solution Approach 1 : Elementary Math Intuition Keep track of the carry using ..
Two Sum (in Python) Problem Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same element twice. Solution Approach 1 : Brute Force The brute force approach is simple.Loop through each element x and find if there is another value that equals to target - x. def twoSum(nums, t..
우선순위 큐 (priority queue), 힙 정렬 (heap sort) 우선순위 큐 (priority queue)- 데이터들이 우선순위를 가지고 있고 우선순위가 높은 데이터가 먼저 나가게 된다.- 배열, 연결 리스트 등의 여러 가지 방법으로 구현이 가능한데, 가장 효율적인 구조는 heap이다. 배열을 사용하는 방법 1. 정렬이 안 되어 있는 배열의 경우삽입 시간 복잡도 \(O(1)\)삭제 시간 복잡도 \(O(n)\) 2. 정렬이 되어 있는 배열의 경우삽입 시간 복잡도 \(O(n)\)삭제 시간 복잡도 \(O(1)\) 연결 리스트를 사용하는 방법 1. 정렬이 안된 리스트- 삽입 시에 첫번째 노드로 삽입시키는 것이 유리하다. 다른 노드를 이동할 필요가 없다. 포인터만 변경하면 된다. 삽입 시간 복잡도 \(O(1)\)- 삭제 시에 포인터를 따라서 모든 노드를 뒤져보아야 한다. 삭제..