当前位置: 首页 > article >正文

算法两道题

算法一

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 required
    int moves = A[0];  # We need at least A[0] moves to reach the first element's value
    
    for (int i = 1; i < n; ++i) {
        # If current element is greater than the previous one, 
        # we need extra moves to reach it
        if (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.kler.cn/news/308026.html

相关文章:

  • PyCharm 安装
  • 【重学 MySQL】二十八、SQL99语法新特性之自然连接和 using 连接
  • 无人机在战争方面的应用!!!
  • 计算机毕业设计 基于协同过滤算法的个性化音乐推荐系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
  • unity3d入门教程七
  • 如何编写智能合约——基于长安链的Go语言的合约开发
  • 我想要抓取新加坡当地电商平台数据,使用什么地区的IP最合适
  • C++数据排序( 附源码 )
  • linux-硬件与设备管理-硬件信息查看
  • 计算机网络 第3章 数据链路层
  • JS - 获取剪切板内容 Clipboard API
  • 数据结构——栈和队列(队列的定义、顺序队列以及链式队列的基本操作)
  • opencv学习:图像直方图均衡化与对比度受限的自适应直方图均衡化及实验代码
  • 针对特定接口记录审核日志类的写入数据库的方法
  • raksmart的G口大流量服务器怎么样?
  • C++学习, 数据抽象
  • 物联网架构
  • 单片机带隙电压基准电路
  • C#环境搭建和入门教程--vs2022之下
  • 51单片机应用开发---数码管的控制应用
  • c++_list
  • 前端开发macbook——NVM环境配置以及git配置流程
  • 《论软件需求管理》写作框架,软考高级系统架构设计师
  • TCP/IP - TCP
  • MySQL5.7基于mysqldump、xtrbackup、innobackupex工具进行全量备份/恢复、增量备份/恢复
  • 【编程基础知识】Java处理JSON格式转换的常用第三方库
  • 面试经典150题——多数元素
  • 表格标记<table>
  • [Linux]:动静态库
  • Python的学习步骤