0% found this document useful (0 votes)
17 views4 pages

Array List

Uploaded by

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

Array List

Uploaded by

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

1.

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


sort(nums, nums + length);
int max = 0, sum = 0;
for (int i = 0; i < length; i++) {
sum += nums[i];
if (sum > k) break;
max++;
}
return max;
}

2.
bool consecutiveOnes(vector<int>& nums) {
// STUDENT ANSWER
bool a = false;
vector<int> check;
if (nums.size() == 0) return true;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == 1) {
if (check.size() == 0 && a == true) {
return false;
}
check.push_back(1);
if (check.size() > 1) {
a = true;
}
} else {
check.clear();
}
}
return a;
}

3.
int equalSumIndex(vector<int>& nums) {
// STUDENT ANSWER
int right = 0;
int left = 0;
for (int i = 1; i < nums.size(); i++) {
right += nums[i];
}
if (left == right) return 0;
for (int i = 1; i < nums.size(); i++) {
left += nums[i - 1];
right -= nums[i];
if (left == right) {
return i;
}
}
return -1;
}

4.
int longestSublist(vector<string>& words) {
if (words.size() == 0) return 0;
vector<char> check;
check.push_back(words[0][0]);
int count = 1;
int max = 0;
for (int i = 1; i < words.size(); i++) {
if (words[i][0] == check[0]) {
count++;
}
else {
check.clear();
check.push_back(words[i][0]);
count = 1;
}
if (count >= max) max = count;
}
return max;
}

5.
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!");
*/
ensureCapacity(count + 1);
count++;
for (int i = count - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
}

template<class T>
int ArrayList<T>::size() {
/* Return the length (size) of the array */
return count;
}

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;
}
}

6.
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 - 1) {
throw out_of_range("index is out of range");
}
int temp = data[index];
for (int i = index; i < count; i++) {
data[i] = data[i + 1];
}
count--;
return temp;
}

template<class T>
bool ArrayList<T>::removeItem(T item) {
/* Remove the first apperance of item in array and return true, otherwise
return false */
int index = -1;
for (int i = 0; i < count; i++) {
if (data[i] == item) {
index = i;
break;
}
if (i == count - 1) {
return false;
}
}
removeAt(index);
return true;
}

template<class T>
void ArrayList<T>::clear() {
/*
Delete array if array is not NULL
Create new array with: size = 0, capacity = 5
*/
if (data != NULL) {
delete[] data;
}
capacity = 5;
count = 0;
T* new_data = new T(capacity);
data = new_data;
}

7.
vector<int> updateArrayPerRange(vector<int>& nums, vector<vector<int>>& operations)
{
for (size_t i = 0; i < operations.size(); i++) {
int L = operations[i][0];
int R = operations[i][1];
int add = operations[i][2];
for (int j = L; j <= R; j++) {
nums[j] += add;
}
}
return nums;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy