Skip to content

Commit 18f1772

Browse files
Diagnoal Traverse
1 parent cbe1206 commit 18f1772

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

Arrays/Diagnoal_Traveres.ipynb

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "d2398630",
6+
"metadata": {},
7+
"source": [
8+
"# Diagnoal Traverse"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "0b663837",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"[1, 2, 4, 7, 5, 3, 6, 8, 9]\n"
22+
]
23+
}
24+
],
25+
"source": [
26+
"mat = [[1,2,3],[4,5,6],[7,8,9]]\n",
27+
"\n",
28+
"if not mat or not mat[0]:\n",
29+
" print([])\n",
30+
" \n",
31+
"m, n = len(mat), len(mat[0])\n",
32+
"res = []\n",
33+
"row, col = 0, 0\n",
34+
"up = True # direction flag\n",
35+
"\n",
36+
"for _ in range(m * n):\n",
37+
" res.append(mat[row][col])\n",
38+
" \n",
39+
" if up: # moving up-right\n",
40+
" if col == n - 1:\n",
41+
" row += 1\n",
42+
" up = False\n",
43+
" elif row == 0:\n",
44+
" col += 1\n",
45+
" up = False\n",
46+
" else:\n",
47+
" row -= 1\n",
48+
" col += 1\n",
49+
" else: # moving down-left\n",
50+
" if row == m - 1:\n",
51+
" col += 1\n",
52+
" up = True\n",
53+
" elif col == 0:\n",
54+
" row += 1\n",
55+
" up = True\n",
56+
" else:\n",
57+
" row += 1\n",
58+
" col -= 1\n",
59+
"\n",
60+
"print(res)"
61+
]
62+
}
63+
],
64+
"metadata": {
65+
"kernelspec": {
66+
"display_name": "Python 3",
67+
"language": "python",
68+
"name": "python3"
69+
},
70+
"language_info": {
71+
"codemirror_mode": {
72+
"name": "ipython",
73+
"version": 3
74+
},
75+
"file_extension": ".py",
76+
"mimetype": "text/x-python",
77+
"name": "python",
78+
"nbconvert_exporter": "python",
79+
"pygments_lexer": "ipython3",
80+
"version": "3.13.3"
81+
}
82+
},
83+
"nbformat": 4,
84+
"nbformat_minor": 5
85+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "5a2ff0b4",
6+
"metadata": {},
7+
"source": [
8+
"# Partition Array into K distinct parts"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 7,
14+
"id": "dec09bb5",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdout",
19+
"output_type": "stream",
20+
"text": [
21+
"True\n"
22+
]
23+
}
24+
],
25+
"source": [
26+
"from collections import Counter\n",
27+
"\n",
28+
"nums = [1,2,3,4]\n",
29+
"k = 2\n",
30+
"\n",
31+
"if len(nums)%k != 0:\n",
32+
" print(False)\n",
33+
"\n",
34+
"freq = Counter(nums)\n",
35+
"groups = len(nums) // k\n",
36+
"\n",
37+
"for count in freq.values():\n",
38+
" if count>groups:\n",
39+
" print(False)\n",
40+
"\n",
41+
"print(True)"
42+
]
43+
}
44+
],
45+
"metadata": {
46+
"kernelspec": {
47+
"display_name": "Python 3",
48+
"language": "python",
49+
"name": "python3"
50+
},
51+
"language_info": {
52+
"codemirror_mode": {
53+
"name": "ipython",
54+
"version": 3
55+
},
56+
"file_extension": ".py",
57+
"mimetype": "text/x-python",
58+
"name": "python",
59+
"nbconvert_exporter": "python",
60+
"pygments_lexer": "ipython3",
61+
"version": "3.13.3"
62+
}
63+
},
64+
"nbformat": 4,
65+
"nbformat_minor": 5
66+
}

Maths/GCD_Odd_Even.ipynb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "294c6d05",
6+
"metadata": {},
7+
"source": [
8+
"# GCD of N-th ODD & EVEN Number"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 9,
14+
"id": "085c46ef",
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"data": {
19+
"text/plain": [
20+
"6"
21+
]
22+
},
23+
"execution_count": 9,
24+
"metadata": {},
25+
"output_type": "execute_result"
26+
}
27+
],
28+
"source": [
29+
"n = 6\n",
30+
"\n",
31+
"def gcd(a, b):\n",
32+
" while b:\n",
33+
" a, b = b, a%b\n",
34+
" return a\n",
35+
"gcd(n**2, n*(n+1))"
36+
]
37+
}
38+
],
39+
"metadata": {
40+
"kernelspec": {
41+
"display_name": "Python 3",
42+
"language": "python",
43+
"name": "python3"
44+
},
45+
"language_info": {
46+
"codemirror_mode": {
47+
"name": "ipython",
48+
"version": 3
49+
},
50+
"file_extension": ".py",
51+
"mimetype": "text/x-python",
52+
"name": "python",
53+
"nbconvert_exporter": "python",
54+
"pygments_lexer": "ipython3",
55+
"version": "3.13.3"
56+
}
57+
},
58+
"nbformat": 4,
59+
"nbformat_minor": 5
60+
}

0 commit comments

Comments
 (0)