Epic - Snake Sequence
You are given a grid of numbers. A snakes equence is made up of adjacent numbers such that for each number, the number on the right or the number below it is +1 or -1 its value. For example,
1 3 2 6 8
-9 7 1 -1 2
1 5 0 1 9
In this grid, (3, 2, 1, 0, 1) is a snake sequence. Given a grid, find the longest snake sequences and their lengths (so there can be multiple snake sequences with the maximum length).
简单的动态规划,使用一个相同大小的数组记录到snake sequence该点的长度(默认为1),并维护最大值。
def snake(matrix)
return 0 if matrix.empty?
m , n = matrix.length, matrix[0].length
dp = Array.new(m){Array.new(n,1)}
ans = -1
m.times do |i|
n.times do |j|
dp[i][j] = dp[i-1][j] + 1 if i > 0 and (matrix[i-1][j] - matrix[i][j]).abs == 1
dp[i][j] = dp[i][j-1] + 1 if j > 0 and (matrix[i][j-1] - matrix[i][j]).abs == 1
ans = [ans,dp[i][j]].max
end
end
ans
end
Epic - Snake Sequence的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
随机推荐
- 解决Android开发中,ActiveAndroid和Gson同时使用,对象序列化失败的问题
ActiveAndroid是安卓开发常用的ORM框架. Gson则是Google提供的轻量级序列化框架,非常适合Android开发使用. 但这两者同时使用,会产生序列化失败的问题.你通常会收到如下信息 ...
- JSON对象末尾多余逗号问题
平时开发用的IE10,没发现这个问题,测试人员对系统兼容性测试时发现了在IE7下存在问题. 问题代码如下: var person = { name: "John", age: 25 ...
- ado执行sql查询出现“发送数据流时出现算术溢出”错误
开发一个数据采集监控系统,比较变态的是有将近2000项数据.根据数据类型分多个表存储.数据库访问层采用ado.最近发现当一条sql一次性查询1700多个字段数据后就出现“发送数据流时出现算术溢出”错误 ...
- 如何使用 EXCEL 的筛选功能
假设有一个Excel文档,有两列“姓名”和“成绩”. 现需筛选出成绩 “大于等于90”或者“小于60”的学生. 步骤如下: 1.选中任意一个单元格,点击工具栏上的 数据 - 筛选 - 自动筛选 ,可以 ...
- hbase-0.94.16 在hadoop-1.2.1的安装配置
1. ZooKeeper的安装: ZooKeeper是一个分布式的服务框架.可用于处理分布式的一些数据管理问题,如统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理等. 步骤如下: a. 准 ...
- 第六篇 ORACLE EBS用户界面通用元素或功能背后的道理解析
本篇打算介绍一下ORACLE EBS用户界面(User Interface)中通用的元素或功能背后蕴含的一些道理.这些通用元素或功能包括: List of Values (LOV),值列表 Flexf ...
- leetcode:Insertion Sort List
Sort a linked list using insertion sort. 分析:此题要求在链表上实现插入排序. 思路:插入排序是一种O(n^2)复杂度的算法,基本想法就是每次循环找到一个元素在 ...
- svn:revert to this version 和 revert changes from this version的区别
假设我们有许多个版本,版本号分别是1-10 如果我们在7这里选择revert to this version那么7之后的8,9,10的操作都会被消除 如果在7选择revert changes from ...
- java.lang.ClassNotFoundException
在项目的properties中的Java Build Path里将Order and Export里的类库勾选上.
- spring mvc 自定义转换器
<!-- 注册转化器 --> <mvc:annotation-driven conversion-service="conversionService" /> ...