算法导论-任务调度问题

问题描述: 在单处理器上具有期限和惩罚的单位时间任务调度问题(课本P239) 实验要求: (1)实现这个问题的贪心算法 (2)将每个 wi 替换为max{m1,m2…mn}—wi,运行算法比较结果。 解题思路: 1.先将任务按照时间惩罚递减顺序...

2016-05-25 14:56:19

阅读数:2153

评论数:0

01背包和部分背包问题

01背包: 有N件物品和一个容量为M的背包。放入第i件物品耗费的空间是W[i],得到的价值是V[i]。求解将哪些物品装入背包可使价值总和最大。解题思路:每种物品仅有一件,可以选择放或不放。 则其状态转移方程便是: DP[i,j]=max{DP[i−1,j],DP[i−1,j−Wi]+Vi}在这...

2016-05-24 00:18:20

阅读数:1106

评论数:0

106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. 根据中根遍历和后跟遍历,求二叉树。 class Solution { public: TreeNode *createTree(vect...

2016-05-23 20:45:36

阅读数:148

评论数:0

34. Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the or...

2016-05-22 23:17:34

阅读数:183

评论数:0

147. Insertion Sort List

Sort a linked list using insertion sort.链表的插入排序: class Solution { public: ListNode* insertionSortList(ListNode* head) { if(head==NULL)ret...

2016-05-21 14:56:53

阅读数:171

评论数:0

330. Patching Array

iven a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be for...

2016-05-20 09:47:28

阅读数:129

评论数:0

63. Unique Paths II

Follow up for “Unique Paths”:Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space ...

2016-05-19 19:26:01

阅读数:263

评论数:0

86. Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the ...

2016-05-18 09:50:06

阅读数:203

评论数:0

120. Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the fo...

2016-05-17 18:03:46

阅读数:132

评论数:0

201. Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.For example, given the ...

2016-05-16 22:13:28

阅读数:114

评论数:0

109. Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题目是将一个有序链表转为平衡二叉树。class Solution { pub...

2016-05-15 12:35:25

阅读数:131

评论数:0

78. Subsets

Given a set of distinct integers, nums, return all possible subsets.Note: Elements in a subset must be in non-descending order. The solution set mu...

2016-05-14 23:57:16

阅读数:132

评论数:0

213. House Robber II

Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so tha...

2016-05-13 22:56:56

阅读数:141

评论数:0

排序算法的稳定性总结

1.首先我们来看看插入排序,从第2个元素开始,把每个元素依次插入前面有序的序列中。 因为只有小于前面的元素时,才进行插入和移动操作,所以不会改变相同元素的相对顺序。 所以该算法是稳定,但是如果把a[j]>a[i]改为a[j]>=a[i]那就是不稳定的了。//直接插入排序 void ...

2016-05-12 22:17:51

阅读数:204

评论数:0

39. Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeat...

2016-05-11 23:51:36

阅读数:114

评论数:0

114. Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should l...

2016-05-10 23:58:43

阅读数:96

评论数:0

142. Linked List Cycle II

先看141. Linked List Cycle 。 Given a linked list, determine if it has a cycle in it. 判断链表是否有环,用两个快慢指针移动看是否重合即可。class Solution { public: bool ha...

2016-05-09 22:52:44

阅读数:150

评论数:0

[华为机试练习题]汽水瓶

有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用...

2016-05-08 20:50:06

阅读数:606

评论数:0

331. Verify Preorder Serialization of a Binary Tree

One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node...

2016-05-07 22:54:02

阅读数:147

评论数:0

81. Search in Rotated Sorted Array II

Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a functi...

2016-05-06 17:54:10

阅读数:352

评论数:0

提示
确定要删除当前文章?
取消 删除
关闭
关闭