Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented/Added #LeetCode324 (Wiggle Sort) to JAVA. #36

Merged
merged 1 commit into from
Oct 30, 2024

Conversation

naman648
Copy link
Contributor

@naman648 naman648 commented Oct 29, 2024

Problem: Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....

You may assume the input array always has a valid answer.

Example 1:

Input: nums = [1,5,1,1,6,4]
Output: [1,6,1,5,1,4]
Explanation: [1,4,1,5,1,6] is also accepted.

Summary by Sourcery

New Features:

  • Add a Java implementation for the Wiggle Sort problem, which reorders an integer array such that nums[0] < nums[1] > nums[2] < nums[3]...

Problem: Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....

You may assume the input array always has a valid answer.

Example 1:

Input: nums = [1,5,1,1,6,4]
Output: [1,6,1,5,1,4]
Explanation: [1,4,1,5,1,6] is also accepted.
Copy link
Contributor

sourcery-ai bot commented Oct 29, 2024

Reviewer's Guide by Sourcery

This PR implements a solution to LeetCode problem 324 (Wiggle Sort) in Java. The implementation uses a sorting-based approach where the array is first sorted, and then elements are placed alternately to achieve the wiggle pattern. The solution includes both the core algorithm and a main method for testing with user input.

Class diagram for WiggleSort implementation

classDiagram
    class WiggleSort {
        +wiggleSort(int[] nums)
        +main(String[] args)
    }
Loading

File-Level Changes

Change Details Files
Implemented the wiggle sort algorithm using a sorting-based approach
  • Created a method that clones and sorts the input array
  • Implemented two-pass placement of elements to create wiggle pattern
  • Larger elements are placed at odd indices first
  • Remaining elements are placed at even indices
Java/WiggleSort.java
Added interactive testing functionality
  • Implemented command-line input for array size and elements
  • Added array printing functionality to display results
  • Included user prompts for input guidance
Java/WiggleSort.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @naman648 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The Scanner resource should be closed after use. Consider using try-with-resources or explicitly calling scanner.close().
  • Consider adding input validation to handle null arrays and edge cases (empty or single-element arrays) for more robust code.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Scanner resource should be closed after use

Consider using try-with-resources to ensure proper closure of the Scanner object.

import java.util.Scanner;

public class WiggleSort {
public static void wiggleSort(int[] nums) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider using a single-pass in-place swapping approach instead of sorting and cloning the array.

The current implementation unnecessarily sorts the entire array and uses O(n) extra space. The wiggle property can be achieved more efficiently with a single pass and O(1) space complexity. Here's a simpler approach:

public static void wiggleSort(int[] nums) {
    for (int i = 0; i < nums.length - 1; i++) {
        if ((i % 2 == 0) == (nums[i] > nums[i + 1])) {
            // Swap if current relationship doesn't match desired pattern
            int temp = nums[i];
            nums[i] = nums[i + 1];
            nums[i + 1] = temp;
        }
    }
}

This solution:

  • Eliminates extra array allocation
  • Removes the need for sorting
  • Uses a single pass through the array
  • Maintains the wiggle property by ensuring even indices are local minimums and odd indices are local maximums

@x0lg0n x0lg0n merged commit 64234f4 into x0lg0n:main Oct 30, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants