the arithmetic sequence of integers
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了the arithmetic sequence of integers相关的知识,希望对你有一定的参考价值。
Provide an implementation of the following interface: public interface Powers extends Iterator<Long> { /* Returns the next integer a in the arithmetic sequence of integers where * a = m^n, m > 1 n > 1, and m and n are both integers * Thus, the first few outputs will be 4, 8, 9, 16, 25, 27, 32, 36, etc. */ public Long next(); /* Resets the sequence to the beginning, such that the next call to next() * will return 4. */ public void reset(); }
1) Use a minheap 2) Upon reset (or instantiation), make sure heap is cleared with 4 on the top with {2,2} for m^n as metadata. 3) When next is called, pop the min and add 8 {2,3} and 9 {3,2} 4) Repeat for {m,n+1} when you pop {m,n}. If a {m,2} is popped, then add {m+1,2} 5) For any existing values, push {m,n+2} instead 6) Rinse and repeat for each next().
以上是关于the arithmetic sequence of integers的主要内容,如果未能解决你的问题,请参考以下文章
[动态规划] leetcode 1027 Longest Arithmetic Sequence
LeetCode 1027. Longest Arithmetic Sequence