算法两道题

算法一

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, 01, 1, 12, 1, 12, 1, 22, 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, 01, 1, 0, 0, 02, 2, 0, 0, 02, 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, 01, 1, 1, 1, 12, 2, 2, 2, 13, 3, 2, 2, 14, 4, 2, 2, 15, 4, 2, 2, 15, 4, 2, 3, 15, 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挑出第三大的数字即可。

相关推荐
程序员清风9 小时前
公司要裁员之前,都会有哪些信号?
java·后端·面试
Flittly9 小时前
【AgentScope Java新手村系列】(20)文字转语音TTS
java·spring boot·spring
帅次9 小时前
Kotlin Flow 与 StateFlow:UI 单向数据流基础
开发语言·ui·kotlin·stateflow·onlifecycle
唠点键盘之外的10 小时前
10 Spring Boot + 大模型:Java 项目接入 AI 的三种姿势
java·spring boot·aigc·cursor
NWU_LK10 小时前
【WebFlux】第五篇 —— 两种编程模型与 WebClient
java
艾伦野鸽ggg10 小时前
JavaScript 浏览器本地存储
开发语言·javascript·ecmascript
增量星球10 小时前
《持续交付2.0系列六》业务需求协作管理
java·运维·自动化·devops·持续部署·持续集成
重生之我是Java开发战士10 小时前
【Java EE】Spring IoC 与 DI
java·spring·java-ee
SimonKing10 小时前
阿里要求全员卸载 Claude Code:事件始末与深层逻辑
java·后端·程序员
云烟成雨TD10 小时前
Agent Scope Java 2.x 系列【42】2.0 GA 正式发布:从透明开发迈向智能体系统工程
java·人工智能·agent