LeetCode 3285.找到稳定山的下标:遍历

【LetMeFly】3285.找到稳定山的下标:遍历

力扣题目链接:https://leetcode.cn/problems/find-indices-of-stable-mountains/

有 n 座山排成一列,每座山都有一个高度。给你一个整数数组 height ,其中 height[i] 表示第 i 座山的高度,再给你一个整数 threshold 。

对于下标不为 0 的一座山,如果它左侧相邻的山的高度 严格大于 threshold ,那么我们称它是 稳定 的。我们定义下标为 0 的山 不是 稳定的。

请你返回一个数组,包含所有 稳定 山的下标,你可以以 任意 顺序返回下标数组。

 

示例 1:

输入:height = [1,2,3,4,5], threshold = 2

输出:[3,4]

解释:

  • 下标为 3 的山是稳定的,因为 height[2] == 3 大于 threshold == 2 。
  • 下标为 4 的山是稳定的,因为 height[3] == 4 大于 threshold == 2.

示例 2:

输入:height = [10,1,10,1,10], threshold = 3

输出:[1,3]

示例 3:

输入:height = [10,1,10,1,10], threshold = 10

输出:[]

 

提示:

  • 2 <= n == height.length <= 100
  • 1 <= height[i] <= 100
  • 1 <= threshold <= 100

解题方法:遍历

i i i 1 1 1 n − 1 n-1 n1遍历山峰,如果 h e i g h t [ i − 1 ] > t h r e s h o l d height[i - 1] \gt threshold height[i1]>threshold,则将 i i i添加到答案数组中。

  • 时间复杂度 O ( n ) O(n) O(n)
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
/*
 * @Author: LetMeFly
 * @Date: 2024-12-19 15:53:21
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2024-12-19 15:53:31
 */
class Solution {
public:
    vector<int> stableMountains(vector<int>& height, int threshold) {
        vector<int> ans;
        for (int i = 1; i < height.size(); i++) {
            if (height[i - 1] > threshold) {
                ans.push_back(i);
            }
        }
        return ans;
    }
};
Python
'''
Author: LetMeFly
Date: 2024-12-19 15:58:23
LastEditors: LetMeFly.xyz
LastEditTime: 2024-12-19 16:11:17
'''
from typing import List

class Solution:
    def stableMountains(self, height: List[int], threshold: int) -> List[int]:
        return [i for i in range(1, len(height)) if height[i - 1] > threshold]
Java
/*
 * @Author: LetMeFly
 * @Date: 2024-12-19 16:12:01
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2024-12-19 16:27:42
 */
import java.util.List;
import java.util.ArrayList;

class Solution {
    public List<Integer> stableMountains(int[] height, int threshold) {
        List<Integer> ans = new ArrayList<>();
        for (int i = 1; i < height.length; i++) {
            if (height[i - 1] > threshold) {
                ans.add(i);
            }
        }
        return ans;
    }
}
Go
/*
 * @Author: LetMeFly
 * @Date: 2024-12-19 16:29:10
 * @LastEditors: LetMeFly.xyz
 * @LastEditTime: 2024-12-19 16:29:28
 */
package main

func stableMountains(height []int, threshold int) (ans []int) {
    for i := 1; i < len(height); i++ {
        if height[i - 1] > threshold {
            ans = append(ans, i)
        }
    }
    return
}

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/144596618

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tisfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值