- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试在 Haskell 中实现“stock span problem”。这是我想出的解决方案。想看看是否有任何其他惯用的方法来做到这一点。这是 O(n^2) 算法 (?) 并使用堆栈,我们可以将其变为 O(n)。任何指向可以使用的其他高阶函数的指针都是值得赞赏的。
import Data.List (inits)
type Quote = Int
type Quotes = [Quote]
type Span = [Int]
stockSpanQuad :: Quotes -> Span
stockSpanQuad [] = []
stockSpanQuad xs = map spanQ (map splitfunc (tail $ inits xs))
where
spanQ (qs, q) = 1 + (length $ takeWhile (\a -> a <= q) (reverse qs))
splitfunc xs = (init xs, last xs)
最佳答案
您提供的链接包含一个使用堆栈数据结构的解决方案。每种语言的示例都会改变堆栈,使用带索引的数组并按索引访问数组的元素。所有这些操作在 Haskell 中都不是很常见。
让我们考虑以下解决方案:
type Quote = Int
type Quotes = [Quote]
type Span = [Int]
stockSpanWithStack :: Quotes -> Span
stockSpanWithStack quotes = calculateSpan quotesWithIndexes []
where
quotesWithIndexes = zip quotes [0..]
calculateSpan [] _ = []
calculateSpan ((x, index):xs) stack =
let
newStack = dropWhile (\(y, _) -> y <= x) stack
stockValue [] = index + 1
stockValue ((_, x):_) = index - x
in
(stockValue newStack) : (calculateSpan xs ((x, index):newStack))
让我们将它与 Python 解决方案进行比较:
# Calculate span values for rest of the elements
for i in range(1, n):
# Pop elements from stack whlie stack is not
# empty and top of stack is smaller than price[i]
while( len(st) > 0 and price[st[0]] <= price[i]):
st.pop()
# If stack becomes empty, then price[i] is greater
# than all elements on left of it, i.e. price[0],
# price[1], ..price[i-1]. Else the price[i] is
# greater than elements after top of stack
S[i] = i+1 if len(st) <= 0 else (i - st[0])
# Push this element to stack
st.append(i)
对于堆栈解决方案,我们需要带有索引的元素。可以这样模仿:
quotesWithIndexes = zip quotes [0..]
报价列表递归迭代,而不是在每次循环迭代中修改堆栈,我们可以使用修改后的值调用函数:
calculateSpan ((x, index):xs) stack =
Python 中的以下行(弹出堆栈中小于当前值的元素):
while( len(st) > 0 and price[st[0]] <= price[i]):
st.pop()
可以在 Haskell 中重写为:
newStack = dropWhile (\(y, _) -> y <= x) stack
以及股票值(value)的计算:
S[i] = i+1 if len(st) <= 0 else (i - st[0])
可以解释为:
stockValue [] = index + 1
stockValue ((_, x):_) = index - x
下面说,修改变量的状态并不常见,比如 S[i] = ...
或 st.append(i)
。但是我们可以使用新的堆栈值递归调用该函数并将当前结果添加到它之前:
(stockValue newStack) : (calculateSpan xs ((x, index):newStack))
从技术上讲,我们在列表的开头压入并删除列表的第一个元素,因为这是在 Haskell 中使用列表的惯用且性能更高的方式。
关于algorithm - Haskell:库存跨度算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49583718/
我是 PrestaShop 的新手。我遇到了一个问题,无法在 PrestaShop 从库存中减去商品的代码中找到。 当客户创建订单并选择付款时,商品将从库存中减去,但如果客户不付款并返回,则商品不会退
是否有最好的方法在关系数据库中存储诸如玩家背包之类的数据? 假设我想将玩家背包存储在数据库中。 表“eq”的数据库模型如下所示: 玩家 ID |槽 |商品编号 |金额 现在假设我在数据库中有这一行:
有没有一种方法可以在不使用 [all:vars] 的情况下在组之间继承变量 例子: [GROUP_A] server-A1 [GROUP_A:vars] vpc_cidr="192.10.0.0/21
我的 WooCommerce 插件遇到技术问题,但似乎无法找到答案。我得到了一个可变产品,有两个不同的研讨会时间。举一个简单的例子: 产品:事件当天门票 研讨会 1:红色 |蓝色|绿色 研讨会 2:红
我正在制作一个需要访问用户的 Steam 库存的网站。我找到了一个用于获取 Team Fortress 2 库存、Dota 2、CS:S、CS:GO 和 Portal 2 的 API。但我没有找到任何
我正在建立一个 woocommerce 网上商店。 这是一家手机壳网店,客户想在 woocommerce 后台保留库存,只有一个问题: 它们有不同的型号,例如 iPhone 4 和 iPhone 5。
我在 CodeBlocks (Windows 7) 中使用 GTK,并尝试创建 gtk_button_new_from_stock 所以我使用了以下代码: #include #include in
我有一个名为“条目”的表,我在其中保存每日销售和购买交易/条目。 “条目”表如下所示: ---条目表--- EntryDate EntryType Item Qty Rate
假设我有以下两条记录; tran_id item_id qty_in qty_out price 1 1 15
当我点击元素栏中的元素时,它什么都不做,我可以将它拖到任何我想要的地方。然后我重新打开库存,元素又回来了。我希望单击该项目(在本例中为通知项目)以切换通知 boolean 值并关闭库存。请帮忙。这可能
(我使用的是 Java,因此添加了“Java”标签,以防它影响任何答案,但有些人可能会认为该标签是不必要的。) 考虑以下因素: 我有一个阳台/阳台可以进行图形规划。作为最终计划的一部分,我需要列出 b
我有 2 个表 Tblinkreceiving 和 Tblinkdelivery。我想显示每个 inkcode 的库存余额。我尝试了下面的 sql 连接查询,但当我交叉检查手动计算时,许多 inkco
所以在很大程度上我明白我做错了什么,问题是我不知道如何解决它。 目标:这是一个必须包含可操作的菜单和库存管理功能的商店管理系统。为此,我使用数组来添加商店的商品、它们的描述和它们的数量。所有数组都部分
我发现设置 pg_hba.conf 文件非常困难。这是浪费我时间的常见情况。我需要做什么? 首先,在 Amazon、Digital Ocean 或我自己的硬件上安装新安装的 Linux 发行版的虚拟机
我正在尝试修改旨在与 HTML5 和 Javascript 一起使用的库存列表 Dynagrid 演示,它最初设计用于每次订阅订阅一个项目。 在我的例子中,我已将此演示连接到我的 lightstrea
元素以件和盒子的形式出现。 例如假设, 我购买了 100 片(片剂)medicineX。 我购买了一盒,其中包含50 medicineY。 现在元素仅按件分配。 意味着 MedicineX 将根据他们
我正在尝试使用 Terraform 中的 local_file 函数创建 Ansible 库存文件(我愿意接受以不同方式执行此操作的建议) 模块“vm”配置: resource "azurerm_li
我正在尝试使用 Terraform 中的 local_file 函数创建 Ansible 库存文件(我愿意接受以不同方式执行此操作的建议) 模块“vm”配置: resource "azurerm_li
我想知道是否有更有效/更干净的方法来执行以下操作。假设我有一个包含 2 列的数据框,百分比(基于之前的价格)和操作,玩/买 (1) 或不玩/卖 (-1)。它基本上与股票有关。 为简单起见,请考虑示例
不确定如何表达,但我正在创建一个音乐应用程序,并且我有一个流派选项卡按钮,该按钮将流派列为 UICollectionView 中的单元格。 然后用户选择一个流派,比如“Hip-Hop”,然后会显示相应
我是一名优秀的程序员,十分优秀!