Skip to content

Commit

Permalink
June9,10,11: daily challenge [M]
Browse files Browse the repository at this point in the history
  • Loading branch information
aucker committed Jun 11, 2024
1 parent bdb3e50 commit f70d5c7
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
27 changes: 27 additions & 0 deletions daily/June10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief LC: 881: Boats to save people
* GREEDY: Time: O(nlogn), Space: O(logn)
*
* @param people
* @param limit
* @return int
*/
int numRescueBoats(vector<int>& people, int limit) {
int len = people.size();
int le = 0, ri = len - 1;
int ans = 0;
while (le <= ri) {
if (people[le] + people[ri] <= limit) {
le++;
}
ri--;
ans++;
}
return ans;
}
};
25 changes: 25 additions & 0 deletions daily/June11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief LC: 419: Battleships in board
* Time: O(mn), Space: O(1)
*
* @param board
* @return int
*/
int countBattleships(vector<vector<int>>& board) {
int ans = 0;
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[0].size(); j++) {
if (board[i][j] == 'X' && (j == 0 || board[i][j - 1] != 'X') &&
(i == 0 || board[i - 1][j] != 'X')) {
ans++;
}
}
}
return ans;
}
};
33 changes: 33 additions & 0 deletions daily/June9.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
/**
* @brief LC: 312: Burst Ballons [H][M]
* Time: O(N^3), Space: O(N^2)
*
* @param nums
* @return int
*/
int maxCoins(vector<int>& nums) {
int n = nums.size();
vector<vector<int>> rec(n + 2, vector<int>(n + 2));
vector<int> val(n + 2);
val[0] = val[n + 1] = 1;

for (int i = 1; i <= n; i++) {
val[i] = nums[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 2; j <= n + 1; j++) {
for (int k = i + 1; k < j; k++) {
int sum = val[i] * val[k] * val[j];
sum += rec[i][k] + rec[k][j];
rec[i][j] = max(rec[i][j], sum);
}
}
}
return rec[0][n + 1];
}
};

0 comments on commit f70d5c7

Please sign in to comment.