力扣第11刷-公交站间的距离

简介: 力扣第11刷-公交站间的距离

Example 11

公交站间的距离

题目概述:环形公交路线上有n个站,按次序从0到n - 1进行编号。我们已知每一对相邻公交站之间的距离,distance[i]表示编号为i的车站和编号为(i + 1) % n的车站之间的距离。

环线上的公交车都可以按顺时针和逆时针的方向行驶。

返回乘客从出发点start到目的地destination之间的最短距离。

示例 1:

图片1.png

输入:distance = [1,2,3,4], start = 0, destination = 1

输出:1

解释:公交站 0 和 1 之间的距离是 1 或 9,最小值是 1。

示例 2:

图片2.png

输入:distance = [1,2,3,4], start = 0, destination = 2

输出:3

解释:公交站 0 和 2 之间的距离是 3 或 7,最小值是 3。

示例 3:

图片3.png

输入:distance = [1,2,3,4], start = 0, destination = 3

输出:4

解释:公交站 0 和 3 之间的距离是 6 或 4,最小值是 4。

解题思路:记数组distance 的长度为n。假设start≤destination,那么可以:

从start到destination,距离为

从start 到0,再从0到destination,距离为

答案为这两个距离的最小值。

解题步骤:

1. 因为在后续处理中涉及到循环问题,默认初始点总是小于终点,因此,首先判断初始点是否小于终点,若不小于,则将初始点与终点对调,以保证初始点总是小于终点,且最终结果不变。

2. 定义变量sum1和sum2,分别记录正行与逆行的距离,其初始值均为0。

3. 因整条路径是环形路段,因此正行路段与逆行路段之和为整条路径的和,则遍历整条路径,若某支分路径编号在[start,destination)之间,则其属于正行路段,将其加到sum1中,否则说明其属于逆行路段,将其加到sum2中。

4. 遍历结束后,将sum1与sum2的较小值返回,即为最短路径。

 

示例代码如下:

public class DistanceFromBusStop {
    /**
     * 环形公交路线上有n个站,按次序从0到n - 1进行编号。我们已知每一对相邻公交站之间的距离,distance[i]表示编号为i的车站和编号为(i + 1) % n的车站之间的距离。
     * 环线上的公交车都可以按顺时针和逆时针的方向行驶。
     * 返回乘客从出发点start到目的地destination之间的最短距离。
     * 示例 1:
     * 输入:distance = [1,2,3,4], start = 0, destination = 1
     * 输出:1
     * 解释:公交站 0 和 1 之间的距离是 1 或 9,最小值是 1。
     * 示例 2:
     * 输入:distance = [1,2,3,4], start = 0, destination = 2
     * 输出:3
     * 解释:公交站 0 和 2 之间的距离是 3 或 7,最小值是 3。
     * 示例 3:
     * 输入:distance = [1,2,3,4], start = 0, destination = 3
     * 输出:4
     * 解释:公交站 0 和 3 之间的距离是 6 或 4,最小值是 4。
     * 来源:力扣(LeetCode)
     * 链接:https://leetcode.cn/problems/distance-between-bus-stops
     */
    public static void main(String[] args) {
        DistanceFromBusStop dfbs = new DistanceFromBusStop();
        System.out.println(dfbs.distanceBetweenBusStops(new int[]{7, 10, 1, 12, 11, 14, 5, 0}, 7, 2));
    }
    /**
     * 个人
     * @param distance
     * @param start
     * @param destination
     * @return
     */
    /*public int distanceBetweenBusStops(int[] distance, int start, int destination) {
        int n = distance.length;
        int totalLength = 0;
        for (int i : distance) {
            totalLength += i;
        }
        int distanceOne = 0;
        int begin = Math.min(start, destination);
        int end = Math.max(start, destination);
        for (int i = begin; i < end; i++) {
            distanceOne += distance[i];
        }
        return Math.min(distanceOne, totalLength -distanceOne);
    }*/
    /**
     * 官方
     *
     * @param distance
     * @param start
     * @param destination
     * @return
     */
    public int distanceBetweenBusStops(int[] distance, int start, int destination) {
        if (start > destination) {
            int temp = start;
            start = destination;
            destination = temp;
        }
        int sum1 = 0, sum2 = 0;
        for (int i = 0; i < distance.length; i++) {
            if (i >= start && i < destination) {
                sum1 += distance[i];
            } else {
                sum2 += distance[i];
            }
        }
        return Math.min(sum1, sum2);
    }
}
相关文章
|
4天前
|
算法
【力扣】55.跳跃游戏
【力扣】55.跳跃游戏
|
12月前
|
算法 C++ Python
每日算法系列【LeetCode 719】找出第 k 小的距离对
每日算法系列【LeetCode 719】找出第 k 小的距离对
|
存储
Leetcode-每日一题1210. 穿过迷宫的最少移动次数(BFS)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_46618592/article/details/128890280?spm=1001.2014.3001.5501
90 0
Leetcode-每日一题1210. 穿过迷宫的最少移动次数(BFS)
LeetCode每日一题——1184. 公交站间的距离
环形公交路线上有 n 个站,按次序从 0 到 n - 1 进行编号。
80 0
LeetCode每日一题——1184. 公交站间的距离
|
机器学习/深度学习
LeetCode每日一题——719.找出第k小的数对距离
数对 (a,b) 由整数 a 和 b 组成,其数对距离定义为 a 和 b 的绝对差值。
105 0