1124:表现良好的最长时间段

This commit is contained in:
huangge1199 2021-05-10 15:19:03 +08:00
parent 970a919cac
commit 45a49d65cc
4 changed files with 118 additions and 1 deletions

View File

@ -0,0 +1,59 @@
//给你一份工作时间表 hours上面记录着某一位员工每天的工作小时数
//
// 我们认为当员工一天中的工作小时数大于 8 小时的时候那么这一天就是劳累的一天
//
// 所谓表现良好的时间段意味在这段时间内劳累的天数是严格 大于不劳累的天数
//
// 请你返回表现良好时间段的最大长度
//
//
//
// 示例 1
//
// 输入hours = [9,9,6,0,6,6,9]
//输出3
//解释最长的表现良好时间段是 [9,9,6]
//
//
//
// 提示
//
//
// 1 <= hours.length <= 10000
// 0 <= hours[i] <= 16
//
// Related Topics
// 👍 136 👎 0
package leetcode.editor.cn;
//1124:表现良好的最长时间段
public class LongestWellPerformingInterval {
public static void main(String[] args) {
//测试代码
Solution solution = new LongestWellPerformingInterval().new Solution();
System.out.println(solution.longestWPI(new int[]{9,9,6,0,6,6,9}));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int longestWPI(int[] hours) {
int length = hours.length;
int sum = 0;
int max = 0;
for (int i = 0; i < length; i++) {
for (int j = i; j < length; j++) {
sum += hours[j] > 8 ? -1 : 1;
if (sum < 0) {
max = Math.max(max, j - i + 1);
}
}
sum = 0;
}
return max;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,25 @@
<p>给你一份工作时间表&nbsp;<code>hours</code>,上面记录着某一位员工每天的工作小时数。</p>
<p>我们认为当员工一天中的工作小时数大于&nbsp;<code>8</code> 小时的时候,那么这一天就是「<strong>劳累的一天</strong>」。</p>
<p>所谓「表现良好的时间段」,意味在这段时间内,「劳累的天数」是严格<strong> 大于</strong>「不劳累的天数」。</p>
<p>请你返回「表现良好时间段」的最大长度。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>hours = [9,9,6,0,6,6,9]
<strong>输出:</strong>3
<strong>解释:</strong>最长的表现良好时间段是 [9,9,6]。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= hours.length &lt;= 10000</code></li>
<li><code>0 &lt;= hours[i] &lt;= 16</code></li>
</ul>
<div><div>Related Topics</div><div><li></li></div></div>\n<div><li>👍 136</li><li>👎 0</li></div>

View File

@ -0,0 +1,33 @@
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int size = nums1.length;
int[] result = new int[size];
Stack<Integer> stack = new Stack<>();
for (int i = size - 1; i >= 0; i--) {
result[i] = -2;
stack.push(nums1[i]);
}
int length = nums2.length;
int index = 0;
while (!stack.isEmpty()) {
int i;
for (i = 0; i < length; i++) {
if (result[index] == -2 && nums2[i] == stack.peek()) {
result[index] = -1;
} else if (result[index] == -1 && nums2[i] > stack.peek()) {
result[index] = nums2[i];
stack.pop();
break;
}
}
if (i == length && stack.peek() != result[index]) {
stack.pop();
}
index++;
}
return result;
}
}
//runtime:40 ms
//memory:38.7 MB

File diff suppressed because one or more lines are too long