
动态规划
junchen1992
Be the change that you wish to see in the world.
展开
-
HackerRank: Bricks Game
题目链接分析本题使用动态规划的思想解决。dp[i]表示:若player从当前位置开始,能获得的最大分数。很明显,需要逆序构建dp数组。代码(Python 2.7)t = int(raw_input())for t_i in xrange(t): n = int(raw_input()) a = map(int, raw_input().split()) cum_sum = [原创 2016-08-17 13:30:37 · 408 阅读 · 0 评论 -
LeetCode #10: Regular Expression Matching
Problem Statement(Source) Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cove原创 2016-09-04 12:45:36 · 244 阅读 · 0 评论 -
LeetCode #188: Best Time to Buy and Sell Stock IV
Problem Statement(Source) Say you have an array for which the ithi^{th} element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most k transac原创 2016-09-04 11:42:56 · 252 阅读 · 0 评论 -
LeetCode #140: Word Break II
Problem Statement(Source)Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For examp原创 2016-09-03 13:07:28 · 304 阅读 · 0 评论 -
PIQ07: Longest Palindromic Subsequence
Problem StatementGiven a string S, Find the length of the longest palindromic subsequence.Approach 1Reverse S to T, then make use of the Longest Common Subsequence algorithm.Approach 2Dynamic programmi原创 2016-08-23 11:45:01 · 324 阅读 · 0 评论 -
PIQ26: Longest Palindromic Substring
Problem StatementGiven a string S, find the longest palindromic substring of S.def longest_palindromic_substring(s): if not s: return None n = len(s) lps, lps_len = s[0], 1 dp =原创 2016-08-23 09:46:27 · 243 阅读 · 0 评论 -
PIQ39: Longest Common Subsequence
Problem StatementGiven two string s1 and s2. Find the longest common subsequence between s1 and s2.Link@HackerRank: The Longest Common Subsequencedef longest_common_subsequence(s1, s2): """Find the原创 2016-08-23 09:00:05 · 333 阅读 · 0 评论 -
PIQ19: Longest Common Substring
Problem Statement:Given two strings S1 and S2, find the longest common substring between S1 and S2.Approach 1: Dynamic Programmingdef LCS(s1, s2): """ Time complexity: O(n^2) Space complexi原创 2016-08-22 14:15:41 · 287 阅读 · 0 评论 -
LeetCode #329: Longest Increasing Path in a Matrix
Problem Statement(Source) Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diago原创 2016-08-31 20:45:13 · 421 阅读 · 0 评论 -
LeetCode #132: Palindrome Partitioning II
Problem statementGiven a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = “aab”原创 2016-08-22 20:55:22 · 573 阅读 · 0 评论 -
LeetCode #312: Burst Balloons
Problem Statement(Source) Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst ballo原创 2016-08-30 12:17:24 · 413 阅读 · 0 评论 -
HackerRank: Play with words
题目链接分析经典动态规划问题“最长回文子串(Longest Palindromic Subsequence)”的变形。动态规划#!/usr/bin/pythons = raw_input()n = len(s)dp = [[0 for i in xrange(n)] for j in xrange(n)]for i in xrange(n): dp[i][i] = 1for col i原创 2016-08-18 09:28:37 · 416 阅读 · 0 评论 -
HackerRank: Sam and sub-strings
题目链接分析考虑每一位数字对最终总和的贡献,画出表格分析小例子得到模式。代码s = raw_input()n = len(s)candies = 0mul = 1MOD = 1000000007for index in xrange(n - 1, -1, -1): candies = (candies + (ord(s[index]) - ord('0')) * mul *(ind原创 2016-08-17 18:30:07 · 562 阅读 · 0 评论 -
LeetCode 115. Distinct Subsequences
Problem Statement(Source) Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by dele原创 2016-11-02 20:51:19 · 264 阅读 · 0 评论