0% found this document useful (0 votes)
18 views

Array List

Uploaded by

henrykun100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Array List

Uploaded by

henrykun100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

int buyCar(int* nums, int length, int k) {

sort(nums, nums + length);

int result = 0;

int sum = 0;

for (int i = 0; i < length; i++) {

if (sum + nums[i] <= k) {

sum += nums[i];

result++;

else {

break;

return result;

}
int longestSublist(vector<string>& words) {

// STUDENT ANSWER

int longest = 0;

int current = 0;

for (int i = 0; i < words.size(); i++) {

if (i > 0 && words[i][0] != words[i - 1][0]) {

longest = max(longest, current);

current = 1;

} else {

current++;

return max(longest, current);

}
template<class T>

void ArrayList<T>::ensureCapacity(int cap){

/*

if cap == capacity:

new_capacity = capacity * 1.5;

create new array with new_capacity

else: do nothing

*/

if (cap >= capacity) {

int new_capacity = capacity * 1.5;

T* new_data = new T[new_capacity];

for (int i = 0; i < count; i++) {

new_data[i] = data[i];

delete[] data;

data = new_data;

capacity = new_capacity;

}
template <class T>

void ArrayList<T>::add(T e) {

/* Insert an element into the end of the array. */

ensureCapacity(count);

data[count] = e;

count++;

template<class T>

void ArrayList<T>::add(int index, T e) {

/*

Insert an element into the array at given index.

if index is invalid:

throw std::out_of_range("the input index is out of range!");

*/

if (index < 0 || index > count) {

throw std::out_of_range("Index out of range");

ensureCapacity(count + 1);

for (int i = count; i > index; i--) {

data[i] = data [i-1];

data[index] = e;

count++;
}

template<class T>

int ArrayList<T>::size() {

/* Return the length (size) of the array */

return count;

}
template<class T>

T ArrayList<T>::removeAt(int index){

/*

Remove element at index and return removed value

if index is invalid:

throw std::out_of_range("index is out of range");

*/

if (index < 0 || index >= count) {

throw std::out_of_range("index is out of range");

T removedValue = data[index];

for (int i = index; i < count - 1; i++) {

data[i] = data[i + 1];

count--;
return removedValue;

template<class T>

bool ArrayList<T>::removeItem(T item){

/* Remove the first apperance of item in array and return true, otherwise return false */

for (int i = 0; i < count; i++) {

if (data[i] == item) {

removeAt(i);

return true;

return false;

template<class T>

void ArrayList<T>::clear(){

/*

Delete array if array is not NULL

Create new array with: size = 0, capacity = 5

*/

delete[] data;

capacity = 5;

count = 0;

data = new T[5];

}
vector<int> updateArrayPerRange(vector<int>& nums, vector<vector<int>>& operations) {

// STUDENT ANSWER

int n = nums.size();

vector<int> result(n);

for (const auto& operation : operations) {

int L = operation[0];

int R = operation[1];

int X = operation[2];

for (int i = L; i <= R; i++) {

result[i] += X;

for (int i = 0; i < n; i++) {

result[i] += nums[i];

}
return result;

You might also like