Skip to content

[pull] main from doocs:main #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

我们直接模拟摩天轮的轮转过程,每次轮转时,累加等待的游客以及新到达的游客,然后最多 $4$ 个人上船,更新等待的游客数和利润,记录最大利润与其对应的轮转次数。

时间复杂度 $O(\frac{S}{4})$,其中 $S$ 为数组 `customers` 的元素和,即游客总数。空间复杂度 $O(1)$。
时间复杂度 $O(n)$,其中 $n$ 为数组 `customers` 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -182,6 +182,66 @@ func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int)
}
```

### **TypeScript**

```ts
function minOperationsMaxProfit(
customers: number[],
boardingCost: number,
runningCost: number,
): number {
let ans: number = -1;
let [mx, t, wait, i] = [0, 0, 0, 0];
while (wait > 0 || i < customers.length) {
wait += i < customers.length ? customers[i] : 0;
let up: number = Math.min(4, wait);
wait -= up;
++i;
t += up * boardingCost - runningCost;

if (t > mx) {
mx = t;
ans = i;
}
}

return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn min_operations_max_profit(
customers: Vec<i32>,
boarding_cost: i32,
running_cost: i32
) -> i32 {
let mut ans = -1;
let mut mx = 0;
let mut t = 0;
let mut wait = 0;
let mut i = 0;

while wait > 0 || i < customers.len() {
wait += if i < customers.len() { customers[i] } else { 0 };
let up = std::cmp::min(4, wait);
wait -= up;
i += 1;
t += up * boarding_cost - running_cost;

if t > mx {
mx = t;
ans = i as i32;
}
}

ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ The profit was never positive, so return -1.

## Solutions

**Solution 1: Simulation**

We directly simulate the rotation process of the Ferris wheel. Each time it rotates, we add up the waiting customers and the newly arrived customers, then at most $4$ people get on the ride, update the number of waiting customers and profit, and record the maximum profit and its corresponding number of rotations.

The time complexity is $O(n)$, where $n$ is the length of the `customers` array. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -165,6 +171,66 @@ func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int)
}
```

### **TypeScript**

```ts
function minOperationsMaxProfit(
customers: number[],
boardingCost: number,
runningCost: number,
): number {
let ans: number = -1;
let [mx, t, wait, i] = [0, 0, 0, 0];
while (wait > 0 || i < customers.length) {
wait += i < customers.length ? customers[i] : 0;
let up: number = Math.min(4, wait);
wait -= up;
++i;
t += up * boardingCost - runningCost;

if (t > mx) {
mx = t;
ans = i;
}
}

return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn min_operations_max_profit(
customers: Vec<i32>,
boarding_cost: i32,
running_cost: i32
) -> i32 {
let mut ans = -1;
let mut mx = 0;
let mut t = 0;
let mut wait = 0;
let mut i = 0;

while wait > 0 || i < customers.len() {
wait += if i < customers.len() { customers[i] } else { 0 };
let up = std::cmp::min(4, wait);
wait -= up;
i += 1;
t += up * boarding_cost - running_cost;

if t > mx {
mx = t;
ans = i as i32;
}
}

ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
impl Solution {
pub fn min_operations_max_profit(
customers: Vec<i32>,
boarding_cost: i32,
running_cost: i32
) -> i32 {
let mut ans = -1;
let mut mx = 0;
let mut t = 0;
let mut wait = 0;
let mut i = 0;

while wait > 0 || i < customers.len() {
wait += if i < customers.len() { customers[i] } else { 0 };
let up = std::cmp::min(4, wait);
wait -= up;
i += 1;
t += up * boarding_cost - running_cost;

if t > mx {
mx = t;
ans = i as i32;
}
}

ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function minOperationsMaxProfit(
customers: number[],
boardingCost: number,
runningCost: number,
): number {
let ans: number = -1;
let [mx, t, wait, i] = [0, 0, 0, 0];
while (wait > 0 || i < customers.length) {
wait += i < customers.length ? customers[i] : 0;
let up: number = Math.min(4, wait);
wait -= up;
++i;
t += up * boardingCost - runningCost;

if (t > mx) {
mx = t;
ans = i;
}
}

return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# [2980. 检查按位或是否存在尾随零](https://leetcode.cn/problems/check-if-bitwise-or-has-trailing-zeros)

[English Version](/solution/2900-2999/2980.Check%20if%20Bitwise%20OR%20Has%20Trailing%20Zeros/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>给你一个<strong> 正整数 </strong>数组 <code>nums</code> 。</p>

<p>你需要检查是否可以从数组中选出 <strong>两个或更多 </strong>元素,满足这些元素的按位或运算( <code>OR</code>)结果的二进制表示中 <strong>至少</strong><strong> </strong>存在一个尾随零。</p>

<p>例如,数字 <code>5</code> 的二进制表示是 <code>"101"</code>,不存在尾随零,而数字 <code>4</code> 的二进制表示是 <code>"100"</code>,存在两个尾随零。</p>

<p>如果可以选择两个或更多元素,其按位或运算结果存在尾随零,返回 <code>true</code>;否则,返回<em> </em><code>false</code> 。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<pre>
<strong>输入:</strong>nums = [1,2,3,4,5]
<strong>输出:</strong>true
<strong>解释:</strong>如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110" ,存在一个尾随零。
</pre>

<p><strong class="example">示例 2:</strong></p>

<pre>
<strong>输入:</strong>nums = [2,4,8,16]
<strong>输出:</strong>true
<strong>解释:</strong>如果选择元素 2 和 4,按位或运算结果是 6,二进制表示为 "110",存在一个尾随零。
其他按位或运算结果存在尾随零的可能选择方案包括:(2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), 以及 (2, 4, 8, 16) 。
</pre>

<p><strong class="example">示例 3:</strong></p>

<pre>
<strong>输入:</strong>nums = [1,3,5,7,9]
<strong>输出:</strong>false
<strong>解释:</strong>不存在按位或运算结果存在尾随零的选择方案。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

## 解法

<!-- 这里可写通用的实现逻辑 -->

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

```

### **C++**

```cpp

```

### **Go**

```go

```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# [2980. Check if Bitwise OR Has Trailing Zeros](https://leetcode.com/problems/check-if-bitwise-or-has-trailing-zeros)

[中文文档](/solution/2900-2999/2980.Check%20if%20Bitwise%20OR%20Has%20Trailing%20Zeros/README.md)

## Description

<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>

<p>You have to check if it is possible to select <strong>two or more</strong> elements in the array such that the bitwise <code>OR</code> of the selected elements has <strong>at least </strong>one trailing zero in its binary representation.</p>

<p>For example, the binary representation of <code>5</code>, which is <code>&quot;101&quot;</code>, does not have any trailing zeros, whereas the binary representation of <code>4</code>, which is <code>&quot;100&quot;</code>, has two trailing zeros.</p>

<p>Return <code>true</code> <em>if it is possible to select two or more elements whose bitwise</em> <code>OR</code> <em>has trailing zeros, return</em> <code>false</code> <em>otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5]
<strong>Output:</strong> true
<strong>Explanation:</strong> If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation &quot;110&quot; with one trailing zero.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,4,8,16]
<strong>Output:</strong> true
<strong>Explanation: </strong>If we select the elements 2 and 4, their bitwise OR is 6, which has the binary representation &quot;110&quot; with one trailing zero.
Other possible ways to select elements to have trailing zeroes in the binary representation of their bitwise OR are: (2, 8), (2, 16), (4, 8), (4, 16), (8, 16), (2, 4, 8), (2, 4, 16), (2, 8, 16), (4, 8, 16), and (2, 4, 8, 16).
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,5,7,9]
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no possible way to select two or more elements to have trailing zeros in the binary representation of their bitwise OR.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

## Solutions

<!-- tabs:start -->

### **Python3**

```python

```

### **Java**

```java

```

### **C++**

```cpp

```

### **Go**

```go

```

### **...**

```

```

<!-- tabs:end -->
Loading