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 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- idea更改包名无法加载主类解决
把工程下面的.idea目录下的workspace.xml里面的路径改成你最新的路径即可. <option name="SPRING_BOOT_MAIN_CLASS" valu ...
- GetQueuedCompletionStatus客户端前端和server之间的通信
项目中遇到了这个东西,怎么都调试不到.记录下. 一.完成端口IOCP https://www.cnblogs.com/yuanchenhui/p/iocp_windows.html
- lambda表达式在python和c++中的异同
Lambda表达式是干么的?.lambda表达式首先是一个表达式,是一个函数对象一个匿名函数,但不是函数.现在流行语言例如:JS.PHP都支持一种和面向过程.面向对象并列的函数式编程,lambda就是 ...
- C++类的静态成员笔记
下面是C++类的静态成员笔记. 静态数据成员特征 用关键字static声明 为该类的所有对象共享,静态数据成员具有静态生存期 必须在类外定义和初始化,用(::)来指明所属的类 举例说明-具有静态数据成 ...
- Vue 解决img标签不显示图片问题
今天在写前端页面的时候,上传图片返回图片地址后,<img> 标签居然显示不出来,经过排查,原因是 <img v-if="hotel.url" :src=" ...
- h5返回上一页ios页面不刷新
var isPage=false; window.addEventListener('pageshow', function () { if (isPage) { window.loc ...
- AJAX基本操作
XMLHttpRequest对象: XMLHttpRequest 是 AJAX 的基础.所有现代浏览器均支持 XMLHttpRequest 对象(IE5 和 IE6 使用 ActiveXObject) ...
- 爬虫必知必会(5)_scrapy框架_基础
一.移动端数据的爬取 基于某一款抓包工具,fiddler,青花瓷,miteproxy fillder进行一个基本的配置:tools->options->connection->all ...
- vue 仿zTree折叠树
需求: vue实现仿zTree折叠树,此文章仅作为记录文档. 实现: <template> <div class="line-tree"> <div ...
- Tomcat详解系列(2) - 理解Tomcat架构设计
Tomcat - 理解Tomcat架构设计 前文我们已经介绍了一个简单的Servlet容器是如何设计出来,我们就可以开始正式学习Tomcat了,在学习开始,我们有必要站在高点去看看Tomcat的架构设 ...