Problem
Assume you have an array of length n initialized with all 0"s and are given k update operations.
Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc.
Return the modified array after all k operations were executed.
ExampleGiven:
length = 5,
updates =
[ [1, 3, 2], [2, 4, 3], [0, 2, -2] ]
return [-2, 0, 3, 5, 3]
Initial state:
[ 0, 0, 0, 0, 0 ]
After applying operation [1, 3, 2]:
[ 0, 2, 2, 2, 0 ]
After applying operation [2, 4, 3]:
[ 0, 2, 5, 5, 3 ]
After applying operation [0, 2, -2]:
[-2, 0, 3, 5, 3 ]
public class Solution { /** * @param length: the length of the array * @param updates: update operations * @return: the modified array after all k operations were executed */ public int[] getModifiedArray(int length, int[][] updates) { // Write your code here int[] res = new int[length]; Arrays.fill(res, 0); for (int[] update: updates) { for (int i = update[0]; i <= update[1]; i++) { res[i] += update[2]; } } return res; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/71674.html
摘要:首先,建立二元結(jié)果數(shù)組,起點(diǎn),終點(diǎn)。二分法求左邊界當(dāng)中點(diǎn)小于,移向中點(diǎn),否則移向中點(diǎn)先判斷起點(diǎn),再判斷終點(diǎn)是否等于,如果是,賦值給。 Problem Given a sorted array of n integers, find the starting and ending position of a given target value. If the target is not...
摘要:題目為求從到的自然數(shù)里取個(gè)數(shù)的所有組合全集。使用遞歸的模板,建立函數(shù)。模板如下也可以不建立新的,而是遞歸調(diào)用之后刪去中最后一個(gè)元素 Problem Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example For example,If n = 4 a...
Problem Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words. Example Given s = lintcode, dict = [lint, code]. R...
Problem Given a string, find the first non-repeating character in it and return its index. If it doesnt exist, return -1. Example Given s = lintcode, return 0. Given s = lovelintcode, return 2. Tags A...
摘要:建立兩個(gè)堆,一個(gè)堆就是本身,也就是一個(gè)最小堆另一個(gè)要寫一個(gè),使之成為一個(gè)最大堆。我們把遍歷過的數(shù)組元素對(duì)半分到兩個(gè)堆里,更大的數(shù)放在最小堆,較小的數(shù)放在最大堆。同時(shí),確保最大堆的比最小堆大,才能從最大堆的頂端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...
閱讀 2045·2023-04-26 02:15
閱讀 2311·2021-11-19 09:40
閱讀 1061·2021-10-27 14:13
閱讀 3329·2021-08-23 09:44
閱讀 3623·2019-12-27 12:24
閱讀 665·2019-08-30 15:53
閱讀 1181·2019-08-30 10:53
閱讀 2171·2019-08-26 12:14