2019-06-16 14:35:52

  • 1089. Duplicate Zeros - Easy

问题描述:

问题求解:

很显然的可以使用O(n), O(n)的解法。主要问题在于如何在O(1)空间复杂度完成求解。

答案就是通过两次遍历,第一次求出复制后的数组的长度,第二次遍历填写数组。

    public void duplicateZeros(int[] arr) {
if (arr.length == 1) return;
final int n = arr.length;
int newLen = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == 0) newLen++;
newLen++;
}
for (int i = n - 1; i >= 0; i--) {
if (--newLen < n) arr[newLen] = arr[i];
if (arr[i] == 0 && --newLen < n) arr[newLen] = 0;
}
}

  

  • 1090. Largest Values From Labels - Medium

问题描述:

问题求解:

本题要求的是挑选至多num_wanted个数的物品,并且每个物品不能超过limit的数目。

显然的是,可以根据价值来贪心的选取,问题就是如何不超过limit的数目,也就是将value和label建立联系。这个建立联系的方式是需要经验的,这里采用的是组pair的方式建立联系,事实上,很多时候这种建立pair的方式是最行之有效的,可以特别关注一下。

    public int largestValsFromLabels(int[] values, int[] labels, int num_wanted, int use_limit) {
List<int[]> pairs = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
final int n = values.length;
for (int i = 0; i < n; i++) {
if (!map.containsKey(labels[i])) map.put(labels[i], 0);
pairs.add(new int[]{values[i], labels[i]});
}
PriorityQueue<int[]> pq = new PriorityQueue<>((int[] o1, int[] o2) -> o2[0] - o1[0]);
pq.addAll(pairs);
int res = 0;
for (int i = 0; i < num_wanted && !pq.isEmpty();) {
int[] curPair = pq.poll();
if (map.get(curPair[1]) < use_limit) {
res += curPair[0];
map.put(curPair[1], map.get(curPair[1]) + 1);
i++;
}
}
return res;
}

  

  • 1091. Shortest Path in Binary Matrix - Medium

问题描述:

问题求解:

裸的BFS。

    public int shortestPathBinaryMatrix(int[][] grid) {
if (grid.length == 0 || grid[0].length == 0) return -1;
final int n = grid.length;
final int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
if (grid[0][0] != 0 || grid[n - 1][n - 1] != 0) return -1;
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{0, 0});
grid[0][0] = 2;
int step = 0;
while (!q.isEmpty()) {
int size = q.size();
step++;
for (int i = 0; i < size; i++) {
int[] curNode = q.poll();
for (int[] dir : dirs) {
int x = curNode[0] + dir[0];
int y = curNode[1] + dir[1];
if (x < 0 || x >= n || y < 0 || y >= n || grid[x][y] != 0) continue;
if (x == n - 1 && y == n - 1) return step + 1;
q.add(new int[]{x, y});
grid[x][y] = 2;
}
}
}
return -1;
}

 

  • 1092. Shortest Common Supersequence - Hard

问题描述:

问题求解:

问题需要的是最短的supersequence,那么就是需要将两个字符串拼接后能够最大限度的去除重复的字符,因此本题就变为了求最长公共子序列的问题,并且需要构造出最长公共子序列。

这里直接暴力的进行构造。

    public String shortestCommonSupersequence(String s1, String s2) {
if (s1.length() == 0) return s2;
if (s2.length() == 0) return s1;
int m = s1.length();
int n = s2.length();
String common = lcs(s1, s2);
System.out.println(common);
StringBuffer sb = new StringBuffer();
int i = 0;
int j = 0;
for (char c : common.toCharArray()) {
while (i < m && s1.charAt(i) != c) {
sb.append(s1.charAt(i));
i++;
}
while (j < n && s2.charAt(j) != c) {
sb.append(s2.charAt(j));
j++;
}
sb.append(c);
i++;
j++;
}
while (i < m) sb.append(s1.charAt(i++));
while (j < n) sb.append(s2.charAt(j++));
return sb.toString();
} private String lcs(String s1, String s2) {
int m = s1.length();
int n = s2.length();
String[][] dp = new String[m + 1][n + 1];
for (int j = 0; j <= n; j++) dp[0][j] = "";
for (int i = 0; i <= m; i++) dp[i][0] = "";
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1] + s1.charAt(i - 1);
else if (dp[i - 1][j].length() > dp[i][j - 1].length()) dp[i][j] = dp[i - 1][j];
else dp[i][j] = dp[i][j - 1];
}
}
return dp[m][n];
}

  

 

Contest 141的更多相关文章

  1. floj 2265 【lxs Contest #141】航海舰队

    首先抠出包围了阵形的最小矩形. 将地图拉伸成一条链,即将第一行.第二行.第三行按顺序连接.阵形也可以用同样的方法处理. 那么问题转化为,给定两个 01 串 S 和 T,问每个 S 中长度为 |T| 的 ...

  2. 牛客网多校第3场C-shuffle card 平衡树或stl(rope)

    链接:https://www.nowcoder.com/acm/contest/141/C 来源:牛客网 题目描述 Eddy likes to play cards game since there ...

  3. 牛客多校第三场 A- PACM Team 背包/记忆路径

    https://www.nowcoder.com/acm/contest/141#question 一眼背包,用四维dp记录在A,B,C,D条件限制下可以获得的最大知识点,但是题目要求输出路径,在输入 ...

  4. 牛客网多校第3场Esort string (kmp)

    链接:https://www.nowcoder.com/acm/contest/141/E 来源:牛客网 题目描述 Eddy likes to play with string which is a ...

  5. 牛客多校第三场 A—pacm team (4维背包加路径压缩)

    链接:https://www.nowcoder.com/acm/contest/141/A 来源:牛客网 Eddy was a contestant participating , Eddy fail ...

  6. 牛客第三场多校 E Sort String

    链接:https://www.nowcoder.com/acm/contest/141/E来源:牛客网 Eddy likes to play with string which is a sequen ...

  7. 牛客第三场多校 H Diff-prime Pairs

    链接:https://www.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy has solved lots of problem involving calcul ...

  8. 2018牛客网暑期ACM多校训练营(第三场) H - Shuffle Cards - [splay伸展树][区间移动][区间反转]

    题目链接:https://www.nowcoder.com/acm/contest/141/C 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  9. 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]

    题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

随机推荐

  1. Ubuntu 16.04 PXE+kickstart部署系统

    #PXE+TFTP+Kickstart 自动部署服务器系统系统Ubuntu16.04apt-get install isc-dhcp-servervim /etc/default/isc-dhcp-s ...

  2. 【系统篇】Archlinux系统安装

    本教程为最新安装Linux的教程,想看更详细可以到我B站主页看视频教程 ArchLinux安装配置手册[系统篇] 本教程参考自 https://wiki.archlinux.org/index.php ...

  3. C++扬帆远航——6(三色球)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:sanseqiu.cpp * 作者:常轩 * 完成日期:2016 ...

  4. Vue+Axios+iview+vue-router实战管理系统项目

    项目实现登陆验证,iviewui组件使用,vue-router路由拦截,在删除登陆存储的token后,直接路由进登陆页面,实战数据请求,本地data.json,笔者也是初出茅庐,在项目运行后,会一直c ...

  5. ubuntu16.04设置开机自启服务

    网上说了开机自启有许多种方法: 1.最简单的是:在/etc/rc.local的exit 0前面加上你启动服务的脚本文件路径 注:这个脚本文件应写绝对路径! 2.网上:修改rc.local开头的#/bi ...

  6. css 居中方法

    垂直居中 利用“精灵元素”(ghost element)技术实现垂直居中,即在父容器内放一个100%高度的伪元素,让文本和伪元素垂直对齐,从而达到垂直居中的目的. .ghost-center { po ...

  7. 10个机器学习人工智能开发框架和AI库(优缺点对比表)/贪心学院

    概述 通过本文我们来一起看一些用于人工智能的高质量AI库,它们的优点和缺点,以及它们的一些特点. 人工智能(AI)已经存在很长时间了.然而,由于这一领域的巨大进步,近年来它已成为一个流行语.人工智能曾 ...

  8. URL及short URL短网址

    URL,uniform resource locator,经常被称为网址,尤其是在使用HTTP的时候.通常是一个指向某个资源的字符串.   URLs经常被用于网页(http),但也可以用于文件传输(f ...

  9. vue基础 ref的作用

    1.  ref 获取dom元素,除了能获取dom元素也能获取组件dom,   组件通信:        在父组件中直接调用ref定义的组件的数据或者方法 <div id="app&qu ...

  10. 如何安装vue-devtool调试工具

    1.从git上下载工具压缩包,github下载地址:https://github.com/vuejs/vue-devtools: 2.打开cmd,切换到下载的文件目录下:npm install---- ...