随笔分类 - Algorithms
摘要:Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive intege
阅读全文
摘要:Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string
阅读全文
摘要:// Code goes here function countNegative (M, n, m) { count = 0; i = 0; j = m - 1; while (j >=0 && i < n) { if (M[i][j] < 0) { count += (j+1) i += 1; } else { j...
阅读全文
摘要:Let's say we are given an array: We want to get K = 3 smallest items from the array and using Max heap data structure. So this is how to think about i
阅读全文
摘要:Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original ar
阅读全文
摘要:For example there is a staricase N = 3 | | | | | | | | | | There is N = 3 staricase, for each step, you can either take {1 or 2} step at a time. So as
阅读全文
摘要:For example we have 'a' -> 1 'b' -> 2 .. 'z' -> 26 By given "12", we can decode the string to give result "ab" or 'L', 2 ways to decode, your function
阅读全文
摘要:Given a string, find the longest subsequence consisting of a single character. Example: longest("ABAACDDDBBA") should return {'D': 3}.
阅读全文
摘要:Each row and each column are already SORTED in the given matrix!
阅读全文
摘要:For a given array, we try to find set of pair which sums up as the given target number. For example, we are given the array and target as: We should b
阅读全文
摘要:About how to traverse binary tree, can refer this post.
阅读全文
摘要:For example we have: We want to get: Requirement: You can only do in array swap, you cannot create a new array. The way to do it: 1. Reverse whole str
阅读全文
摘要:vThere are three ways to solve Fibonacci problem 'First Recursion approach: As we can see to calculate fib(5), we need to calculate fib(3) twice and f
阅读全文
摘要:Q1: Find the smallest value from array: O(n), cannot be improved anymore, because we have to loop though the array once. Q2: Find 2nd smallest value,
阅读全文
摘要:A balanced binary tree is something that is used very commonly in analysis of computer science algorithms. In this lesson we cover how to determine th
阅读全文
摘要:Shuffling is a common process used with randomizing the order for a deck of cards. The key property for a perfect shuffle is that each item should hav
阅读全文
摘要:One integer takes 32bit in memory, 1 byte = 8bits, therefore one integer takes 4 bytes. Now let's assume we have an array: [1,2,3] 4bytes . 4bytes . 4
阅读全文
摘要:Naive solution for this problem would be caluclate all the possible combinations: The maximum subarray problem is one of the nicest examples of dynami
阅读全文
摘要:The median maintenance problem is a common programming challenge presented in software engineering job interviews. In this lesson we cover an example
阅读全文
摘要:Source, git Heap is a data structure that can fundamentally change the performance of fairly common algorithms in Computer Science. The heap data stru
阅读全文