forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
59 changed files
with
1,794 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* struct ListNode *next; | ||
* }; | ||
*/ | ||
|
||
struct ListNode* reverse(struct ListNode* head) { | ||
struct ListNode* prev = NULL; | ||
struct ListNode* curr = head; | ||
struct ListNode* next = curr->next; | ||
|
||
while (curr != NULL) { | ||
next = curr->next; | ||
curr->next = prev; | ||
prev = curr; | ||
curr = next; | ||
} | ||
|
||
return prev; | ||
} | ||
|
||
void merge(struct ListNode* l1, struct ListNode* l2) { | ||
while (l1 != NULL) { | ||
struct ListNode* p1 = l1->next; | ||
struct ListNode* p2 = l2->next; | ||
|
||
l1->next = l2; | ||
if (p1 == NULL) { | ||
break; | ||
} | ||
l2->next = p1; | ||
|
||
l1 = p1; | ||
l2 = p2; | ||
} | ||
} | ||
|
||
void reorderList(struct ListNode* head){ | ||
if (head->next == NULL) { | ||
return; | ||
} | ||
|
||
struct ListNode* prev = NULL; | ||
struct ListNode* slow = head; | ||
struct ListNode* fast = head; | ||
|
||
while (fast != NULL && fast->next != NULL) { | ||
prev = slow; | ||
slow = slow->next; | ||
fast = fast->next->next; | ||
} | ||
|
||
prev->next = NULL; | ||
|
||
struct ListNode* l1 = head; | ||
struct ListNode* l2 = reverse(slow); | ||
|
||
merge(l1, l2); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
int maxProfit(int* prices, int pricesSize){ | ||
int sold = 0; | ||
int hold = INT_MIN; | ||
int rest = 0; | ||
|
||
for (int i = 0; i < pricesSize; i++) { | ||
int prevSold = sold; | ||
sold = hold + prices[i]; | ||
hold = max(hold, rest - prices[i]); | ||
rest = max(rest, prevSold); | ||
} | ||
return max(sold, rest); | ||
} | ||
|
||
// C doesn't have a built-in max function | ||
int max(int a, int b) { | ||
return (a > b) ? a : b; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Return the fewest number of coins that you need to make up that amount. | ||
Time; O(nm) where n is the amount desired and m the number of coins | ||
Space: O(n) | ||
*/ | ||
|
||
int min(unsigned int a, int b) { | ||
return a<b?a:b; | ||
} | ||
|
||
int coinChange(int* coins, int coinsSize, int amount){ | ||
if (amount==0) | ||
return 0; | ||
int dp[amount +1]; | ||
for (int i=1; i<=amount; i++) | ||
dp[i] = UINT_MAX; | ||
dp[0] = 0; | ||
for (int i=1; i<=amount; i++) { | ||
unsigned int cpt = UINT_MAX; | ||
for (int j=0; j<coinsSize; j++) { | ||
if (i>=coins[j] && cpt>dp[i-coins[j]]) | ||
cpt = dp[i-coins[j]]; | ||
} | ||
if (cpt!=UINT_MAX) | ||
dp[i] = cpt+1; | ||
} | ||
return dp[amount]==UINT_MAX?-1:dp[amount]; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
bool isValidSudoku(char** board, int boardSize, int* boardColSize){ | ||
const int cnt = boardSize; | ||
bool row[cnt][cnt]; | ||
bool col[cnt][cnt]; | ||
bool sub[cnt][cnt]; | ||
|
||
// initialize all the rows, cols, and sub-boxes to false | ||
for (int r = 0; r < cnt; r++) { | ||
for (int c = 0; c < cnt; c++) { | ||
row[r][c] = false; | ||
col[r][c] = false; | ||
sub[r][c] = false; | ||
} | ||
} | ||
|
||
for (int r = 0; r < cnt; r++) { | ||
for (int c = 0; c < cnt; c++) { | ||
// pass if not a number | ||
if (board[r][c] == '.') { | ||
continue; | ||
} | ||
|
||
// gets numerical index | ||
int boardIndex = board[r][c] - '0' - 1; | ||
int area = (r / 3) * 3 + (c / 3); | ||
|
||
// if number exists | ||
if (row[r][boardIndex] || col[c][boardIndex] || sub[area][boardIndex]) { | ||
return false; | ||
} | ||
|
||
row[r][boardIndex] = true; | ||
col[c][boardIndex] = true; | ||
sub[area][boardIndex] = true; | ||
} | ||
} | ||
return true; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
int characterReplacement(char * s, int k){ | ||
int count[26] = {0}; | ||
int left = 0, right = 0, maxCount = 0; | ||
|
||
while (right < strlen(s)) { | ||
count[s[right] - 'A']++; | ||
maxCount = max(maxCount, count[s[right] - 'A']); | ||
right++; | ||
if (right - left - maxCount > k) { | ||
count[s[left] - 'A']--; | ||
left++; | ||
} | ||
} | ||
return right - left; | ||
} | ||
|
||
// C doesn't have a built-in max function | ||
int max(int a, int b) { | ||
return (a > b) ? a : b; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
bool isPermutation(int *count) { | ||
for (int i = 0; i < 26; i++) { | ||
if (count[i] != 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
bool checkInclusion(char * s1, char * s2){ | ||
const int m = strlen(s1); | ||
const int n = strlen(s2); | ||
|
||
if (m > n) { | ||
return false; | ||
} | ||
|
||
int count[26] = {0}; | ||
for (int i = 0; i < m; i++) { | ||
count[s1[i] - 'a']++; | ||
count[s2[i] - 'a']--; | ||
} | ||
|
||
if (isPermutation(count)) { | ||
return true; | ||
} | ||
|
||
for (int i = m; i < n; i++) { | ||
count[s2[i] - 'a']--; | ||
count[s2[i - m] - 'a']++; | ||
if (isPermutation(count)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
int directions[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; | ||
|
||
bool rotting_process(int** grid, int rows, int cols, int timestamp) { | ||
bool continue_process = false; | ||
for (int row = 0; row < rows; row++) { | ||
for (int col = 0; col < cols; col++) { | ||
if (grid[row][col] == timestamp) { | ||
for (int i = 0; i < 4; i++) { | ||
int r = row + directions[i][0]; | ||
int c = col + directions[i][1]; | ||
if (rows > r && r >= 0 && cols > c && c >= 0) { | ||
if (grid[r][c] == 1) { | ||
grid[r][c] = timestamp + 1; | ||
continue_process = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return continue_process; | ||
} | ||
|
||
int orangesRotting(int** grid, int gridSize, int* gridColSize){ | ||
|
||
int rows = gridSize; | ||
int cols = gridColSize[0]; | ||
|
||
int timestamp = 2; | ||
while (rotting_process(grid, rows, cols, timestamp)) { | ||
timestamp += 1; | ||
} | ||
|
||
for (int row = 0; row < rows; row++) { | ||
for (int col = 0; col < cols; col++) { | ||
if (grid[row][col] == 1) { | ||
return -1; | ||
} | ||
} | ||
} | ||
return timestamp - 2; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
public class Solution { | ||
public int NumDistinct(string s, string t) { | ||
var nS = s.Length; | ||
var nT = t.Length; | ||
|
||
if (nS < nT) return 0; | ||
|
||
var dp = new int[nS + 1, nT + 1]; | ||
|
||
for (int i = 0; i <= nS; i++) { | ||
dp[i, 0] = 1; | ||
} | ||
|
||
for (int i = 1; i <= nS; i++) { | ||
var sIndex = i - 1; | ||
for (int j = 1; j <= nT; j++) { | ||
var tIndex = j - 1; | ||
if (s[sIndex] == t[tIndex]) { | ||
dp[i, j] = dp[i - 1, j - 1] + dp[i - 1, j]; | ||
} else { | ||
dp[i, j] = dp[i - 1, j]; | ||
} | ||
} | ||
} | ||
|
||
return dp[nS, nT]; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
public class Solution { | ||
|
||
int maxPathSum = Int32.MinValue; | ||
|
||
public int MaxPathSum(TreeNode root) | ||
{ | ||
DfsMaxPathSum(root); | ||
return maxPathSum; | ||
} | ||
|
||
private int DfsMaxPathSum(TreeNode root) | ||
{ | ||
if (root == null) | ||
return 0; | ||
|
||
int leftMax = DfsMaxPathSum(root.left), | ||
rightMax = DfsMaxPathSum(root.right), | ||
currentMax = 0; | ||
|
||
currentMax = Math.Max(currentMax, Math.Max(leftMax + root.val, rightMax + root.val)); | ||
maxPathSum = Math.Max(maxPathSum, leftMax + root.val + rightMax); | ||
|
||
return currentMax; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Solution { | ||
public int CanCompleteCircuit(int[] gas, int[] cost) { | ||
if (gas.Sum() < cost.Sum()) { | ||
return -1; | ||
} | ||
|
||
int res = 0, total = 0; | ||
|
||
for (int i = 0; i < gas.Length; i++) { | ||
total += gas[i] - cost[i]; | ||
if (total < 0) { | ||
total = 0; | ||
res = i + 1; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
public class Solution { | ||
public class Engineer | ||
{ | ||
public int speed; | ||
public int efficiency; | ||
public Engineer(int speed, int efficiency) | ||
{ | ||
this.speed = speed; | ||
this.efficiency = efficiency; | ||
} | ||
|
||
} | ||
|
||
public int MaxPerformance(int n, int[] speed, int[] efficiency, int k) | ||
{ | ||
List<Engineer> engineers = new(); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
engineers.Add(new Engineer(speed[i], efficiency[i])); | ||
} | ||
|
||
engineers = engineers.OrderByDescending(x => x.efficiency).ToList(); | ||
var queue = new PriorityQueue<int, int>(); | ||
long speedTotal = 0, result = 0; | ||
foreach (var engineer in engineers) | ||
{ | ||
if (queue.Count > k - 1) | ||
speedTotal -= queue.Dequeue(); | ||
queue.Enqueue(engineer.speed, engineer.speed); | ||
speedTotal += engineer.speed; | ||
result = Math.Max(result, speedTotal * engineer.efficiency); | ||
} | ||
|
||
return (int)(result % 1000000007); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public class Solution { | ||
private static int evaluate(int b, int a, string op) => op switch{ | ||
"+" => a + b, | ||
"-" => a - b, | ||
"*" => a * b, | ||
"/" => a / b, | ||
_ => throw new Exception() | ||
}; | ||
public int EvalRPN(string[] tokens) { | ||
var stack = new Stack<int>(); | ||
var result = 0; | ||
|
||
foreach(var token in tokens) { | ||
int number = 0; | ||
var isNumber = int.TryParse(token, out number); | ||
if(isNumber) | ||
stack.Push(number); | ||
else { | ||
result = evaluate(stack.Pop(), stack.Pop(), token); | ||
stack.Push(result); | ||
} | ||
|
||
} | ||
|
||
return stack.Pop(); | ||
} | ||
} |
Oops, something went wrong.