当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript OrderedSet.toList方法代码示例

本文整理汇总了TypeScript中Immutable.OrderedSet.toList方法的典型用法代码示例。如果您正苦于以下问题:TypeScript OrderedSet.toList方法的具体用法?TypeScript OrderedSet.toList怎么用?TypeScript OrderedSet.toList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Immutable.OrderedSet的用法示例。


在下文中一共展示了OrderedSet.toList方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: computeHelper

  private computeHelper(
    prices: OrderedSet<number>,
    budget: number): any { // actually Set<List<number>>
    let hashed = this.hashArgs(prices, budget);

    let memoizedResult = this.memo[hashed];
    if (typeof memoizedResult !== "undefined") {
      return memoizedResult;
    }


    // Base cases

    // If there are no prices, there can be no solution. Return empty set.
    if (prices.size === 0) {
      let results = Set([]);
      this.memo[hashed] = results;
      return results;

      // With one price, return empty set if price is not a factor of budget
      // or return a list of length budget / price filled with price.
      // E.g., for price 2 and budget 8, return List([2, 2, 2, 2]).
    } else if (prices.size === 1) {
      let onlyElement = prices.toList().get(0);
      if (budget % onlyElement === 0) {
        let results = Set([List(Array(budget / onlyElement).fill(onlyElement))]);
        this.memo[hashed] = results;
        return results;
      } else {
        let results = Set([]);
        this.memo[hashed] = results;
        return results;
      }

      // Recursive case. Divide-and-conquer algorithm compiles and filters
      // results by recurring on each price price, subtracting that price
      // from the budget and filtering the list of price prices to be less
      // than or equal to both the new budget and the current price. See
      // README for additional information.
    } else {
      return prices.flatMap((price: number) => {
        let newBudget = budget - price; // If we buy this item, what is our new budget?

        // Remove items that are more than our budget and more than the item
        // under consideration.
        let newMenuItems = prices.filter(c => {
          let priceCeiling = Math.min(newBudget, price);
          return c <= priceCeiling;
        }) as OrderedSet<number>; // Should remain an OrderedSet after filter

        // No recursion if the item under consideration exactly zeroes our
        // budget.
        if (newBudget === 0) {
          let results = List([List([price])]);
          this.memo[hashed] = results;
          return results;
        };

        // Recursive call
        let recursive = this.computeHelper(newMenuItems, newBudget);

        // If recursion returned results, concat the item under consideration
        // onto each result and return that. If recursion didn't return results
        // return empty set.
        let results = recursive
          ? recursive.map((e: List<number>) => e.concat(price)).toSet()
          : OrderedSet([]);

        this.memo[hashed] = results;
        return results;
      });
    }
  };
开发者ID:Ethan826,项目名称:tablexi-coding-challenge,代码行数:73,代码来源:knapsack.ts


注:本文中的Immutable.OrderedSet.toList方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。