算法两道题

算法一

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的操作。

python 复制代码
# 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.

  1. 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挑出第三大的数字即可。

相关推荐
Promising_GEO1 分钟前
GEE实战02期:从卫星波段开始:利用 Sentinel-2 自己计算 NDVI
java·sentinel·遥感·空间分析
大文说跨境7 分钟前
亚马逊多账号管理浏览器怎么选?云登/紫鸟/AdsPower 对比
java·前端·自动化
CoderYanger22 分钟前
Java EE 进阶:4.Spring Web MVC 入门(第一弹)
java·开发语言·前端·spring·面试·java-ee·mvc
估值探索者36 分钟前
【Python量化选股流水线 #04】选股器好不好用?「昨日候选→今日表现」回看验收
开发语言·python·股票数据·股票数据api接口
顽疲43 分钟前
java springboot vue 养老系统源码详解:请假 care_leave 销假与床位停费日扣实践
java·vue.js·spring boot
XIAEE1 小时前
一个即时通讯项目是怎么长出来的:(Spring Boot 3 + Vue 3 微前端 + 悟空 IM + LiveKit)
java·spring
n87651 小时前
Playwright Java WEB UI 自动化实现框架与逻辑
java·前端·ui
Java_AI工程师1 小时前
Spring4 / SpringBoot1.x 老项目想接入 AI?不用升级框架!3 套无痛落地方案
java·人工智能·spring boot
BBmmo1 小时前
Java Stream 的 filter 为什么看起来没运行?
java
VimorphTech1 小时前
Java 文件缓存实战:如何避免读到未下载完成的视频?
java·spring boot