
算法集中营
只想学学算法
Demon的黑与白
Stay hungry,Stay foolish
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
POJ 刷题系列:1837. Balance
POJ 刷题系列:1837. Balance传送门:1837. Balance题意: 一个天枰上有C个钩子,把M个砝码挂在这些C个钩子上,问多少种平衡的挂法。思路: 首先考虑单个砝码在天枰的位置,可以得到C个状态,对应的平衡度为:G[0] * C[i],记录这些初始平衡度的个数。这样当放入第二个砝码时,可以遍历这些平衡度,从而得到一个新的平衡度。此时,不同位置,不同质量的砝...原创 2018-05-23 00:07:17 · 892 阅读 · 0 评论 -
LWC 74: 793. Preimage Size of Factorial Zeroes Function
LWC 74: 793. Preimage Size of Factorial Zeroes Function传送门:793. Preimage Size of Factorial Zeroes FunctionProblem: Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * ...原创 2018-03-06 14:50:04 · 623 阅读 · 0 评论 -
LWC 74: 795. Number of Subarrays with Bounded Maximum
LWC 74: 795. Number of Subarrays with Bounded Maximum传送门:795. Number of Subarrays with Bounded MaximumProblem: We are given an array A of positive integers, and two positive integers L and R (...原创 2018-03-06 14:02:54 · 580 阅读 · 0 评论 -
LWC 74: 792. Number of Matching Subsequences
LWC 74: 792. Number of Matching Subsequences传送门:792. Number of Matching SubsequencesProblem: Given string S and a dictionary of words words, find the number of words[i] that is a subsequence o...原创 2018-03-06 10:33:13 · 1367 阅读 · 0 评论 -
LWC 74: 794. Valid Tic-Tac-Toe State
LWC 74: 794. Valid Tic-Tac-Toe State传送门:794. Valid Tic-Tac-Toe StateProblem: A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board...原创 2018-03-05 21:58:54 · 1314 阅读 · 0 评论 -
LWC 73: 790. Domino and Tromino Tiling
LWC 73: 790. Domino and Tromino Tiling传送门:790. Domino and Tromino TilingProblem: We have two types of tiles: a 2x1 domino shape, and an “L” tromino shape. These shapes may be rotated. XX ...原创 2018-02-26 10:08:54 · 1044 阅读 · 2 评论 -
LWC 73: 791. Custom Sort String
LWC 73: 791. Custom Sort String传送门:791. Custom Sort StringProblem: S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sorted in some custom or...原创 2018-02-26 09:38:36 · 764 阅读 · 0 评论 -
LWC 73: 789. Escape The Ghosts
LWC 73: 789. Escape The Ghosts传送门:789. Escape The GhostsProblem: You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (target[0], target1). There ar...原创 2018-02-26 09:01:53 · 651 阅读 · 0 评论 -
LWC 73: 788. Rotated Digits
LWC 73: 788. Rotated Digits传送门:788. Rotated DigitsProblem: X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. A number...原创 2018-02-26 08:40:12 · 868 阅读 · 0 评论 -
LWC 72: 786. K-th Smallest Prime Fraction
LWC 72: 786. K-th Smallest Prime Fraction传送门:786. K-th Smallest Prime FractionProblem: A sorted list A contains 1, plus some number of primes. Then, for every p < q in the list, we conside...原创 2018-02-18 18:46:37 · 1101 阅读 · 0 评论 -
LWC 72: 787. Cheapest Flights Within K Stops
LWC 72: 787. Cheapest Flights Within K Stops传送门:787. Cheapest Flights Within K StopsProblem: There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pr...原创 2018-02-18 18:06:44 · 1659 阅读 · 0 评论 -
LWC 72: 785. Is Graph Bipartite?
LWC 72: 785. Is Graph Bipartite?传送门:785. Is Graph Bipartite?Problem: Given a graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it’s set o...原创 2018-02-18 15:21:56 · 1092 阅读 · 0 评论 -
LWC 72: 784. Letter Case Permutation
LWC 72: 784. Letter Case Permutation传送门:784. Letter Case PermutationProblem: Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string....原创 2018-02-18 13:53:15 · 2254 阅读 · 0 评论 -
LWC 71: 780. Reaching Points
LWC 71: 780. Reaching Points传送门:780. Reaching PointsProblem: A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). Given a starting point (sx, sy) a...原创 2018-02-11 18:05:07 · 637 阅读 · 0 评论 -
LWC 71: 781. Rabbits in Forest
LWC 71: 781. Rabbits in Forest传送门:781. Rabbits in ForestProblem: In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how many other rabbits have the...原创 2018-02-11 15:18:55 · 922 阅读 · 0 评论 -
LWC 71: 783. Minimum Distance Between BST Nodes
LWC 71: 783. Minimum Distance Between BST Nodes传送门:783. Minimum Distance Between BST NodesProblem: Given a Binary Search Tree (BST) with the root node root, return the minimum difference betwe...原创 2018-02-11 14:50:50 · 1078 阅读 · 0 评论 -
POJ 刷题系列:3274. Gold Balanced Lineup
POJ 刷题系列:3274. Gold Balanced Lineup传送门:3274. Gold Balanced Lineup题意: 官方题解: Consider the partial sum sequence of each of the k features built by taking the sum of all the values up t原创 2018-02-04 22:04:21 · 469 阅读 · 0 评论 -
LWC 70: 778. Swim in Rising Water
LWC 70: 778. Swim in Rising Water传送门:778. Swim in Rising WaterProblem: On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j). Now rain starts to fall. At t原创 2018-02-04 19:28:53 · 1051 阅读 · 0 评论 -
LWC 70: 777. Swap Adjacent in LR String
LWC 70: 777. Swap Adjacent in LR String传送门:777. Swap Adjacent in LR StringProblem: In a string composed of ‘L’, ‘R’, and ‘X’ characters, like “RXXLRXRXL”, a move consists of either replacing o原创 2018-02-04 18:54:33 · 949 阅读 · 0 评论 -
LWC 70: 776. Split BST
LWC 70: 776. Split BST传送门:776. Split BSTProblem: Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees where one subtree has nodes that a原创 2018-02-04 18:01:50 · 1007 阅读 · 0 评论 -
LWC 70: 779. K-th Symbol in Grammar
LWC 70: 779. K-th Symbol in Grammar传送门:779. K-th Symbol in GrammarProblem: On the first row, we write a 0. Now in every subsequent row, we look at the previous row and replace each occurrence原创 2018-02-04 17:32:44 · 557 阅读 · 0 评论 -
POJ 刷题系列:2503. Babelfish
POJ 刷题系列:2503. Babelfish传送门:2503. Babelfish题意: 翻译:给出一英文单词和与之对应的外文,组成字典集。后续当给出一个外文单词时,求出对应的英文单词。思路: 无脑 HashMap,外文为key,英文单词为value,存入hashMap。代码如下:import java.io.IOException;import java原创 2018-02-04 10:27:00 · 420 阅读 · 0 评论 -
LWC 69: 774. Minimize Max Distance to Gas Station
LWC 69: 774. Minimize Max Distance to Gas Station传送门:774. Minimize Max Distance to Gas StationProblem: On a horizontal number line, we have gas stations at positions stations[0], stations1, …,原创 2018-01-29 14:36:25 · 2136 阅读 · 0 评论 -
LWC 69: 773. Sliding Puzzle
LWC 69: 773. Sliding Puzzle传送门:773. Sliding PuzzleProblem: On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0. A move consists原创 2018-01-29 13:45:09 · 957 阅读 · 0 评论 -
LWC 69: 775. Global and Local Inversions
LWC 69: 775. Global and Local Inversions传送门:775. Global and Local InversionsProblem: We have some permutation A of [0, 1, …, N - 1], where N is the length of A. The number of (global) inv原创 2018-01-29 12:33:35 · 1063 阅读 · 0 评论 -
LWC 68: 767. Reorganize String
LWC 68: 767. Reorganize String传送门:767. Reorganize StringProblem: Given a string S, check if the letters can be rearranged so that two characters that are adjacent to each other are not the sam原创 2018-01-21 14:12:27 · 1108 阅读 · 0 评论 -
LWC 68: 769. Max Chunks To Make Sorted (ver. 1)
LWC 68: 769. Max Chunks To Make Sorted (ver. 1)传送门:769. Max Chunks To Make Sorted (ver. 1)Problem: Given an array arr that is a permutation of [0, 1, …, arr.length - 1], we split the array int原创 2018-01-21 15:18:12 · 690 阅读 · 0 评论 -
LWC 69: 771. Jewels and Stones
LWC 69: 771. Jewels and Stones传送门:771. Jewels and StonesProblem: You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each characte原创 2018-01-29 11:09:31 · 1563 阅读 · 0 评论 -
LWC 68: 766. Toeplitz Matrix
LWC 68: 766. Toeplitz Matrix传送门:766. Toeplitz MatrixProblem: A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given an M x N matrix, return T原创 2018-01-21 13:04:18 · 747 阅读 · 0 评论 -
POJ 刷题系列:1840. Eqs
POJ 刷题系列:1840. Eqs传送门:1840. Eqs题意: 等式 a1x31+a2x32+a3x33+a4x34+a5x35=0a_1x_1^3+ a_2x_2^3+ a_3x_3^3+ a_4x_4^3+ a_5x_5^3=0 ,其中ai∈[−50,50],i=1,2,3,4,5a_i \in [-50, 50], i = 1, 2, 3, 4, 5,xi∈[−50,5原创 2018-01-17 19:31:05 · 367 阅读 · 0 评论 -
POJ 刷题系列:3349. Snowflake Snow Snowflakes
POJ 刷题系列:3349. Snowflake Snow Snowflakes传送门:3349. Snowflake Snow Snowflakes题意: 每个雪花都有六个分支,用六个整数代表,这六个整数是从任意一个分支开始,朝顺时针或逆时针方向遍历得到的。输入多个雪花,判断是否有形状一致的雪花存在。思路: 和HASH检测冲突的原理很像,先对6个数进行HASH而不管顺序,原创 2018-01-15 19:34:01 · 515 阅读 · 0 评论 -
POJ 刷题系列:2299. Ultra-QuickSort
POJ 刷题系列:2299. Ultra-QuickSort传送门:2299. Ultra-QuickSort题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列。思路: 实际上是让你求解逆序对的个数,求逆序对的个数可以采用归并排序。曾写过一篇关于算法细节系列(27):时间复杂度为何还能优化?今天又重新复习了一遍,有了一些新的理解。原创 2018-01-14 21:09:04 · 595 阅读 · 0 评论 -
POJ 刷题系列:2388. Who's in the Middle
POJ 刷题系列:2388. Who’s in the Middle传送门:2388. Who’s in the Middle题意: 给定n个数,从小到大排列,求出中间大的数。思路: 直接排序,调用接口就非常方便了。代码如下:import java.io.BufferedReader;import java.io.File;import java.io.Fi原创 2018-01-14 18:52:09 · 440 阅读 · 0 评论 -
POJ 刷题系列:1936. All in All
POJ 刷题系列:1936. All in All传送门:1936. All in All题意: 给定两个字符串s和t,你需要判断s是否是t的“子列”。也就是说,如果你去掉t中的某些字符,剩下字符将连接而成为s。思路: 动规or递归+记忆化,定义状态dp[i][j]表示T[0…i - 1] 及S[0…j - 1]能否构成子列,状态转移如下:if S[j] == T[原创 2018-01-14 18:40:27 · 348 阅读 · 0 评论 -
POJ 刷题系列:3080. Blue Jeans
POJ 刷题系列:3080. Blue Jeans传送门:3080. Blue Jeans题意: 一组字符串,求字符串组的公共最长子串,如果有多个最长,取alphabetical order最小。思路: KMP + 暴力枚举子串,不过Java关于模式匹配,已有封装好的代码,如下:import java.io.BufferedReader;import java.i原创 2018-01-14 17:58:40 · 396 阅读 · 0 评论 -
LWC 67: 765. Couples Holding Hands
LWC 67: 765. Couples Holding Hands传送门:765. Couples Holding HandsProblem: N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum number of swaps so that原创 2018-01-14 16:44:41 · 1858 阅读 · 0 评论 -
LWC 67: 764. Largest Plus Sign
LWC 67: 764. Largest Plus Sign传送门:764. Largest Plus SignProblem: In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given list mines which are 0. What i原创 2018-01-14 16:19:02 · 1251 阅读 · 0 评论 -
LWC 67: 763. Partition Labels
LWC 67: 763. Partition Labels传送门:763. Partition LabelsProblem: A string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter app原创 2018-01-14 15:38:50 · 1137 阅读 · 0 评论 -
LWC 67: 762. Prime Number of Set Bits in Binary Representation
LWC 67: 762. Prime Number of Set Bits in Binary Representation传送门:762. Prime Number of Set Bits in Binary RepresentationProblem: Given two integers L and R, find the count of numbers in the ra原创 2018-01-14 14:48:00 · 896 阅读 · 0 评论 -
POJ 刷题系列:1035. Spell checker
POJ 刷题系列:1035. Spell checker传送门:1035. Spell checker题意: 一组字符串,组成字典。查询某个字符串是否在该字典中。如果不存在,输出字典集中与查询字符串编辑距离为1的字符串。思路: 因为每个字符串的最大长度为15,查询O(1),采用暴力。代码如下:import java.io.BufferedReader;import原创 2018-01-10 19:18:31 · 495 阅读 · 0 评论