본문 바로가기

LeetCode84

LeetCode. Cheapest Flights Within K Stops Problem: There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w. Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no such route, output -1. -Summary- Using Dijkstra's Algorithm -Debugging- leetcode.com/problems/ch.. 2020. 6. 15.
LeetCode. Count Primes [Math] Problem: Given an integer, write a function to determine if it is a power of three. Example 1: Input: 27 Output: true Example 2: Input: 0 Output: false Example 3: Input: 9 Output: true Example 4: Input: 45 Output: false Follow up: Could you do it without using any loop / recursion? -Summary- 1. Check if n is bigger than 0 for when input is 0 2. Check if given n can divide the maximum number for .. 2020. 6. 15.
LeetCode. Count Primes [Math] Problem: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. -Summary- Used Sieve of Eratosthenes 1. List all the numbers start from the 2. 2. 2 is a Prime number. 3. Remove all the number that is multiple of 2. 4. The smallest number that is not removed, is a 3 which is a pri.. 2020. 6. 14.
LeetCode. Largest Divisible Subset Problem: Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. If there are multiple solutions, return any subset is fine. Example 1: Input: [1,2,3] Output: [1,2] (of course, [1,3] will also be ok) Example 2: Input: [1,2,4,8] Output: [1,2,4,8] -Summary- 1. Sort the input nums list (*Sorti.. 2020. 6. 14.
LeetCode. Insert Delete GetRandom O(1) Problem: Design a data structure that supports all following operations in average O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if present. getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned. Example: -Summary- Using a dictionary data struct.. 2020. 6. 13.
LeetCode 34. Find First and Last Position of Element in Sorted Array [Binary Search] Problem: Given 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] -Summ.. 2020. 6. 13.