Skip to content

Commit 5f7fe72

Browse files
Merge branch 'neetcode-gh:main' into main
2 parents e0ef7f7 + 6a4e00c commit 5f7fe72

26 files changed

+690
-34
lines changed

Diff for: README.md

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

Diff for: cpp/0075-Sort-colors.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
void sortColors(vector<int>& nums) {
4+
int p1=0,p2=nums.size()-1;
5+
for(int i=p1;i<=p2;i++)
6+
{
7+
if(nums[i]==0)
8+
{
9+
swap(nums[i],nums[p1]);
10+
p1++;
11+
}
12+
if(nums[i]==2)
13+
{
14+
swap(nums[i],nums[p2]);
15+
p2--;
16+
i--;
17+
}
18+
}
19+
20+
21+
}
22+
};

Diff for: cpp/0088-Merge-sorted-array.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
4+
int j=0;
5+
int i=0;
6+
if(n==0) return;
7+
if(m==0)
8+
{
9+
for(int i = 0; i < n; i++){
10+
nums1[i] = nums2[i];
11+
} return;
12+
}
13+
while(i<m)
14+
{
15+
if(nums1[i]>nums2[j])
16+
{
17+
swap(nums1[i],nums2[j]);
18+
sort(nums2.begin(),nums2.end());
19+
}
20+
i++;
21+
}
22+
j=0;
23+
while(i<m+n)
24+
{
25+
nums1[i] = nums2[j];
26+
j++;
27+
i++;
28+
}
29+
30+
}
31+
};

Diff for: go/0010-regular-expression-matching.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
func isMatch(s string, p string) bool {
2+
// dp[i][j] is true if p[:i] matches s[:j]
3+
dp := make([][]bool, len(p) + 1)
4+
for i := 0; i < len(p) + 1; i++ {
5+
dp[i] = make([]bool, len(s) + 1)
6+
for j := 0; j < len(s) + 1; j++ {
7+
dp[i][j] = false
8+
}
9+
}
10+
11+
dp[0][0] = true
12+
13+
// Base case for i = 0 is already set up, as empty pattern can only match empty string
14+
// But a nonempty pattern can match an empty string, so we do base cases for j = 0
15+
for i := 1; i < len(p); i++ {
16+
if p[i] == '*' {
17+
dp[i + 1][0] = dp[i - 1][0]
18+
}
19+
}
20+
21+
// Now for the general case
22+
for i := 0; i < len(p); i++ {
23+
for j := 0; j < len(s); j++ {
24+
if p[i] == '.' || p[i] == s[j] {
25+
// Single character matches
26+
dp[i + 1][j + 1] = dp[i][j]
27+
} else if p[i] == '*' {
28+
// Wildcard - check that the character matches
29+
// or if we can have 0 repetitions of the previous char
30+
dp[i + 1][j + 1] = dp[i - 1][j + 1] || dp[i][j + 1]
31+
if p[i - 1] == '.' || p[i - 1] == s[j] {
32+
if dp[i + 1][j] {
33+
dp[i + 1][j + 1] = dp[i + 1][j]
34+
}
35+
}
36+
}
37+
}
38+
}
39+
40+
return dp[len(p)][len(s)]
41+
}

Diff for: go/0127-word-ladder.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
func ladderLength(beginWord string, endWord string, wordList []string) int {
2+
if !contains(wordList, endWord) {
3+
return 0
4+
}
5+
6+
nei := make(map[string][]string)
7+
wordList = append(wordList, beginWord)
8+
for _, word := range wordList {
9+
for j := 0; j < len(word); j++ {
10+
pattern := word[:j] + "*" + word[j + 1:]
11+
nei[pattern] = append(nei[pattern], word)
12+
}
13+
}
14+
15+
visit := map[string]bool{beginWord: true}
16+
q := []string{beginWord}
17+
res := 1
18+
for len(q) != 0 {
19+
for tmp := len(q); tmp > 0; tmp-- {
20+
word := q[0]
21+
q = q[1:]
22+
if word == endWord {
23+
return res
24+
}
25+
for j := 0; j < len(word); j++ {
26+
pattern := word[:j] + "*" + word[j + 1:]
27+
for _, neiWord := range nei[pattern] {
28+
if !visit[neiWord] {
29+
visit[neiWord] = true
30+
q = append(q, neiWord)
31+
}
32+
}
33+
}
34+
}
35+
res += 1
36+
}
37+
return 0
38+
}
39+
40+
func contains(s []string, word string) bool {
41+
for _, element := range s {
42+
if element == word {
43+
return true
44+
}
45+
}
46+
return false
47+
}

Diff for: go/0210-course-schedule-ii.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const CRS = 0
2+
const PRE = 1
3+
func findOrder(numCourses int, prerequisites [][]int) []int {
4+
prereq := make([][]int, 0)
5+
for i := 0; i < numCourses; i++ {
6+
prereq = append(prereq, make([]int, 0))
7+
}
8+
for _, edge := range prerequisites {
9+
prereq[edge[CRS]] = append(prereq[edge[CRS]], edge[PRE])
10+
}
11+
12+
output := make([]int, 0)
13+
visit, cycle := make([]bool, numCourses), make([]bool, numCourses)
14+
15+
var dfs func(int) bool
16+
dfs = func(crs int) bool {
17+
if cycle[crs] {
18+
return false
19+
} else if visit[crs] {
20+
return true
21+
}
22+
23+
cycle[crs] = true
24+
for _, pre := range prereq[crs] {
25+
if !dfs(pre) {
26+
return false
27+
}
28+
}
29+
cycle[crs] = false
30+
visit[crs] = true
31+
output = append(output, crs)
32+
return true
33+
}
34+
35+
for c := 0; c < numCourses; c++ {
36+
if dfs(c) == false {
37+
return []int{}
38+
}
39+
}
40+
return output
41+
}

Diff for: go/0283-move-zeroes.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func moveZeroes(nums []int) {
2+
for l, r := 0, 0; r < len(nums); r++ {
3+
if nums[r] != 0 {
4+
nums[l], nums[r] = nums[r], nums[l]
5+
l++
6+
}
7+
}
8+
}

Diff for: go/0283-move-zeros.go

-13
This file was deleted.

Diff for: go/0290-word-pattern.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func wordPattern(pattern string, s string) bool {
2+
words := strings.Split(s, " ")
3+
if len(pattern) != len(words) {
4+
return false
5+
}
6+
7+
seenChr, seenWord := make(map[byte]int, len(pattern)), make(map[string]int, len(pattern))
8+
for i := 0; i < len(pattern); i++ {
9+
chrPos, chrOk := seenChr[pattern[i]]
10+
wordPos, wordOk := seenWord[words[i]]
11+
if chrOk != wordOk || chrPos != wordPos {
12+
return false
13+
}
14+
15+
seenChr[pattern[i]] = i
16+
seenWord[words[i]] = i
17+
}
18+
19+
return true
20+
}

Diff for: go/0295-find-median-from-data-stream.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
type IntHeap []int
2+
func (h IntHeap) Len() int { return len(h) }
3+
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
4+
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
5+
func (h *IntHeap) Push(x interface{}) {*h = append(*h, x.(int))}
6+
func (h *IntHeap) Pop() interface{} {
7+
old := *h
8+
n := len(old)
9+
x := old[n-1]
10+
*h = old[0 : n-1]
11+
return x
12+
}
13+
14+
type MedianFinder struct {
15+
small IntHeap
16+
large IntHeap
17+
}
18+
19+
20+
func Constructor() MedianFinder {
21+
return MedianFinder{small: IntHeap{}, large: IntHeap{}}
22+
}
23+
24+
25+
func (this *MedianFinder) AddNum(num int) {
26+
if len(this.large) > 0 && num > this.large[0] {
27+
heap.Push(&this.large, num)
28+
} else {
29+
heap.Push(&this.small, -1 * num)
30+
}
31+
32+
if len(this.small) > len(this.large) + 1 {
33+
val := -1 * heap.Pop(&this.small).(int)
34+
heap.Push(&this.large, val)
35+
}
36+
if len(this.large) > len(this.small) + 1 {
37+
val := heap.Pop(&this.large).(int)
38+
heap.Push(&this.small, -1 * val)
39+
}
40+
}
41+
42+
43+
func (this *MedianFinder) FindMedian() float64 {
44+
if len(this.small) > len(this.large) {
45+
return float64(-1 * this.small[0])
46+
} else if len(this.large) > len(this.small) {
47+
return float64(this.large[0])
48+
}
49+
return float64(-1 * this.small[0] + this.large[0]) / 2
50+
}

Diff for: go/0417-pacific-atlantic-water-flow.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
func pacificAtlantic(heights [][]int) [][]int {
2+
ROWS, COLS := len(heights), len(heights[0])
3+
pac, atl := make(map[int]bool), make(map[int] bool)
4+
5+
var dfs func(int, int, map[int]bool, int)
6+
dfs = func(r, c int, visit map[int]bool, prevHeight int) {
7+
if (
8+
visit[r*COLS + c] ||
9+
r < 0 ||
10+
c < 0 ||
11+
r == ROWS ||
12+
c == COLS ||
13+
heights[r][c] < prevHeight) {
14+
return;
15+
}
16+
visit[r*COLS + c] = true
17+
dfs(r + 1, c, visit, heights[r][c])
18+
dfs(r - 1, c, visit, heights[r][c])
19+
dfs(r, c + 1, visit, heights[r][c])
20+
dfs(r, c - 1, visit, heights[r][c])
21+
}
22+
23+
for c := 0; c < COLS; c++ {
24+
dfs(0, c, pac, heights[0][c])
25+
dfs(ROWS - 1, c, atl, heights[ROWS - 1][c])
26+
}
27+
28+
for r := 0; r < ROWS; r++ {
29+
dfs(r, 0, pac, heights[r][0])
30+
dfs(r, COLS - 1, atl, heights[r][COLS - 1])
31+
}
32+
33+
res := make([][]int, 0)
34+
for r := 0; r < ROWS; r++ {
35+
for c := 0; c < COLS; c++ {
36+
if pac[r*COLS + c] && atl[r*COLS + c] {
37+
res = append(res, []int{r, c})
38+
}
39+
}
40+
}
41+
return res
42+
}

Diff for: go/0463-island-perimeter.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func islandPerimeter(grid [][]int) int {
2+
ROWS, COLS := len(grid), len(grid[0])
3+
visit := make(map[int]bool)
4+
5+
var dfs func(int, int) int
6+
dfs = func(i, j int) int {
7+
if i >= ROWS || j >= COLS || i < 0 || j < 0 || grid[i][j] == 0 {
8+
return 1
9+
} else if visit[i*COLS + j] {
10+
return 0
11+
}
12+
13+
visit[i*COLS + j] = true
14+
perim := dfs(i, j + 1)
15+
perim += dfs(i + 1, j)
16+
perim += dfs(i, j - 1)
17+
perim += dfs(i - 1, j)
18+
return perim
19+
}
20+
21+
for i := 0; i < ROWS; i++ {
22+
for j := 0; j < COLS; j++ {
23+
if grid[i][j] != 0 {
24+
return dfs(i, j)
25+
}
26+
}
27+
}
28+
return -1
29+
}

Diff for: go/0953-verifying-an-alien-dictionary.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func isAlienSorted(words []string, order string) bool {
2+
// first differing char
3+
// if word A is prefix of word B, word B must be AFTER word A
4+
orderInd := make(map[byte]int)
5+
for i := 0; i < len(order); i++ {
6+
orderInd[order[i]] = i
7+
}
8+
9+
for i := 0; i < len(words) - 1; i++ {
10+
w1, w2 := words[i], words[i + 1]
11+
12+
for j := 0; j < len(w1); j++ {
13+
if j == len(w2) {
14+
return false
15+
}
16+
17+
if w1[j] != w2[j] {
18+
if orderInd[w2[j]] < orderInd[w1[j]] {
19+
return false
20+
}
21+
break
22+
}
23+
}
24+
}
25+
return true
26+
}

0 commit comments

Comments
 (0)