Last active
June 21, 2019 09:09
-
-
Save namthatman/db5f67695097bfb65c3b7946f88babdf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <limits.h> | |
#include <math.h> | |
#include <stdbool.h> | |
#include <stddef.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
char* readline(); | |
char** split_string(char*); | |
// Complete the arrayManipulation function below. | |
long arrayManipulation(int n, int queries_rows, int queries_columns, int** queries) { | |
//initialize array of 0 | |
long arr[n]; | |
for (long i = 0; i < (long)n; i++) { | |
arr[i] = 0; | |
} | |
long maximum = arr[0]; | |
//perform operation | |
for (long i = 0; i < (long)queries_rows; i++) { | |
long index_A = (long)queries[i][0]; | |
long index_B = (long)queries[i][1]; | |
long value_K = (long)queries[i][2]; | |
for (long j = index_A-1; j < index_B; j++) { | |
arr[j] = arr[j] + value_K; | |
if (maximum < arr[j]) { | |
maximum = arr[j]; | |
} | |
} | |
} | |
return maximum; | |
} | |
int main() | |
{ | |
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); | |
char** nm = split_string(readline()); | |
char* n_endptr; | |
char* n_str = nm[0]; | |
int n = strtol(n_str, &n_endptr, 10); | |
if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } | |
char* m_endptr; | |
char* m_str = nm[1]; | |
int m = strtol(m_str, &m_endptr, 10); | |
if (m_endptr == m_str || *m_endptr != '\0') { exit(EXIT_FAILURE); } | |
int** queries = malloc(m * sizeof(int*)); | |
for (int i = 0; i < m; i++) { | |
*(queries + i) = malloc(3 * (sizeof(int))); | |
char** queries_item_temp = split_string(readline()); | |
for (int j = 0; j < 3; j++) { | |
char* queries_item_endptr; | |
char* queries_item_str = *(queries_item_temp + j); | |
int queries_item = strtol(queries_item_str, &queries_item_endptr, 10); | |
if (queries_item_endptr == queries_item_str || *queries_item_endptr != '\0') { exit(EXIT_FAILURE); } | |
*(*(queries + i) + j) = queries_item; | |
} | |
} | |
int queries_rows = m; | |
int queries_columns = 3; | |
long result = arrayManipulation(n, queries_rows, queries_columns, queries); | |
fprintf(fptr, "%ld\n", result); | |
fclose(fptr); | |
return 0; | |
} | |
char* readline() { | |
size_t alloc_length = 1024; | |
size_t data_length = 0; | |
char* data = malloc(alloc_length); | |
while (true) { | |
char* cursor = data + data_length; | |
char* line = fgets(cursor, alloc_length - data_length, stdin); | |
if (!line) { break; } | |
data_length += strlen(cursor); | |
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } | |
size_t new_length = alloc_length << 1; | |
data = realloc(data, new_length); | |
if (!data) { break; } | |
alloc_length = new_length; | |
} | |
if (data[data_length - 1] == '\n') { | |
data[data_length - 1] = '\0'; | |
} | |
data = realloc(data, data_length); | |
return data; | |
} | |
char** split_string(char* str) { | |
char** splits = NULL; | |
char* token = strtok(str, " "); | |
int spaces = 0; | |
while (token) { | |
splits = realloc(splits, sizeof(char*) * ++spaces); | |
if (!splits) { | |
return splits; | |
} | |
splits[spaces - 1] = token; | |
token = strtok(NULL, " "); | |
} | |
return splits; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the arrayManipulation function below. | |
def arrayManipulation(n, queries): | |
arr = [0 for x in range(n)] | |
for i in range(len(queries)): | |
a = queries[i][0] | |
b = queries[i][1] | |
k = queries[i][2] | |
arr[a-1] += k | |
arr[b] -= k | |
for i in range (1,n): | |
arr[i] += arr[i-1] | |
return max(arr) | |
if __name__ == '__main__': | |
fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
nm = input().split() | |
n = int(nm[0]) | |
m = int(nm[1]) | |
queries = [] | |
for _ in range(m): | |
queries.append(list(map(int, input().rstrip().split()))) | |
result = arrayManipulation(n, queries) | |
fptr.write(str(result) + '\n') | |
fptr.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array. | |
For example, the length of your array of zeros n = 10. Your list of queries is as follows: | |
a b k | |
1 5 3 | |
4 8 7 | |
6 9 1 | |
Add the values of k between the indices a and b inclusive: | |
index-> 1 2 3 4 5 6 7 8 9 10 | |
[0,0,0, 0, 0,0,0,0,0, 0] | |
[3,3,3, 3, 3,0,0,0,0, 0] | |
[3,3,3,10,10,7,7,7,0, 0] | |
[3,3,3,10,10,8,8,8,1, 0] | |
The largest value is 10 after all operations are performed. | |
Function Description | |
Complete the function arrayManipulation in the editor below. It must return an integer, the maximum value in the resulting array. | |
arrayManipulation has the following parameters: | |
n - the number of elements in your array | |
queries - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment