Dynamic Programming series

数据科学中的Copy and Paste Programming:高效复用的工程方法论 Copy and Paste Programming 并非代码偷懒,而是数据科学领域高度模式化工作流下的理性工程实践。其本质是基于已验证结构进行精准微调,依托数据接入、特征加工、模型训练、结果交付四大标准化范式,显著降低调试成本、知识断层与重复造轮子风险。通过可信来源复用、上下文适配校验与变更可追溯机制,该方法将代码复用升维为可审计、可演进的技术基础设施。它直接支撑pandas、scikit-learn等主流库的生产级落地,广泛应用于金融风控、医疗AI、电商推荐等需快速迭代与强可靠性的场景。 阅读详情

DP is absolutely the best way to category levels of programming..... and it is always the favourite questions being asked by FB or Google.

1: Entry Level DP problem.    

/*
  A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.

For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.

Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
*/
#include "header.h"
using namespace std;
int longestZigzagSequence(vector<int>& nums) {
  int n = nums.size();
  vector<int> end_positive(n, 0);
  vector<int> end_negative(n, 0);
  for(int i = 0; i < nums.size(); ++i) {
    end_positive[i] = 1;
    end_negative[i] = 1;
    for(int j = 0; j <= i; ++j) {
      if(nums[j] > nums[i]) end_negative[i] = max(end_positive[j]+1, end_negative[i]);
      if(nums[i] > nums[j]) end_positive[i] = max(end_negative[j]+1, end_positive[i]);
    }
  }
  return max(end_negative[n-1], end_positive[n-1]);
}

int main(void) {
  vector<int> nums{1, 17, 5, 10, 13, 15, 10, 5, 16, 8};
  vector<int> test{ 70, 55, 13, 2, 99, 2, 80, 80, 80, 80, 100, 19, 7, 5, 5, 5, 1000, 32, 32 };
  cout << longestZigzagSequence(test) << endl;
}

// Same as rob neighbours in LeetCode 

/*
The old song declares "Go ahead and hate your neighbor", and the residents of Onetinville have taken those words to heart. Every resident hates his next-door neighbors on both sides. Nobody is willing to live farther away from the town's well than his neighbors, so the town has been arranged in a big circle around the well. Unfortunately, the town's well is in disrepair and needs to be restored. You have been hired to collect donations for the Save Our Well fund.

Each of the town's residents is willing to donate a certain amount, as specified in the int[] donations, which is listed in clockwise order around the well. However, nobody is willing to contribute to a fund to which his neighbor has also contributed. Next-door neighbors are always listed consecutively in donations, except that the first and last entries in donations are also for next-door neighbors. You must calculate and return the maximum amount of donations that can be collected.
*/
#include "header.h"
using namespace std;

int maxDonations(vector<int>& nums) {
  if(nums.size() == 0) return 0;
  int n = nums.size();
  if(n == 1) return nums[0];
  if(n == 2) return max(nums[0], nums[1]);

  vector<int> donations_odd(n, 0);
  vector<int> donations_even(n, 0);
  donations_odd[0] = nums[0];
  donations_even[1] = nums[1];
  for(int i = 2; i < n - 1; ++i) {
    donations_odd[i] = max(donations_odd[i-2] + nums[i], donations_odd[i-1]);
  }
  for(int i = 3; i < n; ++i) {
    donations_even[i] = max(donations_even[i-2] + nums[i], donations_even[i-1]);
  }
  return max(donations_even[n-1], donations_odd[n-2]);
}

int main(void) {
  vector<int> nums{94, 40, 49, 65, 21, 21, 106, 80, 92, 81, 679, 4, 61,
  6, 237, 12, 72, 74, 29, 95, 265, 35, 47, 1, 61, 397,
  52, 72, 37, 51, 1, 81, 45, 435, 7, 36, 57, 86, 81, 72};
  cout << maxDonations(nums) << endl;
}

Medium Level

Top Coder:  ChessMetric

It is very straight forward to first come up with dfs searching.

struct Pair {
  int _x;
  int _y;
  Pair(int x, int y) : _x(x), _y(y) {}
};

void waysToMove(int matrixSize, Pair start, Pair end, int steps, long long& method) {
  if(start._x < 0 || start._x >= matrixSize || start._y < 0 || start._y >= matrixSize) return;
  if(end._x < 0 || end._x >= matrixSize || end._y < 0 || end._y >= matrixSize || steps < 0) return;
  if((steps == 0) && (start._x == end._x) && (start._y == end._y)) {
    method++;
    return;
  }
  waysToMove(matrixSize, Pair(start._x - 1, start._y), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x + 1, start._y), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x, start._y + 1), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x, start._y - 1), end, steps - 1, method);

  waysToMove(matrixSize, Pair(start._x + 1, start._y + 1), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x + 1, start._y - 1), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x - 1, start._y - 1), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x - 1, start._y + 1), end, steps - 1, method);

  waysToMove(matrixSize, Pair(start._x - 1, start._y + 2), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x - 1, start._y - 2), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x + 1, start._y - 2), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x + 1, start._y + 2), end, steps - 1, method);

  waysToMove(matrixSize, Pair(start._x - 2, start._y - 1), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x - 2, start._y + 1), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x + 2, start._y - 1), end, steps - 1, method);
  waysToMove(matrixSize, Pair(start._x + 2, start._y + 1), end, steps - 1, method);
}


Apparently, we can just use DP to solve it.

class ChessMetric {
public:
	long long howMany( int size, vector <int> start, vector <int> end, int numMoves ) {
		this->size = size;
		map[0][start[0]][start[1]] = 1;

		for(int mv = 1; mv <= numMoves; ++mv) {
			for(int i = 0; i < size; ++i) {
				for(int j = 0; j < size; ++j) {
					map[mv][i][j] = neigh_sum(mv-1,i,j);
				}
			}
		}
		return map[numMoves][end[0]][end[1]];
	}
private:
	int size;
	const vector<pair<int,int>> ds = {{ 0, 1}, { 1, 0}, { 0,-1}, {-1, 0},
					  { 1, 1}, {-1, 1}, { 1,-1}, {-1,-1}, 
					  { 1, 2}, { 2, 1}, {-1, 2}, {-2, 1},
					  { 1,-2}, { 2,-1}, {-1,-2}, {-2,-1}};
	long long neigh_sum(int mv, int i, int j) {
		long long total = 0;
		for(auto delta:ds) {
			int x = i + delta.first;
			int y = j + delta.second;
			if(inbound(x,y)) {
				total += map[mv][x][y];
			}
		}
		return total;
	}

	bool inbound(int i, int j) {
		return 0 <= i && i < size && 0 <= j && j < size;
	}
};

1: https://www.topcoder.com/community/data-science/data-science-tutorials/dynamic-programming-from-novice-to-advanced/

工业互联网工控安全与工业质检AI大模型建设方案.pptx 立即下载

相关推荐

stm32 + freertos + c++混合编程方法

其他一些资料上面,c++配置成--cpp11或者-cpp 都是把工程配置成c++。三、添加cpp文件,我们调用cpp文件里面的接口函数,不需要修改main.c后缀名。2、配置工程编译时候是--c99,是因为工程里面FreeRTOS是c语言,c++模式会报错。我们这里是默认c,编写的c++文件,单独选择--cpp编译,这在后面配置!c++是面向对象编程,对于一些项目来说,c++编程更加合适!stm32cubemax配置,这里就不写了,配置资料比较多。这里就是让cpp文件在c++下编译。最后,编程成功,点亮!

yuhu0012的博客 2629

Jasna:免费修复视频马赛克的 GPU 加速工具,快速入门指南

Jasna 是一款**免费的 AI 视频马赛克修复工具**:它逐帧找出视频里的马赛克,用 GPU 修复模型重建被遮挡的画面,其余部分**原封不动**。整个流程(解码、检测、修复、编码)都在显卡上完成,配有图形界面和命令行,默认修复模型免费,无需任何调参就能跑起来。 ## 先搞清楚:它适合你的哪种情况 Jasna 解决的典型场景有四个: - **批量处理一个文件夹**:把整个视频目录加入队列,

gitblog_01104的博客 853

【Excel VBA 编程】表格内自动生成进度条

在表格中生成进度条,进度条需要自适应单元格的宽度,根据不同进度区间变换颜色,并且按照进度数据大小显示其长短

wei1019的专栏 1518

7 Series FPGAs Clocking Resources

CC 引脚(Clock-Capable Inputs)是 FPGA 专用时钟输入引脚,外部输入时钟。

qq_74282390的博客 306

OPTISYSTEM实战指南:线性调频连续波激光雷达系统仿真设计

本文提供了一份基于OPTISYSTEM软件的线性调频连续波激光雷达系统仿真实战指南。文章详细阐述了从软件界面熟悉、核心参数设置,到发射端、信道模型、相干接收端完整系统搭建的全过程,并介绍了性能优化与高级仿真技巧,旨在帮助读者快速掌握利用OPTISYSTEM进行低成本、高效率激光雷达系统设计与验证的方法。

weixin_29288313的博客 296

基于任务的后勤智能体:三段构建与演进设计探究

这是系统智能化的核心难点。例如,对于一个“加强连级滩头阵地防御”子任务,系统会根据防御时长、预期敌军攻击强度、地形等因素,计算出所需的弹药(不同弹种的具体数量)、构筑工事的器材、人员的给养和饮水量、以及医疗救护的药品和血包数量。首先进行分词、词性标注等基础处理,然后利用在军事语料库上训练的命名实体识别(NER)模型,提取出关键实体,如任务类型(“夺控作战”)、作战对象(“Y岛”)、时间约束(“D日H时”、“24小时内”)、成功标准(“建立稳固滩头阵地”)、保障重点(“弹药充足”、“及时救治”)等。

weixin_49042442的博客 1088

CANN/asc-devkit数据搬运API样例

This directory contains samples for multiple APIs related to data movement. Each sample is based on Ascend C's <<<>>> direct call method, supporting implementation of main function and kernel function

gitblog_01411的博客 384

利用WI生成无线信道数据

选取DeepMIMO的一个射线追踪场景O1(Outdoor 1)Scenario,工作频率为3.5GHz。有两条街和一个十字路口的户外场景18个基站和超过一百万个用户18个基站(BSs):BS1-BS18.每个基站的高度是8米每个基站的天线阵列是isotropic天线三个统一的x-y用户网格用户的总计数量为:1184923每个用户网格中的第一个用户总是(x,y)坐标最低的用户 所有用户的高度是2m每个用户的每个天线阵列是isotropic天线定义射频追踪场景。

bingbingaidaima的博客 5599

CANN数据移动API示例

This directory contains samples for multiple APIs related to data movement. Each sample is based on Ascend C's <<<>>> direct call method, supporting implementation of main function and kernel function

gitblog_00972的博客 810

YOLOv9改进策略目录 | 包含卷积、主干、检测头、注意力机制、Neck上百种创新机制

Hello,各位读者们好本专栏自开设两个月以来已经更新改进教程50余篇其中包含RepNCSPELAN4、主干、检测头、注意力机制、Neck多种结构上创新,也有损失函数和一些细节点上的创新。同时本人一些讲解视频和包含我所有创新的YOLOv9文档并不能在优快云上传(所有的创新点都经过我的测试是可用的,得到该文件之后大家可以随意组合使用),所以会建立群的形式在内上传我的文件和视频我也会在群内不定期和大家交流回答大家问题,同时定期会更新一些文章的创新点(经过我融合测试后的,先到先得)。

Snu77的博客 1万+

华为OD新系统机试真题 2026.5.10 - 美观的灯笼

华为OD新系统机试真题【美观的灯笼】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【美观的灯笼】

qq_45776114的博客 640

CANN/asc-devkit内存管理动态输入示例

This example implements an AddN example based on the static Tensor programming model, using the ListTensorDesc structure to handle dynamic input parameters, combining static memory allocation with eve

gitblog_00499的博客 418

YOLOv11-seg改进系列 | 引入CVPR2022 MetaFormer,零参数池化Token混合+StarReLU MLP,精度涨不掉速

本文提出了一种改进YOLOv11分割任务的C3k2_PoolingFormer模块,通过引入MetaFormer架构和Pooling Token Mixer,在保持模型轻量化的同时增强全局感受野建模能力。核心创新包括:1)使用零参数的AvgPool3×3差值作为Token Mixer,实现高效邻域信息聚合和局部对比增强;2)采用StarReLU MLP替代传统激活函数,提升非线性表达能力。改进后模型仅增加6.1%参数量,却能显著提升分割精度,特别适用于需要长程依赖建模的实例分割任务。该设计为轻量级视觉模型提

qq_44224801的博客 431

动态规化 - 斐波纳契级数

动态规化 - 斐波纳契级数 动态规划(dynamic programming,DP):高效求解递归问题。 递归问题通常需要多次重复求解子问题(subproblem),动态规划通过保存子问题的解(记忆,memoization)降低计算时间复杂度。 递归(recursion):递归求解子问题 记忆(memoization):保存子问题解 斐波纳契级数(Fibonacci Series): ...

Always On The Way 247

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

10个必读的安全研究论文:从Security-Paper项目中精选

在网络安全领域,研究论文是了解前沿技术和漏洞分析的重要资源。Security-Paper项目汇集了各种安全与计算机资料,本文精选其中10篇必读论文,涵盖物联网安全、漏洞利用、硬件黑客等多个领域,帮助安全爱好者和从业者快速掌握关键知识。 ## 1. IoT安全:大规模加密滥用分析 **论文路径**:IoT/CryptoREX_Large-scale_Analysis_of_Cryptograph

gitblog_01108的博客 1022

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

cann/asc-devkit矩阵计算API示例

This sample set introduces typical usage of different features of matrix computation APIs, providing corresponding end-to-end implementations. Samples in the directory are named according to "API_name

gitblog_00008的博客 704

CANN PyPTO并行张量编程框架

- 2026/04/10: Version 0.2.0 released, changing frontend expression methods, improving ease of use, enhancing features and performance, refining module capabilities, and optimizing development experien

gitblog_01107的博客 1019

Animeko 追番指南:从安装到开播,跨端进度同步只需 3 步

在手机看到一半的番,切到电脑就忘了上次看到哪一集;同一集内容,要挨个点开几个不同的网站找源;弹幕则散落在各个平台,各看各的。这类碎掉的使用体验,几乎是追番的标配。Animeko 是一个基于 Kotlin 与 Compose Multiplatform 的跨端追番客户端,把找番、追番、看番收拢在一个应用里:观看进度通过 Bangumi 云端同步,在线源与 BitTorrent 源聚合选源,弹幕自带云

gitblog_00114的博客 1290

GOTO 语句有害论 | 反对 GOTO 语句的理由

……

u013669912的博客 1873

cann/hccl自定义通信算子-点对点通信

This sample demonstrates how to develop Send and Recv point-to-point communication operators based on the HCCL communication programming interface. It covers the following features: 1. Implement poin

gitblog_00679的博客 1074

Python大数据旅游行程规划系统开发实践

推荐系统作为大数据时代的核心技术,通过分析用户行为和项目特征实现个性化推荐。其核心原理包括协同过滤、内容推荐和混合算法,能有效解决信息过载问题。在旅游领域,结合Spark数据处理和Django框架,可以构建实时更新的行程规划系统。典型应用需要处理多源数据采集、特征工程和可视化展示等环节,其中PySpark和Redis的组合能显著提升GB级数据的处理效率。本文介绍的混合推荐架构融合了协同过滤与动态规划算法,配合Pyecharts实现交互式可视化,为旅游行业提供了可落地的智能决策方案。

weixin_34099526的博客 225

CANN/catlass变更日志

### CATLASS 1.5.0 - Key Features - Added support for the Ascend 950 architecture and its underlying template components. - TLA enhancement: introduced origin_shape, added APIs such as TileView, M

gitblog_00129的博客 401

asc-devkit矩阵乘法最佳实践

This example is based on the static Tensor programming paradigm, implementing high-performance matrix multiplication through multiple optimization methods including L1/L0 double buffering, large packe

gitblog_01155的博客 803

美赛必备:数学建模常用模型英文对照表与术语应用指南

在数学建模与数据分析领域,掌握核心模型与算法的标准英文术语是进行高效文献检索、国际学术交流以及撰写专业论文的基础。从概念上讲,无论是优化领域的线性规划、遗传算法,还是预测分析中的时间序列模型、灰色预测,每个模型都有其广泛接受的国际命名规范。理解这些术语的构成原理,例如“方法核心+应用对象”的组合规律,不仅能避免沟通歧义,更是严谨学术表达的体现。其技术价值在于,精准的术语使用能直接提升文献调研的效率和模型描述的专业性,是研究成果获得国际认可的前提。在应用场景上,这尤其体现在像美国大学生数学建模竞赛这类国际赛事

weixin_30514745的博客 419

NGC_Overview _ Navigation Guidance and Control

NGC: Navigation, Guidance and ControlIn a broad sense, navigation and guidance both refer to the process or technology of determining position, planning a route, and guiding an object to a destination. In military and engineering contexts, guidance typical

eillot_qian的博客 112

【课程设计】(附源码)基于Spring Boot的个人博客系统的设计与实现 毕业设计271611

个人博客系统主要为用户提供博客资讯、博客资讯分类、博客分类、博客中心、数据分析等功能,用户通过Internet可以发表一些自已撰写的文章以和其他网友进行交流。博客系统主要实现了文章管理的数字化、信息化、智能化,是打破传统报刊、杂志发表文章方式的新尝试。 本系统的开发设计实现采用springboot技术,系统后台使用JavaScript的Mysql数据库,并通过使用Java技术访问。

Biye_Design的博客 5637
上一篇: TODO: house robber IIII
下一篇: Maximum array distance Sum
ActiveCoder
博客等级 码龄11年 4粉丝 345原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值