Weekly Contest 138
1051. Height Checker
Students are asked to stand in non-decreasing order of heights for an annual photo.
Return the minimum number of students not standing in the right positions. (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)
Example 1:
Input: [1,1,4,2,1,3]
Output: 3
Explanation:
Students with heights 4, 3 and the last 1 are not standing in the right positions.
Note:
1 <= heights.length <= 1001 <= heights[i] <= 100
Approach #1: [Java]
class Solution {
public int heightChecker(int[] heights) {
int[] copy = new int[heights.length];
for (int i = 0; i < heights.length; ++i) {
copy[i] = heights[i];
}
Arrays.sort(heights);
int ret = 0;
for (int i = 0; i < heights.length; ++i) {
if (copy[i] != heights[i])
ret++;
}
return ret;
}
}
1052. Grumpy Bookstore Owner
Today, the bookstore owner has a store open for
customers.lengthminutes. Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.On some minutes, the bookstore owner is grumpy. If the bookstore owner is grumpy on the i-th minute,
grumpy[i] = 1, otherwisegrumpy[i] = 0. When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.The bookstore owner knows a secret technique to keep themselves not grumpy for
Xminutes straight, but can only use it once.Return the maximum number of customers that can be satisfied throughout the day.
Example 1:
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
Output: 16
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
Note:
1 <= X <= customers.length == grumpy.length <= 200000 <= customers[i] <= 10000 <= grumpy[i] <= 1
Approach #1: [Java]
class Solution {
public int maxSatisfied(int[] customers, int[] grumpy, int X) {
int s = 0, e = 0;
int getFromX = 0;
int res = 0;
for (int i = 0; i <= grumpy.length - X; ++i) {
int temp = 0;
for (int j = i; j < i + X; ++j) {
if (grumpy[j] == 1) {
temp += customers[j];
}
}
if (temp > getFromX) {
getFromX = temp;
s = i;
e = i + X;
}
}
for (int i = 0; i < s; ++i) {
if (grumpy[i] == 0)
res += customers[i];
}
for (int i = s; i < e; ++i) {
res += customers[i];
}
for (int i = e; i < grumpy.length; ++i) {
if (grumpy[i] == 0)
res += customers[i];
}
return res;
}
}
1053. Previous Permutation With One Swap
Given an array
Aof positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller thanA, that can be made with one swap (A swap exchanges the positions of two numbersA[i]andA[j]). If it cannot be done, then return the same array.
Example 1:
Input: [3,2,1]
Output: [3,1,2]
Explanation: Swapping 2 and 1.Example 2:
Input: [1,1,5]
Output: [1,1,5]
Explanation: This is already the smallest permutation.Example 3:
Input: [1,9,4,6,7]
Output: [1,7,4,6,9]
Explanation: Swapping 9 and 7.Example 4:
Input: [3,1,1,3]
Output: [1,3,1,3]
Explanation: Swapping 1 and 3.
Note:
1 <= A.length <= 100001 <= A[i] <= 10000
Approach #1: [Java]
class Solution {
public int[] prevPermOpt1(int[] A) {
int len = A.length;
int left = len - 2, right = len - 1, tmp;
while (left >= 0 && A[left] <= A[left+1]) left--;
if (left < 0) return A;
while (A[left] <= A[right]) right--;
while (A[right-1] == A[right]) right--;
tmp = A[left];
A[left] = A[right];
A[right] = tmp;
return A;
}
}
1054. Distant Barcodes
In a warehouse, there is a row of barcodes, where the
i-th barcode isbarcodes[i].Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Note:
1 <= barcodes.length <= 100001 <= barcodes[i] <= 10000
Approach #1: PriorityQueue [Java]
class Solution {
public int[] rearrangeBarcodes(int[] barcodes) {
Map<Integer, Integer> memo = new HashMap<>();
for (int i : barcodes) {
memo.put(i, memo.getOrDefault(i, 0) + 1);
}
Queue<Integer> pq = new PriorityQueue<>((a, b)->(memo.get(b) - memo.get(a)));
for (int i : memo.keySet()) {
pq.add(i);
}
int[] ans = new int[barcodes.length];
int k = 0;
int count = 0;
Queue<Integer> wait = new LinkedList<>();
while (!pq.isEmpty()) {
while (!pq.isEmpty() && count < 2) {
int first = pq.poll();
ans[k++] = first;
memo.put(first, memo.get(first) - 1);
wait.add(first);
count++;
}
while (!wait.isEmpty() && wait.size() != 1) {
if (memo.get(wait.peek()) > 0) {
pq.add(wait.poll());
} else {
wait.poll();
}
}
count = 0;
}
return ans;
}
}
Reference:
https://docs.oracle.com/javase/9/docs/api/java/util/PriorityQueue.html
Weekly Contest 138的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- MySQL 事务的隔离级别
转载:https://developer.aliyun.com/article/743691?accounttraceid=80d4fddb3dc64b97a71118659e106221tozz 简 ...
- linux之安装nginx
nginx官网:http://nginx.org/en/download.html 1.安装nginx所需环境 a) PCRE pcre-devel 安装 # yum install -y pcre ...
- 第七届蓝桥杯JavaB组——第7题剪邮票
题目: 剪邮票 如[图1.jpg], 有12张连在一起的12生肖的邮票. 现在你要从中剪下5张来,要求必须是连着的. (仅仅连接一个角不算相连) 比如,[图2.jpg],[图3.jpg]中,粉红色所示 ...
- 翻译:《实用的Python编程》03_02_More_functions
目录 | 上一节 (3.1 脚本) | 下一节 (3.3 错误检查) 3.2 深入函数 尽管函数在早先时候介绍了,但有关函数在更深层次上是如何工作的细节却很少提供.本节旨在填补这些空白,并讨论函数调用 ...
- k8s自定义controller设计与实现
k8s自定义controller设计与实现 创建CRD 登录可以执行kubectl命令的机器,创建student.yaml apiVersion: apiextensions.k8s.io/v1bet ...
- HDOJ-6621(线段树+二分法)
K-th Closest Distance HDOJ-6621 本题可以使用线段树解决,结点存本结点对应的所有元素,并按照从小打到排序 最后使用二分法求解答案.因为题目中有绝对值,所以需要使用两次查找 ...
- Linux速通08 网络原理及基础设置、软件包管理
使用 ifconfig命令来维护网络 # ifconfig 命令:显示所有正在启动的网卡的详细信息或设定系统中网卡的 IP地址 # 应用 ifconfig命令设定网卡的 IP地址: * 例:修改 et ...
- Git 常用命令 和 安装
这年头不会点git还真不能与别人进行代码交流 安装 windowns版下载:https://git-scm.com/download/win ,下载完成后就自己手动安装 ,很简单就不多说. Ubunt ...
- STM32 ADC详细篇(基于HAL库)
一.基础认识 ADC就是模数转换,即将模拟量转换为数字量 l 分辨率,读出的数据的长度,如8位就是最大值为255的意思,即范围[0,255],12位就是最大值为4096,即范围[0,4096] l ...
- GreenDao3.2使用详解(增,删,改,查,升级)
首先看一下效果图: 项目结构如下图所示: 第一步:在build中添加配置如下: projet 目录下的build.gradle dependencies { classpath 'org.greenr ...