算法两道题

server/2024/9/22 18:29:16/

算法一

Write a function:

int solution(vector<int>&A);

that, given an array A of length N, returns as an integer the minimum number of moves needed to transform a zero-filled array into A.

Examples:

1. Given A = [2, 1, 3], the function should return 4. For example, the following sequence of moves leads to the solution: [0, 0, 0] → [1, 1, 1] → [2, 1, 1]→ [2, 1, 2] → [2, 1, 3].

2. Given A = [2, 2, 0, 0, 1], the function should return 3. The following sequence of moves leads to the solution: [0, 0, 0, 0, 0] → [1, 1, 0, 0, 0] → [2, 2, 0, 0, 0] → [2, 2, 0, 0, 1].

3. Given A = [5, 4, 2, 4, 1], the function should return 7. One possible optimal sequence of moves is: [0, 0, 0, 0, 0] → [1, 1, 1, 1, 1] → [2, 2, 2, 2, 1] → [3, 3, 2, 2, 1] → [4, 4, 2, 2, 1] → [5, 4, 2, 2, 1] → [5, 4, 2, 3, 1] → [5, 4, 2, 4, 1].

Write an efficient algorithm for the following assumptions:

N is an integer within the range [1..100,000]; each element of array A is an integer within therange [0..1,000,000,000]:

we guarantee that the answer will not exceed1,000,000,000.

上面这道题大概就是将一个全0数组变成到目标数组需要多少步骤。每次只能对连续的数字进行增加1的操作。

# vector<int>& A
def solution(A) {int n = len(A);if (n == 0) return 0;# Variable to store the total number of moves requiredint moves = A[0];  # We need at least A[0] moves to reach the first element's valuefor (int i = 1; i < n; ++i) {# If current element is greater than the previous one, # we need extra moves to reach itif (A[i] > A[i - 1]) {moves += (A[i] - A[i - 1]);}}return moves;
}

算法二

There is an array, named `digits`, consisting of N digits. Choose at most three digits (not necessarily adjacent) and merge them into a new integer without changing the order of the digits. What is the biggest number that can be obtained this way?

Write a function:

def solution(digits)

that, given an array of N digits, returns the biggest number that can be built.

Examples:
1. Given digits = [7, 2, 3, 3, 4, 9], the function
should return 749.
2. Given digits = [0, 0, 5, 7], the function should
return 57.

Assume that:
N is an integer within the range [3..50];
each element of array, named `digits`, is an
integer within the range [0..9].

In your solution, focus on correctness. The
performance of your solution will not be the focus of
the assessment.

算法是要从一个数组里面选出三个数用来组成一个新的三位数,这三个数字的相对位置不能改变,求能够组成的最大的数字。

算法思路就是先从0~n-2之前挑出最大的数字,然后从上一个数字的索引往后直到n-1之前挑出第二个最大的数字,再从上一个数字的索引到n挑出第三大的数字即可。


http://www.ppmy.cn/server/118274.html

相关文章

redis:全局ID生成器实现

问题&#xff1a;订单id不能设置为自增长的原因 id的规律性太明显&#xff0c; 受订单的数据量限制:若数据量过大&#xff0c;需要多张表存储&#xff0c;若自增会导致id重复 全局ID生成器&#xff1a;在分布式系统中用来生成全局唯一ID的工具 ID的组成&#xff1a; 符号位…

微信小程序中的实时通讯:TCP/UDP 协议实现详解

文章目录 前言一、实时通讯的基础知识二、微信小程序中 TCP/UDP 的支持2.1 TCP 实现2.2 UDP 实现 三、实现即时通讯的基本架构四、实际开发中的注意事项4.1 网络环境问题4.2 数据格式与协议设计4.3 消息重发机制 五、实时通讯中的性能优化5.1 减少不必要的通信5.2 数据压缩5.3 …

mysql的分区表

1.SQL表创建 下面以时间范围进行创建&#xff08;每月一个分区&#xff0c;表中创建了四个月的分区&#xff09; 创建&#xff1a;CREATE TABLE test_table ( id INT NOT NULL AUTO_INCREMENT, content VARCHAR(255), create_time DATETIME NOT NULL,PRIMARY KEY (id, cre…

Flask中的钩子函数

在Flask中&#xff0c;钩子函数&#xff08;Hook Functions&#xff09;或称为回调函数&#xff08;Callback Functions&#xff09;是特殊的函数&#xff0c;它们在Flask的请求处理流程中的特定点被自动调用。这些钩子函数允许你在请求被处理之前或之后、视图函数执行之前或之…

9.11-kubeadm方式安装k8s

一、安装环境 编号主机名称ip地址1k8s-master192.168.2.662k8s-node01192.168.2.773k8s-node02192.168.2.88 二、前期准备 1.设置免密登录 [rootk8s-master ~]# ssh-keygen [rootk8s-master ~]# ssh-copy-id root192.168.2.77 [rootk8s-master ~]# ssh-copy-id root192.168…

P2P应用

当谈论P2P&#xff08;点对点&#xff09;应用程序时&#xff0c;我们实际上是在讨论一种网络架构和通信模式&#xff0c;它允许设备&#xff08;或节点&#xff09;直接连接并共享资源&#xff0c;而无需传统的客户端-服务器模型。P2P应用程序在许多领域都有广泛的应用&#x…

RocketMQ 消费方式

在消息传递系统中&#xff0c;“推&#xff08;Push&#xff09;”和“拉&#xff08;Pull&#xff09;”是两种不同的消息消费方式&#xff0c;RocketMQ 也支持这两种模式。下面是对这两种模式的详细解释&#xff1a; 1. 推模式&#xff08;Push Model&#xff09; 模式简介…

少儿编程小游戏 | Scratch 火柴人团队冒险:重装上阵

在线玩&#xff1a;Scratch多人游戏-《火柴人团队冒险&#xff1a;重装上阵》免费下载-小虎鲸Scratch资源站 在少儿编程的世界中&#xff0c;团队合作和冒险精神是激发孩子创造力的重要元素。今天我们将为大家介绍一款Scratch平台上的经典少儿编程小游戏——《火柴人团队冒险&a…