Skip to content

Commit

Permalink
Merge branch 'master' into update_versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Panquesito7 authored Jul 19, 2023
2 parents f5dae5d + f241de9 commit 07b2f15
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 38 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @Panquesito7 @tjgurwara99 @alexpantyukhin
28 changes: 18 additions & 10 deletions .github/workflows/leetcode_directory_writer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ on:
push:
paths:
- "leetcode/src/**.c"
branches:
- master
jobs:
build:
if: github.repository == 'TheAlgorithms/C' # We only need this to run in our repository.
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -21,17 +24,22 @@ jobs:
- name: Write LeetCode DIRECTORY.md
run: |
python3 scripts/leetcode_directory_md.py 2>&1 | tee leetcode/DIRECTORY.md
- name: Commit and push changes
uses: stefanzweifel/git-auto-commit-action@v4
id: commit-push
with:
commit_message: "docs: updating `leetcode/DIRECTORY.md`"
branch: "leetcode-directory-${{ github.sha }}"
create_branch: true
- name: Creating and merging the PR
- name: Setup Git configurations
shell: bash
run: |
git config --global user.name github-actions[bot]
git config --global user.email '[email protected]'
- name: Committing changes
shell: bash
run: |
git checkout -b leetcode-directory-${{ github.sha }}
git commit -m "docs: updating `leetcode/DIRECTORY.md`
git push origin leetcode-directory-${{ github.sha }}:leetcode-directory-${{ github.sha }}
- name: Creating the pull request
shell: bash
if: steps.commit-push.outputs.changes_detected == 'true'
run: |
gh pr create --base ${GITHUB_REF##*/} --head leetcode-directory-${{ github.sha }} --title 'docs: updating `leetcode/DIRECTORY.md`' --body 'Updated LeetCode directory (see the diff. for changes).'
if [[ `git status --porcelain` ]]; then
gh pr create --base ${GITHUB_REF##*/} --head leetcode-directory-${{ github.sha }} --title 'docs: updating `leetcode/DIRECTORY.md`' --body 'Updated LeetCode directory (see the diff. for changes).'
fi
env:
GH_TOKEN: ${{ github.token }}
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ add_subdirectory(process_scheduling_algorithms)
add_subdirectory(numerical_methods)
add_subdirectory(math)
add_subdirectory(cipher)
add_subdirectory(dynamic_programming)

## Configure Doxygen documentation system
cmake_policy(SET CMP0054 NEW)
Expand Down
4 changes: 2 additions & 2 deletions data_structures/list/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ int List_length(L list)
{
int n;
for (n = 0; list; list = list->next) n++;
return n;
return n - 1;
}

/* Convert list to array */
void **List_toArray(L list)
{
int i, n = List_length(list);
int i, n = List_length(list) + 1;
void **array = (void **)malloc((n + 1) * sizeof(*array));

for (i = 0; i < n; i++)
Expand Down
18 changes: 18 additions & 0 deletions dynamic_programming/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. The RELATIVE flag makes it easier to extract an executable's name
# automatically.

file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.c )
foreach( testsourcefile ${APP_SOURCES} )
string( REPLACE ".c" "" testname ${testsourcefile} ) # File type. Example: `.c`
add_executable( ${testname} ${testsourcefile} )

if(OpenMP_C_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_C)
endif()
if(MATH_LIBRARY)
target_link_libraries(${testname} ${MATH_LIBRARY})
endif()
install(TARGETS ${testname} DESTINATION "bin/dynamic_programming") # Folder name. Do NOT include `<>`

endforeach( testsourcefile ${APP_SOURCES} )
64 changes: 38 additions & 26 deletions dynamic_programming/lcs.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/**
* @file
* @brief [Longest Common Subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) algorithm
* @brief [Longest Common
* Subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem)
* algorithm
* @details
* From Wikipedia: The longest common subsequence (LCS) problem is the problem
* of finding the longest subsequence common to all sequences in a set of sequences
* (often just two sequences).
* of finding the longest subsequence common to all sequences in a set of
* sequences (often just two sequences).
* @author [Kurtz](https://github.com/itskurtz)
*/

#include <stdio.h> /* for io operations */
#include <stdlib.h> /* for memory management & exit */
#include <string.h> /* for string manipulation & ooperations */
Expand All @@ -15,13 +18,13 @@
enum {LEFT, UP, DIAG};

/**
* @breif Computes LCS between s1 and s2 using a dynamic-programming approach
* @param1 s1 first null-terminated string
* @param2 s2 second null-terminated string
* @param3 l1 length of s1
* @param4 l2 length of s2
* @param5 L matrix of size l1 x l2
* @param6 B matrix of size l1 x l2
* @brief Computes LCS between s1 and s2 using a dynamic-programming approach
* @param s1 first null-terminated string
* @param s2 second null-terminated string
* @param l1 length of s1
* @param l2 length of s2
* @param L matrix of size l1 x l2
* @param B matrix of size l1 x l2
* @returns void
*/
void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {
Expand All @@ -31,8 +34,8 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {

/* loop over the simbols in my sequences
save the directions according to the LCS */
for (i = 1; i <= l1; ++i)
for (j = 1; j <= l2; ++j)
for (i = 1; i <= l1; ++i) {
for (j = 1; j <= l2; ++j) {
if (s1[i-1] == s2[j-1]) {
L[i][j] = 1 + L[i-1][j-1];
B[i][j] = DIAG;
Expand All @@ -44,16 +47,18 @@ void lcslen(const char *s1, const char *s2, int l1, int l2, int **L, int **B) {
else {
L[i][j] = L[i-1][j];
B[i][j] = UP;
}
}
}
}
}

/**
* @breif Builds the LCS according to B using a traceback approach
* @param1 s1 first null-terminated string
* @param2 l1 length of s1
* @param3 l2 length of s2
* @param4 L matrix of size l1 x l2
* @param5 B matrix of size l1 x l2
* @brief Builds the LCS according to B using a traceback approach
* @param s1 first null-terminated string
* @param l1 length of s1
* @param l2 length of s2
* @param L matrix of size l1 x l2
* @param B matrix of size l1 x l2
* @returns lcs longest common subsequence
*/
char *lcsbuild(const char *s1, int l1, int l2, int **L, int **B) {
Expand All @@ -76,13 +81,18 @@ char *lcsbuild(const char *s1, int l1, int l2, int **L, int **B) {
i = i - 1;
j = j - 1;
}
else if (B[i][j] == LEFT)
j = j - 1;
else
i = i - 1;
else if (B[i][j] == LEFT)
{
j = j - 1;
}
else
{
i = i - 1;
}
}
return lcs;
}

/**
* @brief Self-test implementations
* @returns void
Expand Down Expand Up @@ -132,9 +142,11 @@ static void test() {
printf("LCS len:%3d\n", L[l1][l2]);
printf("LCS: %s\n", lcs);

free(lcs);
for (j = 0; j <= l1; j++)
free(L[j]), free(B[j]);
free(lcs);
for (j = 0; j <= l1; j++)
{
free(L[j]), free(B[j]);
}
free(L);
free(B);

Expand Down
23 changes: 23 additions & 0 deletions leetcode/src/69.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//using the binary search method is one of the efficient ones for this problem statement.
int mySqrt(int x){
int start=0;
int end=x;
long long int ans=0;
while(start <= end){
long long int mid=(start+end)/2;
long long int val=mid*mid;
if( val == x){
return mid;
}
//if mid is less than the square root of the number(x) store the value of mid in ans.
if( val < x){
ans = mid;
start = mid+1;
}
//if mid is greater than the square root of the number(x) then ssign the value mid-1 to end.
if( val > x){
end = mid-1;
}
}
return ans;
}

0 comments on commit 07b2f15

Please sign in to comment.