{"id":22513604,"url":"https://github.com/emahtab/queue-reconstruction-by-height","last_synced_at":"2026-02-11T13:02:35.715Z","repository":{"id":79525719,"uuid":"273002902","full_name":"eMahtab/queue-reconstruction-by-height","owner":"eMahtab","description":"Place persons at their correct location in a queue","archived":false,"fork":false,"pushed_at":"2020-06-17T15:27:08.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-25T00:03:59.594Z","etag":null,"topics":["leetcode","problem-solving","sorting"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-06-17T14:56:23.000Z","updated_at":"2020-06-17T15:28:46.000Z","dependencies_parsed_at":"2023-05-10T17:15:57.116Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/queue-reconstruction-by-height","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/queue-reconstruction-by-height","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fqueue-reconstruction-by-height","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fqueue-reconstruction-by-height/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fqueue-reconstruction-by-height/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fqueue-reconstruction-by-height/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/queue-reconstruction-by-height/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fqueue-reconstruction-by-height/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29333155,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T12:42:24.625Z","status":"ssl_error","status_checked_at":"2026-02-11T12:41:23.344Z","response_time":97,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","problem-solving","sorting"],"created_at":"2024-12-07T03:13:40.329Z","updated_at":"2026-02-11T13:02:35.688Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Queue Reconstruction by height\n## https://leetcode.com/problems/queue-reconstruction-by-height\n\nSuppose you have a random list of people standing in a queue. Each person is described by a pair of integers `(h, k)`, where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue.\n\n**Note: The number of people is less than 1,100.**\n\n``` \nExample\n\nInput:\n[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]\n\nOutput:\n[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]\n```\n\n## Approach :\n1. First sort the input array `people` in such a way that, persons are sorted in `descending order` of their height, and if multiple persons have same height, then they will be sorted in `ascending order` of index k. So input array `[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]` will become `[[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]`\n\n2. Next iterate over the sorted array `people` and add the person `people[i]` at index `people[i][1]` of the list `result`, this solves the problem automagically.\n```\nLets see an e.g.\n\nInput array : `[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]`\nAfter sorting : `[[7,0], [7,1], [6,1], [5,0], [5,2], [4,4]]`\n\nNext lets iterate over sorted array and add people[i] at index people[i][1] of list `result`\nresult =\u003e [[7,0]]\nresult =\u003e [[7,0], [7,1]]\nresult =\u003e [[7,0], [6,1], [7,1]]\nresult =\u003e [[5,0], [7,0], [6,1], [7,1]]\nresult =\u003e [[5,0], [7,0], [5,2], [6,1], [7,1]]\nresult =\u003e [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]\n```\n# Implementation :\n```java\nclass Solution {\n    public int[][] reconstructQueue(int[][] people) {\n        if(people == null || people.length \u003c= 1)\n            return people;\n        \n        // We sort the array based on descending order of height\n        // If two persons have same height, we sort in increasing order of index k\n        Arrays.sort(people, (person1, person2) -\u003e {\n           if(person1[0] != person2[0])\n               return person2[0] - person1[0];\n           else\n               return person1[1] - person2[1];\n        });\n        \n        List\u003cint[]\u003e result = new ArrayList\u003c\u003e();\n        for(int[] person : people) {\n            result.add(person[1], person);\n        }\n        \n        return result.toArray(new int[people.length][]);\n    }\n}\n```\n\n### Complexity Analysis\n\n**Time complexity : O(N^2)**\nTo sort people takes O(NlogN) time. Then one proceeds to n insert operations, and each takes up to O(k) time, where k is a current number of elements in the list. In total, one needs up to O(N^2) time for insertion.\n\n**Space complexity : O(N) to keep the result.**\n\n# References ;\n1. https://leetcode.com/articles/queue-reconstruction-by-height\n2. https://evelynn.gitbooks.io/google-interview/queue-reconstruction-by-height.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fqueue-reconstruction-by-height","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fqueue-reconstruction-by-height","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fqueue-reconstruction-by-height/lists"}