Skip to content

Commit 2cd3dc1

Browse files
authored
Merge branch 'main' into scala
2 parents fcc64fd + 9e86cff commit 2cd3dc1

File tree

57 files changed

+1749
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1749
-35
lines changed

Diff for: README.md

+33-33
Large diffs are not rendered by default.

Diff for: c/143-Reorder-List.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
9+
struct ListNode* reverse(struct ListNode* head) {
10+
struct ListNode* prev = NULL;
11+
struct ListNode* curr = head;
12+
struct ListNode* next = curr->next;
13+
14+
while (curr != NULL) {
15+
next = curr->next;
16+
curr->next = prev;
17+
prev = curr;
18+
curr = next;
19+
}
20+
21+
return prev;
22+
}
23+
24+
void merge(struct ListNode* l1, struct ListNode* l2) {
25+
while (l1 != NULL) {
26+
struct ListNode* p1 = l1->next;
27+
struct ListNode* p2 = l2->next;
28+
29+
l1->next = l2;
30+
if (p1 == NULL) {
31+
break;
32+
}
33+
l2->next = p1;
34+
35+
l1 = p1;
36+
l2 = p2;
37+
}
38+
}
39+
40+
void reorderList(struct ListNode* head){
41+
if (head->next == NULL) {
42+
return;
43+
}
44+
45+
struct ListNode* prev = NULL;
46+
struct ListNode* slow = head;
47+
struct ListNode* fast = head;
48+
49+
while (fast != NULL && fast->next != NULL) {
50+
prev = slow;
51+
slow = slow->next;
52+
fast = fast->next->next;
53+
}
54+
55+
prev->next = NULL;
56+
57+
struct ListNode* l1 = head;
58+
struct ListNode* l2 = reverse(slow);
59+
60+
merge(l1, l2);
61+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int maxProfit(int* prices, int pricesSize){
2+
int sold = 0;
3+
int hold = INT_MIN;
4+
int rest = 0;
5+
6+
for (int i = 0; i < pricesSize; i++) {
7+
int prevSold = sold;
8+
sold = hold + prices[i];
9+
hold = max(hold, rest - prices[i]);
10+
rest = max(rest, prevSold);
11+
}
12+
return max(sold, rest);
13+
}
14+
15+
// C doesn't have a built-in max function
16+
int max(int a, int b) {
17+
return (a > b) ? a : b;
18+
}

Diff for: c/322-Coin-Change.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Return the fewest number of coins that you need to make up that amount.
3+
Time; O(nm) where n is the amount desired and m the number of coins
4+
Space: O(n)
5+
*/
6+
7+
int min(unsigned int a, int b) {
8+
return a<b?a:b;
9+
}
10+
11+
int coinChange(int* coins, int coinsSize, int amount){
12+
if (amount==0)
13+
return 0;
14+
int dp[amount +1];
15+
for (int i=1; i<=amount; i++)
16+
dp[i] = UINT_MAX;
17+
dp[0] = 0;
18+
for (int i=1; i<=amount; i++) {
19+
unsigned int cpt = UINT_MAX;
20+
for (int j=0; j<coinsSize; j++) {
21+
if (i>=coins[j] && cpt>dp[i-coins[j]])
22+
cpt = dp[i-coins[j]];
23+
}
24+
if (cpt!=UINT_MAX)
25+
dp[i] = cpt+1;
26+
}
27+
return dp[amount]==UINT_MAX?-1:dp[amount];
28+
}

Diff for: c/36-Valid-Sudoku.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
bool isValidSudoku(char** board, int boardSize, int* boardColSize){
2+
const int cnt = boardSize;
3+
bool row[cnt][cnt];
4+
bool col[cnt][cnt];
5+
bool sub[cnt][cnt];
6+
7+
// initialize all the rows, cols, and sub-boxes to false
8+
for (int r = 0; r < cnt; r++) {
9+
for (int c = 0; c < cnt; c++) {
10+
row[r][c] = false;
11+
col[r][c] = false;
12+
sub[r][c] = false;
13+
}
14+
}
15+
16+
for (int r = 0; r < cnt; r++) {
17+
for (int c = 0; c < cnt; c++) {
18+
// pass if not a number
19+
if (board[r][c] == '.') {
20+
continue;
21+
}
22+
23+
// gets numerical index
24+
int boardIndex = board[r][c] - '0' - 1;
25+
int area = (r / 3) * 3 + (c / 3);
26+
27+
// if number exists
28+
if (row[r][boardIndex] || col[c][boardIndex] || sub[area][boardIndex]) {
29+
return false;
30+
}
31+
32+
row[r][boardIndex] = true;
33+
col[c][boardIndex] = true;
34+
sub[area][boardIndex] = true;
35+
}
36+
}
37+
return true;
38+
}

Diff for: c/424-Longest-Repeating-Character-Replacement.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
int characterReplacement(char * s, int k){
2+
int count[26] = {0};
3+
int left = 0, right = 0, maxCount = 0;
4+
5+
while (right < strlen(s)) {
6+
count[s[right] - 'A']++;
7+
maxCount = max(maxCount, count[s[right] - 'A']);
8+
right++;
9+
if (right - left - maxCount > k) {
10+
count[s[left] - 'A']--;
11+
left++;
12+
}
13+
}
14+
return right - left;
15+
}
16+
17+
// C doesn't have a built-in max function
18+
int max(int a, int b) {
19+
return (a > b) ? a : b;
20+
}

Diff for: c/567-Permutation-in-String.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
bool isPermutation(int *count) {
2+
for (int i = 0; i < 26; i++) {
3+
if (count[i] != 0) {
4+
return false;
5+
}
6+
}
7+
return true;
8+
}
9+
10+
bool checkInclusion(char * s1, char * s2){
11+
const int m = strlen(s1);
12+
const int n = strlen(s2);
13+
14+
if (m > n) {
15+
return false;
16+
}
17+
18+
int count[26] = {0};
19+
for (int i = 0; i < m; i++) {
20+
count[s1[i] - 'a']++;
21+
count[s2[i] - 'a']--;
22+
}
23+
24+
if (isPermutation(count)) {
25+
return true;
26+
}
27+
28+
for (int i = m; i < n; i++) {
29+
count[s2[i] - 'a']--;
30+
count[s2[i - m] - 'a']++;
31+
if (isPermutation(count)) {
32+
return true;
33+
}
34+
}
35+
return false;
36+
}

Diff for: c/994-Rotting-Oranges.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
int directions[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
3+
4+
bool rotting_process(int** grid, int rows, int cols, int timestamp) {
5+
bool continue_process = false;
6+
for (int row = 0; row < rows; row++) {
7+
for (int col = 0; col < cols; col++) {
8+
if (grid[row][col] == timestamp) {
9+
for (int i = 0; i < 4; i++) {
10+
int r = row + directions[i][0];
11+
int c = col + directions[i][1];
12+
if (rows > r && r >= 0 && cols > c && c >= 0) {
13+
if (grid[r][c] == 1) {
14+
grid[r][c] = timestamp + 1;
15+
continue_process = true;
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}
22+
return continue_process;
23+
}
24+
25+
int orangesRotting(int** grid, int gridSize, int* gridColSize){
26+
27+
int rows = gridSize;
28+
int cols = gridColSize[0];
29+
30+
int timestamp = 2;
31+
while (rotting_process(grid, rows, cols, timestamp)) {
32+
timestamp += 1;
33+
}
34+
35+
for (int row = 0; row < rows; row++) {
36+
for (int col = 0; col < cols; col++) {
37+
if (grid[row][col] == 1) {
38+
return -1;
39+
}
40+
}
41+
}
42+
return timestamp - 2;
43+
}

Diff for: csharp/115-Distinct-Subsequences.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Solution {
2+
public int NumDistinct(string s, string t) {
3+
var nS = s.Length;
4+
var nT = t.Length;
5+
6+
if (nS < nT) return 0;
7+
8+
var dp = new int[nS + 1, nT + 1];
9+
10+
for (int i = 0; i <= nS; i++) {
11+
dp[i, 0] = 1;
12+
}
13+
14+
for (int i = 1; i <= nS; i++) {
15+
var sIndex = i - 1;
16+
for (int j = 1; j <= nT; j++) {
17+
var tIndex = j - 1;
18+
if (s[sIndex] == t[tIndex]) {
19+
dp[i, j] = dp[i - 1, j - 1] + dp[i - 1, j];
20+
} else {
21+
dp[i, j] = dp[i - 1, j];
22+
}
23+
}
24+
}
25+
26+
return dp[nS, nT];
27+
}
28+
}

Diff for: csharp/124-Binary-Tree-Maximum-Path-Sum.cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
3+
int maxPathSum = Int32.MinValue;
4+
5+
public int MaxPathSum(TreeNode root)
6+
{
7+
DfsMaxPathSum(root);
8+
return maxPathSum;
9+
}
10+
11+
private int DfsMaxPathSum(TreeNode root)
12+
{
13+
if (root == null)
14+
return 0;
15+
16+
int leftMax = DfsMaxPathSum(root.left),
17+
rightMax = DfsMaxPathSum(root.right),
18+
currentMax = 0;
19+
20+
currentMax = Math.Max(currentMax, Math.Max(leftMax + root.val, rightMax + root.val));
21+
maxPathSum = Math.Max(maxPathSum, leftMax + root.val + rightMax);
22+
23+
return currentMax;
24+
}
25+
}

Diff for: csharp/134-Gas-Station.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int CanCompleteCircuit(int[] gas, int[] cost) {
3+
if (gas.Sum() < cost.Sum()) {
4+
return -1;
5+
}
6+
7+
int res = 0, total = 0;
8+
9+
for (int i = 0; i < gas.Length; i++) {
10+
total += gas[i] - cost[i];
11+
if (total < 0) {
12+
total = 0;
13+
res = i + 1;
14+
}
15+
}
16+
return res;
17+
}
18+
}

Diff for: csharp/1383-Maximum-Performance-Of-A-Team.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution {
2+
public class Engineer
3+
{
4+
public int speed;
5+
public int efficiency;
6+
public Engineer(int speed, int efficiency)
7+
{
8+
this.speed = speed;
9+
this.efficiency = efficiency;
10+
}
11+
12+
}
13+
14+
public int MaxPerformance(int n, int[] speed, int[] efficiency, int k)
15+
{
16+
List<Engineer> engineers = new();
17+
for (int i = 0; i < n; i++)
18+
{
19+
engineers.Add(new Engineer(speed[i], efficiency[i]));
20+
}
21+
22+
engineers = engineers.OrderByDescending(x => x.efficiency).ToList();
23+
var queue = new PriorityQueue<int, int>();
24+
long speedTotal = 0, result = 0;
25+
foreach (var engineer in engineers)
26+
{
27+
if (queue.Count > k - 1)
28+
speedTotal -= queue.Dequeue();
29+
queue.Enqueue(engineer.speed, engineer.speed);
30+
speedTotal += engineer.speed;
31+
result = Math.Max(result, speedTotal * engineer.efficiency);
32+
}
33+
34+
return (int)(result % 1000000007);
35+
}
36+
}

Diff for: csharp/150-Evaluate-Reverse-Polish-Notation.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Solution {
2+
private static int evaluate(int b, int a, string op) => op switch{
3+
"+" => a + b,
4+
"-" => a - b,
5+
"*" => a * b,
6+
"/" => a / b,
7+
_ => throw new Exception()
8+
};
9+
public int EvalRPN(string[] tokens) {
10+
var stack = new Stack<int>();
11+
var result = 0;
12+
13+
foreach(var token in tokens) {
14+
int number = 0;
15+
var isNumber = int.TryParse(token, out number);
16+
if(isNumber)
17+
stack.Push(number);
18+
else {
19+
result = evaluate(stack.Pop(), stack.Pop(), token);
20+
stack.Push(result);
21+
}
22+
23+
}
24+
25+
return stack.Pop();
26+
}
27+
}

0 commit comments

Comments
 (0)