增加leetcodeQ46

This commit is contained in:
fanxb 2020-06-09 17:44:36 +08:00
parent 9a9684458c
commit b11f2c9c27
2 changed files with 131 additions and 53 deletions

View File

@ -1,53 +1,53 @@
--- ---
id: "2019-01-02-17-15" id: "2019-01-02-17-15"
date: "2019/01/02 17:15" date: "2019/01/02 17:15"
title: "Q807 保持城市天际线(Max Increase to Keep City Skyline)" title: "Q807 保持城市天际线(Max Increase to Keep City Skyline)"
tags: ["java", "leetcode","array",'leetcode'] tags: ["java", "leetcode","array",'leetcode']
categories: categories:
- "算法" - "算法"
- "leetcode刷题" - "leetcode刷题"
--- ---
### 解析思路 ### 解析思路
  leetcode 中等难度中比较简单的一个,题目描述[看这里](https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/),最开始看题目是比较懵逼的第一遍没有看懂多看几次才明白其实就是让每个点增大到不超过本行且本列的最大值输出增加的值的和。so 解题方法就是算出每行每列的最大值,然后每个点与当前行当前列最大值中较小的一个差值的和就是结果。   leetcode 中等难度中比较简单的一个,题目描述[看这里](https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/),最开始看题目是比较懵逼的第一遍没有看懂多看几次才明白其实就是让每个点增大到不超过本行且本列的最大值输出增加的值的和。so 解题方法就是算出每行每列的最大值,然后每个点与当前行当前列最大值中较小的一个差值的和就是结果。
### 代码(Java实现) ### 代码(Java实现)
```java ```java
class Solution { class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) { public int maxIncreaseKeepingSkyline(int[][] grid) {
int rowLength = grid.length; int rowLength = grid.length;
int columnLength = grid[0].length; int columnLength = grid[0].length;
//每行对应的最大值 //每行对应的最大值
int[] rowMax = new int[rowLength]; int[] rowMax = new int[rowLength];
//每列对应的最大值 //每列对应的最大值
Arrays.fill(rowMax, -1); Arrays.fill(rowMax, -1);
int[] columnMax = new int[columnLength]; int[] columnMax = new int[columnLength];
Arrays.fill(columnMax, -1); Arrays.fill(columnMax, -1);
int sum = 0; int sum = 0;
for (int i = 0; i < rowLength; i++) { for (int i = 0; i < rowLength; i++) {
for (int j = 0; j < columnLength; j++) { for (int j = 0; j < columnLength; j++) {
if (rowMax[i] == -1) { if (rowMax[i] == -1) {
//计算行最大值 //计算行最大值
for (int temp = 0; temp < columnLength; temp++) { for (int temp = 0; temp < columnLength; temp++) {
if (rowMax[i] < grid[i][temp]) { if (rowMax[i] < grid[i][temp]) {
rowMax[i] = grid[i][temp]; rowMax[i] = grid[i][temp];
} }
} }
} }
if (columnMax[j] == -1) { if (columnMax[j] == -1) {
//计算列最大值 //计算列最大值
for (int temp = 0; temp < rowLength; temp++) { for (int temp = 0; temp < rowLength; temp++) {
if (columnMax[j] < grid[temp][j]) { if (columnMax[j] < grid[temp][j]) {
columnMax[j] = grid[temp][j]; columnMax[j] = grid[temp][j];
} }
} }
} }
sum += Math.min(rowMax[i], columnMax[j]) - grid[i][j]; sum += Math.min(rowMax[i], columnMax[j]) - grid[i][j];
} }
} }
return sum; return sum;
} }
} }
``` ```

View File

@ -0,0 +1,78 @@
---
id: "20200609"
date: "2020/06/09 17:15"
title: "Q46 把数字翻译成字符串"
tags: ["java", "leetcode", "动态规划"]
categories:
- "算法"
- "leetcode刷题"
---
题目描述见:[点击跳转](https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof/)
### 解析思路
&emsp;&emsp;leetcode 中国中的一个中等难度面试题——把数字翻译成字符串,是一个较为简单的动态规划问题(虽然简单我也不会呀)。
咋一看这个题目描述是懵逼的,思考 10 分钟无果,果断看了解题思路,豁然开朗。
假设数字的长度为$n$,第$i$个数为$x_i$,长度为$n$的数字结果为$f(n)$
我们开始找规律:
- $n=0$时,$f(0)=1$
- $n=1$时,$f(1)=1$
- $n=2$时,$f(2)=2$
- $n=3$,假设前两位是 12
<br/>当第三位能和 2 合体时(比如为 5我们可以把 25 当作一个组合,剩余数字$f(1)$的结果就是此种情况下的结果数。另外一种情况是将 5 作为一个组合,剩余数字$f(2)$的结果为此种情况下的结果数。总的为$f(1)+f(2)$
<br/>当第三位不能和第二位合体(比如 8就只能将 5 作为一个整体,此种情况下的结果为$f(2)$
<br/>.
<br/>.
<br/>.
- 依次类推,能得到如下这样一个推导式:
$$
f(n)=\begin{cases}
f(n-1)+f(n-2),\quad 10 \le x_nx_{n-1} \le 25 \\\\
f(n-1), \quad x_nx_{n-1} <10 \parallel x_nx_{n-1}> 25
\end{cases}
$$
思考为什么能得到上面的推导式:
- 当一串数字 n 加一个数字的时候,如果这个数字不能和前一个数字组成一个整体,那么实际上结果数是不变的,还是$f(n-1)$
- 如果能组成那就分成两种情况,将这个数字单独作为一个整体,那么还是和上面一样结果是$f(n-1)$,两外一种是和前一个数字凑成一个整体,这种情况就相当于长度为 n-2 是加上了一个数字,那么结果就是$f(n-2)$
综合起来就得到了上面的推导公式
### 代码(Java 实现)
```java
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/9 15:10
*/
public class Q46 {
public int translateNum(int num) {
String str = String.valueOf(num);
//a=f(0),b=f(1)
int a = 1, b = 1, sum = 1;
for (int i = 1, length = str.length(); i < length; i++) {
String temp = str.substring(i - 1, i + 1);
sum = temp.compareTo("10") >= 0 && temp.compareTo("25") <= 0 ? a + b : b;
a = b;
b = sum;
}
return sum;
}
public static void main(String[] args) {
System.out.println(new Q46().translateNum(12258));
}
}
```
本文原创发布于:[www.tapme.top/blog/detail/20190806](www.tapme.top/blog/detail/20200609)