计算从位置 x 到 y 的最少步数

76 阅读6分钟

计算从位置 x 到 y 的最少步数 问题描述 小F正在进行一个 AB 实验,需要从整数位置 x 移动到整数位置 y。每一步可以将当前位置增加或减少,且每步的增加或减少的值必须是连续的整数(即每步的移动范围是上一步的 -1,+0 或 +1)。首末两步的步长必须是 1。求从 x 到 y 的最少步数。

输入描述 输入包含两个整数 x 和 y,表示起始位置和目标位置。

输出描述 输出从 x 到 y 所需的最小步数。

测试样例 样例1:

输入:x_position = 12, y_position = 6 输出:4

样例2:

输入:x_position = 34, y_position = 45 输出:6

样例3:

输入:x_position = 50, y_position = 30 输出:8

样例4:

输入:x_position = 0, y_position = 0 输出:0

def max_step(x): d = x // 2 step = (1 + d) * d if(x%2): step += d + 1 return step def solution(x_position, y_position): # Please write your code here dis = y_position - x_position if dis < 0 : dis = -dis l = 1 r = dis min_step = r while l <= r: mid = l + r >> 1 if max_step(mid) >= dis: min_step = mid r = mid - 1 else : l = mid + 1

return min_step

if name == "main": # You can add more test cases here print(solution(12, 6) == 4 ) print(solution(34, 45) == 6) print(solution(50, 30) == 8)

' 运行运行 兔生兔 兔群繁殖之谜 题目描述

问题描述

  • 如果一对兔子每月生一对兔子;一对新生兔,从第二个月起就开始生兔子;假定每对兔子都是一雌一雄,试问一对兔子,第 n 个月能繁殖成多少对兔子?(举例,第1个月是1对兔子,第2个月是2对兔子)

输入格式

  • 数字

输出格式

  • 数字

输入样例

  • 5

输出样例

  • 8

数据范围

  • [1, 75]

测试数据集

  • 样例1
    • 输入:5
    • 输出:8
  • 样例2
    • 输入:1
    • 输出:1
  • 样例3
    • 输入:15
    • 输出:987
  • 样例4
    • 输入:50
    • 输出:20365011074 方法 斐波拉切数列 用递归的话记得写记忆化搜索保证时间复杂度

vis = [False for _ in range(76)] F = [0 for _ in range(76)] def calc_fbi(x): if vis[x] == True: return F[x] if x==1 or x==0 : return 1 vis[x] = True F[x] = calc_fbi(x-1) + calc_fbi(x-2) return F[x] def solution(A): # Edit your code here return calc_fbi(A)

if name == "main": # Add your test cases here print(solution(5) == 8) print(solution(1) == 1) print(solution(15) == 987) print(solution(50) == 20365011074)

' 运行运行 找单独的数
题目描述

问题描述

有一堆数字,除了一个数字,其它的数字都是成对出现。班上的每个同学拿一个数字,正好将这些数字全部拿完,问如何快速找到拿了单独数字的同学?

输入格式

  • 空格分隔输入所有的数字

输出格式

  • 单独的那个数字

输入样例(1)

1 1 2 2 3 3 4 5 5

输出样例(1)

4

输入样例(2)

0 1 0 1 2

输出样例(2)

2

方法 经典的异或

def solution(inp): # Edit your code here res = 0 for x in inp: res = res ^ x return res

if name == "main": # Add your test cases here

print(solution([1, 1, 2, 2, 3, 3, 4, 5, 5]) == 4)
print(solution([0, 1, 0, 1, 2]) == 2)

打点计数器的区间合并 问题描述 小明正在设计一台打点计数器,该计数器可以接受多个递增的数字范围,并对这些范围内的每个唯一数字打点。如果多个范围之间有重叠,计数器将合并这些范围并只对每个唯一数字打一次点。小明需要你帮助他计算,在给定的多组数字范围内,计数器会打多少个点。

例如,给定三个数字范围 [1, 4], [7, 10], 和 [3, 5],计数器首先将这些范围合并,变成 [1, 5] 和 [7, 10],然后计算这两个范围内共有多少个唯一数字,即从 1 到 5 有 5 个数字,从 7 到 10 有 4 个数字,共打 9 个点。

测试样例 样例1:

输入:inputArray = [[1, 4], [7, 10], [3, 5]] 输出:7

样例2:

输入:inputArray = [[1, 2], [6, 10], [11, 15]] 输出:9

样例3:

输入:inputArray = [[1, 3], [2, 6], [8, 10]] 输出:7

注意这里其实有个问题 不知道题目是不是直接用ai生成的 这里的区间应该是不包含一端的端点才对

方法 直接将区间按照左端点为第一关键字从小到大, 右端点第一二键字从大到小排序, 然后记录处理的区间和当前的区间 求 ∪。

def solution(inputArray): # 使用 sorted 函数和 lambda 表达式进行排序 sorted_array = sorted(inputArray, key=lambda x: (x[0], -x[1])) # 返回排序后的结果 lenth = len(sorted_array) L, R = sorted_array[0][0], sorted_array[0][1] sum = R-L for i in range(1, lenth): l, r = sorted_array[i][0], sorted_array[i][1] if r <= R: continue if l >= R: sum += r - l L, R = l, r else : sum += r - R R = r

# print(sum)
return sum

if name == "main": # 测试用例 testArray1 = [[1, 4], [7, 10], [3, 5]] testArray2 = [[1, 2], [6, 10], [11, 15]] print(solution(testArray1) == 7 ) print(solution(testArray2) == 9 )

' 运行运行 判断数组是否单调 问题描述 小S最近在研究一些数组的性质,她发现有一种非常有趣的数组被称为 单调数组。如果一个数组是单调递增或单调递减的,那么它就是单调的。

当对于所有索引i <= j时,nums[i] <= nums[j],数组nums是单调递增的。 当对于所有索引i <= j时,nums[i] >= nums[j],数组nums是单调递减的。 你需要编写一个程序来判断给定的数组nums是否为单调数组。如果是,返回true,否则返回false。

测试样例 样例1:

输入:nums = [1, 2, 2, 3] 输出:True

样例2:

输入:nums = [6, 5, 4, 4] 输出:True

样例3:

输入:nums = [1, 3, 2, 4, 5] 输出:False

方法 反着想 直接找到不上升或者不下降的时候打标记即可

def solution(nums: list) -> int: increasing = decreasing = True

for i in range(1, len(nums)):
    if nums[i] > nums[i - 1]:
        decreasing = False
    elif nums[i] < nums[i - 1]:
        increasing = False
        
return 1 if increasing or decreasing else 0

if name == 'main': print(solution(nums=[1, 2, 2, 3]) == 1) print(solution(nums=[6, 5, 4, 4]) == 1) print(solution(nums=[1, 3, 2, 4, 5]) == 0) ' ————————————————

                        版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                    

原文链接:blog.csdn.net/Tankoldbang…