Simple Python code of Knapsack Problem
Just write this snippet for my practice of 0-1 Knapsack Problem: values = [1, 2, 3, 4, 5] weights = [3, 2, 1, 9, 6] max_weight = 12 def knappack(): n = len(values) dp = [[0] * (max_weight+1)...
聚合国内IT技术精华文章,分享IT技术精华,帮助IT从业人士成长
Just write this snippet for my practice of 0-1 Knapsack Problem: values = [1, 2, 3, 4, 5] weights = [3, 2, 1, 9, 6] max_weight = 12 def knappack(): n = len(values) dp = [[0] * (max_weight+1)...
My first idea is depth-first-search: iterate all people, try to give them different hats. The solution got TLE (Time Limit Exceeded). Then as a hint from discussion forum, I started to iterate hat...
To solve Leetcode #1675, I wrote the below code with the help of hints: import sys import heapq class Solution: def minimumDeviation(self, nums: List[int]) -> int: n = len(nums) heapq.he...
The first intuitive idea that jumps out of my mind after taking a look at LeetCode #127 is using the graph algorithm. But for building the graph first, I need to traverse the wordList by O(n2) tim...
The popular solution for LeetCode #494 is dynamic programming. But my first idea is Divide and Conquer. Although it’s not very fast, it’s another idea: from collections import Count...
About two weeks ago I met the “number of islands” problem in LeetCode. The first idea that jumped out of my brain is ‘dynamic programming’: I can create a new matrix (let...
Imagine we have an array of numbers [9, 8, 7, 1, 2, 3, 4, 5, 6]. What’s the best solution to split it into 3 partitions with the most “average sum”, which means they have minimum...
I am reading the second edition of “The Algorithm Design Manual” recently. Reaching the chapter about backtracking, I realized that this method could solve many problems, even complica...
查看本系列文章合集,请看这里。 做好文本预处理,才能输入fastText训练一个效果好的模型出来。 ✓ 原文示例 有时我们拿到的源数据是很粗糙的,带有很多会影响模型效果的内容,例如下面这样: <p>罗望子,是豆科酸豆属唯一的种,是热带乔木,原产于东部非洲,包括马达加斯加落叶森林,但已被引入热带亚洲、拉丁美洲和加勒比海。柽柳是中国海南省三亚的一种城市树木。罗望子最适...
查看本系列文章合集,请看这里。 为 training 数据做标注,这可能是一个艰巨的任务,也可能是一个有捷径的任务。 有时候,我们可以依据一些已知的规则来标注文本,比如不同的数据是从不同的来源获取到的,从来源可以知道它们所属的类别,这是一个捷径。不过我这里不具备这样的条件。 我的数据来源是网上的各种新闻,不是某些专业领域的数据,这种比较常见的文本分类任务,可以利用国内的几大云服务商提供的免...
查看本系列文章合集,请看这里。 ✓ 开始训练第一个文本分类模型 标注好的数据,其格式为: __label__科技 月 10 日 网通 社从 高合 汽车 获悉 华人 运通 微软 2020 世界 人工智能 大会 云端 峰会 WAIC 2020 上 达成 战略 合作 依托 微软 小冰 人工智能 技术 高合 汽车 上 落地 全球 首个 主动式 人工智能 伙伴 HiPhiGo 用户...
查看本系列文章合集,请看这里。 前面说的模型训练、预测过程,是用 fastText 可执行程序完成的。fastText提供了Python的接口,同样的功能也可以用Python实现。如果数据量比较小,单机做文本分类没啥问题。但我的数据量比较大,几十G的文本数据,单机加载模型、预测分类太耗资源了,而且速度慢。 并行这种事嘛,交给Map-Reduce job来做是最合适不过了,不过,要在Hado...