无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

LeetCode 2717. Semi-Ordered Permutation

2023-06-04 12:18 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a?0-indexed?permutation of?n?integers?nums.

A permutation is called?semi-ordered?if the first number equals?1?and the last number equals?n. You can perform the below operation as many times as you want until you make?nums?a?semi-ordered?permutation:

  • Pick two adjacent elements in?nums, then swap them.

Return?the minimum number of operations to make?nums?a?semi-ordered permutation.

A?permutation?is a sequence of integers from?1?to?n?of length?n?containing each number exactly once.

?

Example 1:

Input: nums = [2,1,4,3]Output: 2

Explanation: We can make the permutation semi-ordered using these sequence of operations: 1 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3]. 2 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4]. It can be proved that there is no sequence of less than two operations that make nums a semi-ordered permutation.

Example 2:

Input: nums = [2,4,1,3]Output: 3

Explanation: We can make the permutation semi-ordered using these sequence of operations: 1 - swap i = 1 and j = 2. The permutation becomes [2,1,4,3]. 2 - swap i = 0 and j = 1. The permutation becomes [1,2,4,3]. 3 - swap i = 2 and j = 3. The permutation becomes [1,2,3,4]. It can be proved that there is no sequence of less than three operations that make nums a semi-ordered permutation.

Example 3:

Input: nums = [1,3,4,2,5]Output: 0Explanation: The permutation is already a semi-ordered permutation.

?

Constraints:

  • 2 <= nums.length == n <= 50

  • 1 <= nums[i]?<= 50

  • nums is a permutation.

因為是必須2個相鄰的進行翻轉(zhuǎn),那么我們就找到最大值跟最小值的位置,如果最大值在最小值的左邊,那么他倆一定會進行交換的,所以這時候總的交換次數(shù)減1即可;

其他見代碼:

Runtime:?2 ms, faster than?100.00%?of?Java?online submissions for?Semi-Ordered Permutation.

Memory Usage:?43.5 MB, less than?20.00%?of?Java?online submissions for?Semi-Ordered Permutation.


LeetCode 2717. Semi-Ordered Permutation的評論 (共 條)

分享到微博請遵守國家法律
天镇县| 晋州市| 大悟县| 平江县| 游戏| 汝州市| 黑山县| 白银市| 会昌县| 西和县| 巴彦淖尔市| 蒲城县| 吉木乃县| 曲阳县| 翁牛特旗| 历史| 旅游| 西安市| 安塞县| 阳西县| 泽州县| 东城区| 科技| 冕宁县| 衡南县| 宜章县| 保靖县| 顺昌县| 大同县| 兖州市| 汾阳市| 蓬安县| 清丰县| 柳河县| 洛南县| 喀喇沁旗| 当阳市| 应用必备| 饶河县| 铜梁县| 九台市|