this example is from chapter 4 in <the introduction to algorithm> the main idea is all showed in the book , i think maybe realizing the algorithm is a good way to understand it. /* from : introduction to algorithm chapter four algorithm:divide and c…
Introduction Divide-and-Conquer的三个步骤: Divide the problem into a number of subproblems that are smaller instances of the same problem. Conquer the subproblems by solving them recursively. If the subproblem sizes are small enough, however, just solve t…
的确,正如偶像Bruce Eckel所说,"Life is short, you need Python"! 如果你正在考虑学Java还是Python的话,那就别想了,选Python吧,你的人生会有更多的时间做其他有意思的事情. 研究生之前我没学python是有原因的:首先,我怕蛇,很怕很怕,而这货的logo竟然就是蛇,我因故而避之:其次,我不喜欢脚本语言,我会shell,但是写的时候不是很爽,只是在处理些文件操作或者字符串操作的时候才会想起它,听说python脚本神马的,我便又避之.…
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ …
Awesome Courses Introduction There is a lot of hidden treasure lying within university pages scattered across the internet. This list is an attempt to bring to light those awesome courses which make their high-quality material i.e. assignments, lect…
The pattern language is organized into four design spaces. Generally one starts at the top in the Finding Concurrency design space and works down through the other design spaces in order until a detailed design for a parallel program is obtained. Cl…
Approach #1 Brute Force Intuition We can exhaust the search space in quadratic time by checking whether each element is the majority element.Algorithm The brute force algorithm iterates over the array, and then iterates again for each number to…
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这题和sort list等题都比较相似,需要先用快慢指针的方法找到链表的中点,然后用recursive的方式构建左子树和右子树(用到的思想是Divide&Conquer),然后再构建好这个节点. 编程时一点要注意: (1)dummy节点的使用可以帮助找到中点的prev节点 但是d…
There are 3 possible approaches: DP, divide&conquer and greedy. And apparently, DP has O(n^2) complexity (TLE), DivideConquer can only be worse. Greedy can be O(n)! And its code is amazingly brief: class Solution { public: bool isSubsequence(string s…
Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a function that counts the occurrences of x in arr[]. Expected time complexity is O(Logn) Examples: Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2 Output…
1. 问题: The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 600851475143 ? 2. 解法(by java in Eclipse) package com.lun.alrithmetic; /* * Q1: what's the primary factor? (1 2 3) * Q2: i & i+2ne能否遍历出所有质数 */ public…
Two methods: 1. Traverse 2. Divide & Conquer // Traverse: usually do not have return value public class Solution { public void traverse(TreeNode root) { if (root == null) return; traverse(root.left); traverse(root.right); } } // Divide & Conquer:…
面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. public class Solution { /** * @param matrix, a list of lists of integers * @param target, an integer * @return a boolean, indicate whether matrix contains…
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: F(k…
Problem Statement: Path sum i Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \…